1

Now I know this question is asked a lot of times, and has been answered many times, but still someone new to Java will find the explanation tough to understand. So what I have understood from these question is as follows

String a = "hi";

The above statement first checks whether the string is present in string pool. If not, it adds it in the pool and a reference of it is created in the pool. Basically the object is made in permanent generation area and string pool is used to have a reference of it.

However, with

String a = new String("hello");

In this case, it creates two object. One in permanent generation area, and one in the normal heap memory. The a contains a reference to the heap memory object.

Now my question is whether this concept is right or not. Does string pool is references or a pool of actual strings and whether the concept of permanent generation area here I understood is right or not? If wrong please explain in layman's language. Please don't make it duplicate, as I already know this has been answered a lot of times. None was in layman's language and easy to understand. Are two objects actually made? If yes, then how, and if no, then why? It would be really helpful.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
user3239652
  • 775
  • 2
  • 9
  • 23

3 Answers3

1

Yes, you understand it well. When you do:

String a = new String("hello");

There will be 2 created Strings, one on the pool and one object, not in the pool that contains a copy of the content that's stored in the object from the pool.

You'll have something like that:

    Pool         
  +-------+               
  |"hello" <-------- a 
  |       |     
  +-------+    
Maroun
  • 94,125
  • 30
  • 188
  • 241
1

The effect of what you say is basically correct. The problem with your formulation concerns when things happen. When you write

String a="hi";

or indeed, your Java file has the string literal "hi" anywhere in it, then this string literal is allocated only once: when the class is loaded, when your code starts running. Then the initialization of a just uses the existing String object. But when you have an explicit constructor call as in

String a=new String("hi");

then a new String is created. new means a new string object.

  • yes I do agree,but what happens when using new keyword,will it create two object?if yes one Is definitely in the heap memory,where does the other go ,permanent generation area or there is some area known as string pool – user3239652 Jan 27 '14 at 08:44
  • `new` creates one additional object. The object for "hi" already exists whenever you have a `"hi"` in your code anywhere. Just one, no matter how many times you write the literal string `"hi"` in your code. This object is created when the program starts running. – John Tang Boyland Jan 27 '14 at 08:47
  • if your first statement does no exist,that is we are defining only using new keyword,then what?? – user3239652 Jan 27 '14 at 08:51
  • If the second statement exists, then your program has `"hi"` in it; it's the thing you pass to the String constructor. So since your program has `"hi"` in it, then when your program starts, there will be a String object allocated with contents "hi". Then every time your second statement executes, one additional String object is created. If it's in a loop, this could happen lots of times. – John Tang Boyland Jan 27 '14 at 08:54
0

String a= new String("hello")
one object in string pool is created and one another object is created by new operator in heap area.and a is holding the reference of String pool object and string pool object is holding the reference of heap object. and heap object contains hello.

satya
  • 191
  • 2
  • 7