0% found this document useful (0 votes)
3 views

Hello String

When a String is declared directly as "Hello", the JVM places the value in the string constant pool and any identical Strings will reference the same object in the pool. However, using new String("Hello") always creates a new distinct String object regardless of what is in the pool, because new implies a distinct object will be created.

Uploaded by

sandeep
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Hello String

When a String is declared directly as "Hello", the JVM places the value in the string constant pool and any identical Strings will reference the same object in the pool. However, using new String("Hello") always creates a new distinct String object regardless of what is in the pool, because new implies a distinct object will be created.

Uploaded by

sandeep
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

When you store a String as

String str1 = "Hello";

directly, then JVM creates a String object with the given value in a separate block of
memory known as String constant pool.

And whenever we try to create another String as


String str2 = "Hello";
JVM verifies whether any String object with the same value exists in the String
constant pool, if so, instead of creating a new object JVM assigns the reference of
the existing object to the new variable.

And when we store String as

String str = new String("Hello");


using the new keyword, a new object with the given value is created irrespective of
the contents of the String constant pool.

Rishi Raj
Published on 10-Jan-2018 14:20:42
 Previous Page  Print Page

You might also like