1

I have refernced the following links

What is String pool in Java?

Garbage collection and Strings

http://www.xyzws.com/Javafaq/what-is-string-literal-pool/3

Questions about Java's String pool

Still have some doubts please help me

            `public class StrPool
             { 
               public static void main(String[] args)
               { 
                String abc="hello";
                String abcd="hello";
                System.out.println(abc==abcd);
               }
             }
             `

In the above example OP : true

So we can confirm that both object are refered from same String object Am clear about that.

    `String abc="hello World";
     String abcd="hello";
     System.out.println(abc==abcd);`

This gives output : false

So String pool is carried out when two String object having same literal???

If So two object of String will created in String pool??

Why second output is false ???

I READ THAT String class is immutable

abc and abcd have different object reference then Immutable means

" First String object will created by JVM and give two reference to abc and abcd " Am i right???

Thank you very much........

Community
  • 1
  • 1
Amith
  • 1,907
  • 5
  • 29
  • 48

4 Answers4

1

The second output is false as both the string abc and abcd having different content(text)

yes as you said String class is immutable ie their content does not gets changed, if in case you change the string text then JVM will allocate a new space instead of altering it.

Now when you create a new String reference with some text at that time JVM will check if that text already exists in pool and if then will refer your string to that text or else will create new text within pool

Harmeet Singh
  • 2,555
  • 18
  • 21
1

Consider first code,

While executing statement String abc="hello", JVM will do following things 1. create a reference abc of type String. 2. check if literal "hello" is available in string pool if available, it will assign its reference value to abc, else it will create string literal "hello", assign its reference to abc and add it to String pool.

Same step will be repeated for String abcd="hello". Here, as "hello" is already available in string pool, same will be referred by abcd.

These same steps will be followed in second code block as well, (in fact everytime you create string object with such statement) just that the literal "hello world" and "hello" will be created differently and assigned to their respective variables.

I guess, now you can guess the reason behind the output of above two code samples.

String is immutable, means that you can not change any string object. Instead JVM will create new string object (or fetch one from string pool) and assign it to the reference. e.g.

   String varA = "hello"; -> 1

   String varB = "hello"; -> 2

   varA="world" -> 3

   varB ="world" -> 4

At line 1, JVM will create String object "hello" in string pool and assign its reference to varA. At line 2, JVM will assign the reference of "hello" object already present in String pool to varB. At line 3, JVM will create a new object "world" and assign its reference to varA. Now String pool will have two objects , "hello" and "world". At line 4, JVM will assign the reference of "world" object already present in String pool to varB. String pool will still have two objects , "hello" and "world".

So basically, JVM has not modified any String object, it has just modified the reference.

Hope it helps you. If you have any problem please comment.

Saurabh
  • 1,405
  • 1
  • 21
  • 42
1

So String pool is carried out when two String object having same literal???

--> Not exactly, String pool is carried out when you define literal string. If you create literal string object like String obj = "sometext" and While creating second literal like String obj1 = "some Other Text", obj1 contents will be compared to "sometext" by JVM. If both contents are equal like String str1 = "hello" and String str2 = "hello", then object references str1 and str2 will be pointed to same object("hello").

Why second output is false ???

--> because abc and abcd points to different string objects.

Immutable means: once object is created you can not change its contents.

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
1

Java automatically applies String.intern to double quoted literals. Thus it is guaranteed that

String foo1 = "foo";
String foo2 = "foo";
assert foo1 == foo2;

However, for strings constructed other than as literals this is not guaranteed, i.e. given

String foo1 = "foo";
// contrived example, but imagine foo2 came from reading a file or similar
String foo2 = new String(new char[] { 'f', 'o', 'o' });

it is guaranteed that foo1.equals(foo2) but not necessarily that foo1 == foo2. You can call intern explicitly - it is guaranteed that foo1 == foo2.intern() - but it's generally clearer to use equals when you care about value equality and only use == when you really need reference equality.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183