How strings are processed in java

String a = "abc"; 

This creates the literal "abc" in the string pool.

 String b = "abc"; 

No new literal is created. b indicates an existing "abc" .

 String c = new String("abc"); 

Now the object is created on the heap, and c pointed to the heap. A literal is also created in the row pool.

But what happens if the pool already has the literal "abc" ? Will there be duplicate literals in the pool?

+5
source share
5 answers

But what happens if the pool already has the literal "abc"? Will there be duplicate literals in the pool?

Not.

 String c = new String("abc"); 

semantically equivalent

 String tmp = "abc"; String c = new String(tmp); 

Thus, it should be clear that no additional entry is created in the string pool just because the literal is used as an argument to the string constructor.

You are correct that new String(...) creates a string on the heap, so looking at the heap and the string pool, there will be several objects representing "abc" , but not more than one in the string pool.

[...] That is, when we use string literals. But will a literal be used in the string pool, if we give String a = new String("abc"); ?

Yes, the string pool literal will be reused if you give "abc" as an argument to the constructor. The resulting String object, however, will not be in the pool, but in the heap.

+6
source

But what happens if the pool already has the literal "abc"?

It will be simply reused.

If you are somewhere String x = "abc"; and somewhere else String y = "abc"; then x == y will always be true . This proves that the literal is actually reused.

+3
source

There will be no duplicate entries in the row pool, it will be reused by another link.

String s1="foo"; the literal will go into the pool and s1 will be referenced.

String s2="foo"; this time it will check that the literal "foo" is already available in StringPool or not, since it now exists, so s2 will refer to the same literal.

 String s3=new String("foo"); 

"foo", through the String argument constructor String Object, ie "foo" will be created on the heap due to the creation of the object using the new operator, then s3 will refer to it.

 String interning using inter() method 

Java by default does not put the entire String object in a string pool, instead they give you the flexibility to explicitly store arbitrary objects in a string pool. You can put any object in a String pool by calling the intern () method of the java.lang.String class. Although, when you create using Java literal string notation, it automatically calls intern () to put this object in the string pool if it was no longer in the pool.

0
source

The string literal "abc" already in the string pool before any of this code executes. It is placed there by the compiler and class loader. This does not happen during the execution of this code.

0
source
 String c = new String("abc"); 

Now the object is created on the heap, and c points to the heap. The literal is also created in the string pool.

A literal value will not be created in the row pool. He will remain only in the heap. See this image.

http://www.journaldev.com/797/what-is-java-string-pool

But what happens if the pool already has the literal "abc"? Will there be duplicate literals in the pool?

You cannot duplicate values ​​in a pool. Since strings are immutable object in java. See the second answer in this post from roger_that.

The string is unchanged. What exactly does it mean?

0
source

All Articles