Skip to main content
added 189 characters in body
Source Link
Achintya Jha
  • 12.9k
  • 2
  • 30
  • 40

When compiler optimizes your string literals, it sees that both s1 and s2 have same value and thus you need only one string objectonly one string object. It's safe because String is immutable in Java.

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

This gives result true because s1 and s2 points to the same object.

String Pool is the mechanism that all already defined string are stored in some 'pool' and before creating new String object compiler checks if such string is already defined.

When compiler optimizes your string literals, it sees that both s1 and s2 have same value and thus you need only one string object. It's safe because String is immutable in Java.

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

This gives result true because s1 and s2 points to the same object.

When compiler optimizes your string literals, it sees that both s1 and s2 have same value and thus you need only one string object. It's safe because String is immutable in Java.

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

This gives result true because s1 and s2 points to the same object.

String Pool is the mechanism that all already defined string are stored in some 'pool' and before creating new String object compiler checks if such string is already defined.

Source Link
Achintya Jha
  • 12.9k
  • 2
  • 30
  • 40

When compiler optimizes your string literals, it sees that both s1 and s2 have same value and thus you need only one string object. It's safe because String is immutable in Java.

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

This gives result true because s1 and s2 points to the same object.