Original address: http://www.cnblogs.com/dolphin0520/p/3778589.html
This question in a lot of books have said, such as "Java Programmer interview book", including many domestic large companies written test questions will encounter, most of the online circulation and some interview books are said to be 2 objects, this statement is one-sided.
If you do not know the place can refer to this post:
http://rednaxelafx.iteye.com/blog/774673/
The first thing you have to figure out is what created the object, and when was it created? Does this code create 2 objects during run time? There is no doubt that the bytecode content of the JVM execution can be obtained with javap-c decompile:
Obviously, new is called only once, which means that only one object is created.
And this is where the problem is confusing, and this code actually creates only one object during the run, that is, the "ABC" object is created on the heap. And why are we all talking about 2 objects, here to clarify a concept the code execution process and the class loading process are different. During class loading, it was true that an "ABC" object was created in the run-time pool, and indeed only one string object was created during the execution of the code.
So, if this problem is replaced by string str = new String ("abc"), how many string objects are involved? A reasonable explanation is 2.
Personally feel that in the interview if you encounter this problem, you can ask the interviewer clearly "is the code during the execution of how many objects created or how many objects" and then according to the specific answer.
Reference Sticker Address: http://rednaxelafx.iteye.com/blog/774673/
Question 1:
New
How many string instances are involved at run time?
Answer: Two, one is the instance of the string literal "xyz", which resides (intern) in a globally shared string constant pool, and the other is an instance of the same content as "XYZ" created and initialized by the new string (string).
Question 2:
New String ("xyz");
Several string types of variables that involve user claims?
Answer: One, string S.
Question 2:
null;
Several string types of variables that involve user claims?
A variable in Java is a variable, and a variable of a reference type is simply a reference to an object instance or null, not the instance itself. Declaring the number of variables is not necessarily related to the number of instances created, like this:
String S1 = "a"; = S1.concat (""); NULL ; New
This code involves 3 variables of type string,
1, S1, pointing to 1 of the following string instances
2, S2, pointing to the same as S1
3, S3, value NULL, not pointing to any instance
and 3 instances of string,
1. String instance of the "a" literal corresponding to the resident string constant
2. String instance of "" literal corresponding to the resident string constant
(String.Concat () is an interesting way to return this when it is found that an incoming parameter is an empty string, so there is no additional string instance created here.
3. New string instance created by new string, without any variables pointing to it.
"Go" string str = new String ("abc") How many objects are created?