简体   繁体   English

Java - 计算字符串长度

[英]Java - calculating String length

While initializing array, Java ask length for that array.在初始化数组时,Java 会询问该数组的长度。 Otherwise we get compile-time error.否则我们会得到编译时错误。

char[] b = new char[3];

Here 3 is the length of that array b .we can get it from b.length这里3是数组b的长度。我们可以从b.length得到它

When we see constructor of String class当我们看到 String 类的构造函数时

 public String(String original) {
    this.value = original.value;
    this.hash = original.hash;
}

Where that original parameter in String constructor came? String 构造函数中的原始参数来自哪里? How that get its length?那怎么得到它的长度?

In String length method在字符串长度方法中

    public int length() {
    return value.length;
}

MyQuestion #1我的问题 #1

How can we get the length without initialize array(Since no array initialization inside constructor)?我们如何在没有初始化数组的情况下获得长度(因为构造函数内部没有数组初始化)?

MyQuestion #2我的问题 #2

String s = new String("MyString");

In the above line I am creating instance for String class, when I checked the constructor for the String class I come across the following code for the constructor在上面的行中,我正在为String类创建实例,当我检查String类的构造函数时,我遇到了以下构造函数代码

 public String(String original) {
    this.value = original.value;
    this.hash = original.hash;
}

My doubt is inside the constructor how they are having String class itself as a parameter?我的疑问是在构造函数中他们如何将 String 类本身作为参数? From where it is loading?从哪里加载?

The following statement constructs a String instance s out of a "String literal" in this case "MyString"下面的语句构造一个String实例s在这种情况下,“字符串文字”出"MyString"

String s = new String("MyString");

It is equivalent and less preferable to它等效且不太可取

String s = "MyString";

According to the official docs根据官方文档

  1. A string literal is a reference to an instance of class String (§4.3.1, §4.3.3).字符串文字是对 String 类实例的引用(第 4.3.1 节,第 4.3.3 节)。
  2. A string literal always refers to the same instance of class String (interning)字符串字面量总是指类 String 的同一个实例(实习)

Regarding specifically the length of "MyString" , the compiler would count the number of chars in the literal when parsing the source code and initialize the char[] inside that string with the appropriate length.特别是关于"MyString"的长度,编译器会在解析源代码时计算文字中的字符数,并用适当的长度初始化该字符串中的char[] Then it would create a new String s with the same length然后它将创建一个具有相同长度的新String s

As a side note, you can (for similar reasons) create an array without specifying the length yourself.作为旁注,您可以(出于类似原因)创建一个数组而无需自己指定长度。 The compiler will count the number of elements and set the length accordingly Eg编译器将计算元素的数量并相应地设置长度 例如

char[] b = {'a', 'b', 'c'};

Java allows to give parameter of same class object in which you are writing constructor or other methods. Java 允许提供您正在编写构造函数或其他方法的同一类对象的参数。

 public String(String original) {
    this.value = original.value;
    this.hash = original.hash;
}

String class gets length from hash, as hash has a unique code which tells how many chracters are there. String 类从 hash 中获取长度,因为 hash 有一个唯一的代码,它告诉你有多少个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM