java Java中的字符串初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30694275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:31:38  来源:igfitidea点击:

String initialization in Java

javastring

提问by u4679727

  1. String str1;
  2. String str2 = null;
  3. String str3 = "";
  4. String str4 = new String();
  5. String str5 = new String("");
  1. String str1;
  2. String str2 = null;
  3. String str3 = "";
  4. String str4 = new String();
  5. String str5 = new String("");

I know that for 3rd initialization above, the the string object is initialized in the string pool and the 4th has nothing to do with the string pool.

我知道对于上面的第三次初始化,字符串对象在字符串池中初始化,而第四次与字符串池无关。

What is the difference between 1. and 2.? If I consider str1as a pointer variable, what it stores is a particular memory address that is never used by the JVM or OS?

1. 和 2. 和有什么不一样?如果我将其str1视为指针变量,那么它存储的是JVM 或操作系统从未使用过的特定内存地址吗?

Is there a difference between 4. and 5.?

4. 和 5.之间有区别吗?

When I print str1and str2directly by System.out.println(str1)and System.out.println(str2), for str1, I can't even pass the compilation.For str2, compilation is OK and I get "null" and the output in the console window. Why?

当我直接通过and打印str1和, for 时,我什至无法通过编译。For ,编译没问题,我在控制台窗口中得到“null”和输出。为什么?str2System.out.println(str1)System.out.println(str2)str1str2

Edited after the answer of @aioobe: more questions:

在@aioobe 的回答后编辑:更多问题:

I would like to know more about "null". Since str2(reference variable) is like a pointer variable, there should be something (0/1 bits) in it (in the memory occupied by this pointer variable). As it is initialized as null, is it all-0-bits or the bytecode of null is all-zero? Another question is that if I call the method toString() on str2by str2.toString(), I got a NullPointer Error at runtime. So it is JVM that checks if the reference variable is null? How can JVM know that it is null? JVM checks the bits in str2?

我想了解更多关于“null”的信息。由于str2(引用变量)就像一个指针变量,所以它里面(在这个指针变量占用的内存中)应该有一些东西(0/1位)。当它被初始化为空时,它是全 0 位还是空的字节码全为零?另一个问题是,如果我在str2by上调用 toString() 方法str2.toString(),我会在运行时遇到 NullPointer 错误。那么是JVM检查引用变量是否为空?JVM 怎么知道它是空的?JVM 检查str2?

One more question about nullin Java: concatenation of null and a string literal

大约一个问题在Java中: 空的级联和字符串文字

回答by aioobe

What is the difference between 1. and 2.? If I consider str1as a pointer variable, what it stores is a particular memory address that is never used by the JVM or OS?

1. 和 2. 有什么区别?如果我将其str1视为指针变量,那么它存储的是JVM 或操作系统从未使用过的特定内存地址吗?

If these are fields in a class, there's no difference, since the default value for a field of a reference type (such as String) is already null.

如果这些是class中的字段,则没有区别,因为引用类型(例如 String)字段的默认值已经是null

If these are local variables(i.e. variables declared in a method) str1will not be initialized to anything, while str2will be initialized to null. The difference here is that a local variable can't be used until it has been initialized, so (as you seem to have discovered) you can't print str1, but you can print str2.

如果这些是局部变量(即在方法中声明的变量)str1将不会被初始化为任何东西,而str2将被初始化为null. 这里的区别在于局部变量在初始化之前不能使用,因此(正如您似乎已经发现的那样)您不能 print str1,但您可以 print str2

Is there a difference between 4. and 5.?

4. 和 5. 之间有区别吗?

No, not semantically. You'll get slightly different byte code though.

不,不是语义上的。不过,您会得到略有不同的字节码。

When I print str1 and str2 directly by System.out.println(str1) and System.out.println(str2), for str1, I can't even pass the compilation. For str2, compilation is OK and I get "null" and the output in the console window. Why?

当我通过System.out.println(str1)和System.out.println(str2)直接打印str1和str2时,对于str1,我什至无法通过编译。对于 str2,编译没问题,我得到“null”和控制台窗口中的输出。为什么?

This seems to indicate that these are local variables. Local variables needs to be initialized before they are used.

这似乎表明这些是局部变量。局部变量在使用前需要初始化。

I would like to know more about "null". Since str2(reference variable) is like a pointer variable, there should be something (0/1 bits) in it (in the memory occupied by this pointer variable). As it is initialized as null, is it all-0-bits or the bytecode of nullis all-zero?

我想了解更多关于“null”的信息。由于str2(引用变量)就像一个指针变量,所以它里面(在这个指针变量占用的内存中)应该有一些东西(0/1位)。当它被初始化为 时null,它是全 0 位还是null全零的字节码?

This has already been asked (and answered):

这已经被问到(并回答了):

Another question is that if I call the method toString()on str2by str2.toString(), I got a NullPointer Error at runtime. So it is JVM that checks if the reference variable is null?

另一个问题是,如果我toString()str2by上调用该方法str2.toString(),则在运行时会出现 NullPointer 错误。所以 JVM 会检查引用变量是否为null?

Yes.

是的。

How can JVM know that it is null? JVM checks the bits in str2?

JVM 怎么知道它是null? JVM 检查str2?

Yes.

是的。

回答by loshad vtapkah

Every variable in Java that represents an object is in fact not a value, but pointer to (address of) some place in memory that stores that variable. nullis a special type of pointer that means the variable(pointer) is initialized but points to nothing. Since any two String variables are pointers if your compare them like str1 == str2you'll check it variables points to the same instance. To compare content the pointers point to you need to use equals: str1.equals(str2), but in this case we must be sure that str1 is not null.

Java 中表示对象的每个变量实际上都不是值,而是指向(地址)内存中存储该变量的某个位置的指针。 null是一种特殊类型的指针,这意味着变量(指针)已初始化但不指向任何内容。由于任何两个 String 变量都是指针,如果您比较它们,就像str1 == str2检查变量指向同一个实例一样。要比较指针指向的内容,您需要使用 equals: str1.equals(str2),但在这种情况下,我们必须确保 str1 不是null