In the Java interview, I always like a few questions about the String object, such:
1. String s = new String ("abc"); several String objects are created.
2. String s1 = "abc ";
String s2 = "";
String s3 = s2 + "bc ";
String s4 = "a" + "bc ";
String s5 = s3.intern ();
If s1 = s3 is true or false, s1 = s4 is false or true. What about s1 = s5?
I have encountered a lot of such questions. Today I would like to make a summary of the reason why I dare to say it is comprehensive, because I am referring to the most authoritative materials ...... Java language specification and Java API.
API description:
Strings are constants, and their values cannot be changed after creation. Because the String object is unchangeable, it can be shared.Http://gceclub.sun.com.cn/Java_Docs/jdk6/html/zh_CN/api/java/lang/String.html)
Then, how to share the data? The intern method of the String class is described as follows:
String pool, which is initially null and is privately maintained by the String class.(Http://gceclub.sun.com.cn/Java_Docs/jdk6/html/zh_CN/api/java/lang/String.html#intern ())
That is to say, the String class maintains a pool and places the String object in the program into that pool. Therefore, if the String object with the same value is used in our code, it is the same String object, saves space.
However, no String object is stored in thisString poolOtherwise, there will be no other questions. So which String objects are in the pool and in the heap? See the Java language specification.
The last section of String Literals is as follows:
Literal strings within the same class (section 8) in the same package (section 7) represent references to the same String object (section 4. 3.1 ).
Literal strings within different classes in the same package represent references to the same String object.
Literal strings within different classes in different packages likewise represent references to the same String object.
Strings computed by constant expressions (§ 15. 28) are computed at compile time and then treated as if they were literals.
Strings computed by concatenation at run time are newly created and therefore distinct.
The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
The above six sentences are authoritative and comprehensive explanations. I will explain it in sequence.
What isString Literal constant(String Literals ),Literal constant(Literals) is the value you wrote in the source code, for example, int I = 6; 6 isInteger literal constant. String s = "abc"; "abc" isString Literal constant. In Java, allString Literal constantAs mentioned aboveString poolIt can be shared, that is, String s1 = "abc"; String s2 = "abc"; s1, s2 all reference the same String object, and this object isString poolSo s1 = s2. In addition,String Literal constantWhen is it instantiated and put inString poolIn it? The answer is when I Load the Class (Java Spec 12.5 ).
Let's look at the six sentences below.
The first three statements are basically nonsense. They are the same as tongue twisters, which means that any package of the class has the same value.String Literal constant(String Literals) references the same object.
The fourth sentence isConstant expression(Constant expressions ).String Literal constantThat is to say, they are alsoString pool. What isConstant expression(Constant expressions) This is very important.
Note: In the fifth sentence, the string objects computed by connecting (+) are newly created when the program is running. They are notLiteral constantEven if they have the same value, they are notString poolThey are in the heap memory space, so the referenced objects are different.
The last sentence is also very important. The intern method of the String class returns a String object with the same value, but this object is likeString Literal constantThat is, he alsoString pool.
Now let's look at the first two questions.
1. String s = new String ("abc"); several String objects are created.
There are two answers. One isString Literal constant, InString pool. A new String object is in the heap.
2. String s1 = "abc ";
String s2 = "";
String s3 = s2 + "bc ";
String s4 = "a" + "bc ";
String s5 = s3.intern ();
If s1 = s3 is true or false, s1 = s4 is false or true. What about s1 = s5?
Note two points for this question. Because s2 is a variable, s3 is a string that can be computed at runtime. It is new and is not in the heap.String pool. S4 is throughConstant expressionIt is equivalentString Literal constant, InString pool. So, s1! = S3, s1 = s4. Let's look at s5 again. s5 is put by s3String poolSo s1 = s5. Note that the s3.intern () method returns a string reference in the pool and does not change the reference of the s3 variable, that is, s3 or the "abc" pointing to the heap ", it does not change because the intern () method is called. In fact, it cannot be changed.
Okay. Now let's go back to a question that we didn't clarify. What is it?Constant expression(Constant expressions ). The two extraordinary expressions, new and variable addition, are mentioned above. What is computingConstant expression(Constant expressions), see Java language specifications.
15.28 Constant Expression
ConstantExpression: Expression
A compile-timeConstant expressionIs an expression denoting a value of primitive type orString
That does not complete abruptly and is composed using only the following:
- Literals of primitive type and literals of type
String
- Casts to primitive types and casts to type
String
- The unary operators
+
,-
,~
, And!
(But not++
Or--
)
- The multiplicative operators
*
,/
, And%
- The additive operators
+
And-
- The shift operators
<<
,>>
, And>>>
- The relational operators
<
,<=
,>
, And>=
(But notinstanceof
)
- The capacity ity operators
==
And!=
- The bitwise and logical operators
&
,^
, And|
- The conditional-and operator
&&
And the conditional-or operator||
- The ternary conditional operator
?
:
- Parenthesized expressions whose contained expression is a constant expression.
- Simple names that refer to constant variables
- Qualified names of the formTypeName
.
IdentifierThat refer to constant variables
Compile-time constant expressions are used incase
Labels inswitch
Statements and have a special significance for assignment conversion. Compile-time constants of typeString
Are always "interned" so as to share unique instances, using the methodString.intern.
A compile-time constant expression is always treated as FP-strict, even if it occurs in a context where a non-constant expression wocould not be considered to be FP-strict.
It seems a little dizzy, and there are too many situations. The interview should not be so abnormal. In fact, it looks complicated. Just remember a principle, that is, it can be determined during compilation. Even if it can be determined during runtime, It is not counted. The following is an example
String s = 1 + "23"; // calculation, matching the second Casts to primitive types and casts to typeString
String s = (2> 1) + ""; // indicates that s = "true", and this "true" has been placed inString pool.
String s = (o instanceof Object) + ""; // No. The instanceof operator determines whether to calculate the value. S = "true", but this "true" object is in the heap.
Pay attention to the following situations.
Final String s2 = "";
String s3 = s2 + "bc"; // calculate
Note that the current s2 + "bc" is alsoConstant expressionThe reason is that the last two items in the list, s2 isConstant variable(Constant variables)Constant variable? The specification also says that it was modified by final and passedConstant expressionThe initialized variable isConstant variable. The variable s2 is modified by final. It is initialized by the constant expression "a", so s2 isConstant variableSo the "abc" referenced by s3 is alsoString poolHere, I see.
Here is another counterexample:
Final String s2 = getA (); // s2 is not a constant variable, but the "a" referenced by s2 is still in the constant pool.
Public String getA () {return ""}
String s3 = s2 + "bc"; // s3 is not a constant expression at this time, because s2 is not a constant variable
This is the time s2, notConstant variableBecause getA () is notConstant expression.
Okay, I have said so much about it. I should probably say it clearly. You are welcome to testify for an error. We hope it will be useful to you ~
According to the reply from the netizen lenbias34
This article from the "hate radish test field" blog, please be sure to keep this source http://cymoft.blog.51cto.com/324099/473220