Creating Literals and Creating String Objects

How many String objects are created

I am learning SCJP. I seem to be leaning towards this String problem. It seems that I see several possible answers depending on how I look at the question.

In the next initialization, how many string objects are created?

String s1 = "A" + "B" + "C" + "D"; System.out.println(s1) 

At first I thought there were 5 objects, i.e.

 "A" "B" "C" "D" "ABCD" 

But then, thinking about this, I'm not sure, because, for example, the compiler will concatenate "A" + "B" as one object? those. create 7 objects?

 "A" "B" "C" "D" "AB" "ABC" "ABCD" 

In addition, how many objects will be created if the code has been changed to

 String s1 = new String("A" + "B" + "C" + "D"); System.out.println(s1); 

And finally, how about:

 String s1 = "A"; String s2 = new String("A"); 

In the above example, I think that only 2 objects will be created

 object 1 - "A" object 2 - a String object that refers to the "A" object above. 

Is this correct or will they not be connected? that is, the object mentioned from the constant pool will be different from the object referenced by the link to s2.

thanks

Edit

In addition, please note that I am interested in knowing the total number of objects created, including those that are discarded not only by those that end up in the constant pool.

Edit

Looking at John’s answer, I may have completely misunderstood how objects are created. I know that String is only created once in the constant pool, and it is reused, but I'm not sure about the process that goes through when the "final" string is built. Here is the section from the book that I am reading, which apparently involves the creation of temporary objects, which is completely opposite to the answers here. (Or maybe the book is wrong or I misunderstood the book)

Sample code was

 String s1 = "spring "; String s2 = s1 + "summer "; s1.concat("fall "); s2.concat(s1); s1 += "winter"; System.out.println(s1 + " " + s2); 

The question was

What is the conclusion? For extra credit, how many String objects and how many reference variables were created before the println statement .

And the answer

The result of this code snippet is spring water spring summer . There are two reference variables, s1 and s2. There were eight in total String objects are created as follows: spring, summer (lost), spring summer, falling (lost), spring fall (lost), spring spring spring (lost), winter "(lost)," winter of winter "(currently" spring "lost). Only two out of eight String objects are not lost in this process.

thanks

+7
source share
3 answers

The compiler combines all "A" + "B" + "C" + "D" into one constant, so in your first example only one line is created. The same line will be reused if you execute the same code several times. The constant is placed in the class file, and when the class is loaded, the virtual machine checks to see if the string is already equal in the string pool, so it will reuse it even if you have the same code in several classes.

You can make sure that only one row is in the constant pool inside the class with javap :

 javap -v Test Constant pool: #1 = Methodref #6.#17 // java/lang/Object."<init>":()V #2 = String #18 // ABCD #3 = Fieldref #19.#20 // java/lang/System.out:Ljava/io/PrintStream; 

However, here:

 String s1 = "A"; String s2 = new String("A"); 

you have two separate string objects. One (constant) will be reused every time you execute the code (and shared between two statements), and each of them will be created due to the call to the constructor.

So, for example, this method:

 public static void foo() { for (int i = 0; i < 5; i++) { String s1 = "A"; String s2 = new String("A"); } } 

... in the end, six string objects will be used - one for the constant and five new ones created each time you call the method.

+13
source

How many objects are created?

 String s1 = "A" + "B" + "C" + "D"; System.out.println(s1) 

One or none. This boils down to a single string literal that may already be loaded.

 String s1 = new String("A" + "B" + "C" + "D"); System.out.println(s1); 

This always creates an additional object.

BTW: A string usually consists of two objects: String and char[] it is wrapped.

+4
source
 String s1 = "A" + "B" + "C" + "D"; 

The compiler will create only one string literal "ABCD" and put it in the string pool. A single object will be created (one that is in the row pool).


 String s1 = new String("A" + "B" + "C" + "D"); 

Same thing, except that you copy it from a string literal. Thus, 2 objects will be created here. One is new and one in the row pool.


 String s1 = "A"; String s2 = new String("A"); 

The same, "A" will be a constant in the row pool. The constructor will copy it. Thus, two objects will be created here.

+2
source

All Articles