String s = new String ( "123") actually created several objects

Foreword

Today, the water of the process to work, someone asked about this issue, this issue is also controversial for the Internet, there are said to create an object, said there are two, there are three said.

First, that three is certainly nonsense, today to discuss this statement in the end we created several objects.

Before entering the topic, let's review a few basics.

Constant pool

The constant pool is to avoid duplication and to create and destroy objects affect system performance in order to achieve the object sharing. Compile the string can be determined, and is stored in the constant pool, such as String s = "123" ;. After that if you use this string to 123, will be available directly from the constant pool, rather than each time a new 123 again out. About constant pool more detailed knowledge, please read this article Java constant pool

Object comparison

Equals the difference between == and presumably do not need my highlights, equals comparison is value == when comparing reference types, comparison is the address.

Into the title

A little introduction about the basics of the above two points, following entered, String s = new String ( "123") actually created several objects? Let me talk conclusion here, for the first time created two, the second time to create one.

Let's analyze this statement. S is a first reference variable, to receive the new String, s therefore not an object. new String is typical to create an object, into the heap. The "123" is also the object 123 can be determined at compile time, and therefore put constant pool. This code can be split into the above two below.

String temp = "123";
String s = new String(temp);

So it is easy to understand, String temp = "123" There is no doubt that an object is created, new String (temp) and creates an object, and therefore is two.

Then create an object to the conclusion that come from?

First, to prove through the code to see if there is the constant pool.

String s1 = "123";
String s2 = "123";
System.out.println(s1 == s2);

s1 and s2 are String object == by comparing the two addresses, the results is true, illustrate both the same address, the same object.

In String s1 = "123"; this statement, creates an object "123", into the constant pool, the following String s2 = "123"; 123 and taken out from the constant pool assigned to s2, s2 so this statement does not create the object.

Then we look at the code below.

String temp = "123";
String s1 = new String(temp);

String temp2 = "123";
String s2 = new String(temp2);

In this code, the above code creates two objects are "123" and the new String ( "123"), and then, 123 is stored in the constant pool. The following code extracted 123 and new String ( "123"), only one object is created.

Epilogue

In String s = new String ( "123") this statement

When there is no constant pool 123, creating two objects.

When there is the constant pool 123, to create an object.

Published 30 original articles · won praise 35 · views 2769

Guess you like

Origin blog.csdn.net/qq_36403693/article/details/103803560