为什么这些字符串没有指向 java 中的同一个对象?
why these strings are not pointing to the same object in java?
我知道如果我有 2 个具有相同值的字符串变量,由于 java 字符串池,它们指向同一个字符串对象。
这是一个例子:
String test = "1234";
String test2 = "1234";
System.out.println(test == test2);
System.out.println("1234" == test2);
输出如下:
true
true
但是如果我有以下代码,它会打印出它们不是同一个对象
String test = "1234";
int i = 1234;
String s = "" + i;
System.out.println(test == s);
System.out.println("1234" == s);
输出:
false
false
有人可以向我解释这种行为的原因吗?
"" + i;
不是常量表达式,所以它没有被驻留。如果i
是final或者直接写成"" + 1234
,那么这个值就会被intern。
Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.
A constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:
- Literals of primitive type and literals of type String (§3.10.1, §3.10.2, §3.10.3, §3.10.4, §3.10.5)
- Casts to primitive types and casts to type String (§15.16)
- The unary operators +, -, ~, and ! (but not ++ or --) (§15.15.3, §15.15.4, §15.15.5, §15.15.6)
- The multiplicative operators *, /, and % (§15.17)
- The additive operators + and - (§15.18)
- The shift operators >, and >>> (§15.19)
- The relational operators , and >= (but not instanceof) (§15.20)
- The equality operators == and != (§15.21)
- The bitwise and logical operators &, ^, and | (§15.22)
- The conditional-and operator && and the conditional-or operator || (§15.23, §15.24)
- The ternary conditional operator ? : (§15.25)
- Parenthesized expressions (§15.8.5) whose contained expression is a constant expression.
- Simple names (§6.5.6.1) that refer to constant variables (§4.12.4).
- Qualified names (§6.5.6.2) of the form TypeName . Identifier that refer to constant variables (§4.12.4).
我知道如果我有 2 个具有相同值的字符串变量,由于 java 字符串池,它们指向同一个字符串对象。
这是一个例子:
String test = "1234";
String test2 = "1234";
System.out.println(test == test2);
System.out.println("1234" == test2);
输出如下:
true
true
但是如果我有以下代码,它会打印出它们不是同一个对象
String test = "1234";
int i = 1234;
String s = "" + i;
System.out.println(test == s);
System.out.println("1234" == s);
输出:
false
false
有人可以向我解释这种行为的原因吗?
"" + i;
不是常量表达式,所以它没有被驻留。如果i
是final或者直接写成"" + 1234
,那么这个值就会被intern。
Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.
A constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:
- Literals of primitive type and literals of type String (§3.10.1, §3.10.2, §3.10.3, §3.10.4, §3.10.5)
- Casts to primitive types and casts to type String (§15.16)
- The unary operators +, -, ~, and ! (but not ++ or --) (§15.15.3, §15.15.4, §15.15.5, §15.15.6)
- The multiplicative operators *, /, and % (§15.17)
- The additive operators + and - (§15.18)
- The shift operators >, and >>> (§15.19)
- The relational operators , and >= (but not instanceof) (§15.20)
- The equality operators == and != (§15.21)
- The bitwise and logical operators &, ^, and | (§15.22)
- The conditional-and operator && and the conditional-or operator || (§15.23, §15.24)
- The ternary conditional operator ? : (§15.25)
- Parenthesized expressions (§15.8.5) whose contained expression is a constant expression.
- Simple names (§6.5.6.1) that refer to constant variables (§4.12.4).
- Qualified names (§6.5.6.2) of the form TypeName . Identifier that refer to constant variables (§4.12.4).