0

I read a lot about String Literal vs String Object. I read that String literal is stored in a String pool and String object will create an object in the heap. I'm quite confused in an instance variable of a class that is initialized using "".

class A {
    private String aStr = "ASTRING";
}

Will aStr will be added to String pool or will it create an object in the heap?

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Develofer
  • 41
  • 10
  • this will go in pool and `String str=new String("ASTRING");` will go to heap, to put in pool use `str.intern()` will go too pool too and it will refer to `aStr`'s value and **tip** you can google this too. A simple google reslut resulted in this (http://www.java67.com/2014/08/difference-between-string-literal-and-new-String-object-Java.html) – bananas Oct 22 '16 at 04:46
  • so when an instance of A was garbage collected. the "ASTRING" will still stay in the pool? – Develofer Oct 22 '16 at 04:48
  • 1
    @Develofer garbage collection only happens for heap – Gaur93 Oct 22 '16 at 04:50

2 Answers2

2

Whenever new Keyword is used then object is created in heap. Here new Keyword is not used so string object is created in string pool.
For example:

String s1= new String("string object");

In the above example two objects are being created one is string object in string pool since it is in double quotes another is s1 which is created in heap as new keyword is used.

Gaur93
  • 685
  • 7
  • 19
-1

As Gaur93 said its true, but i would like to add some more points. Lets take an example:

String s = "hello";
String s1 = new String("hello");

String objects are basically wrappers around string literals. Unique string objects are pooled to prevent unnecessary object creation, and the JVM decide to pool string literals internally.

When you use a literal, say String str = "hello" the object in the pool is used. If you use String str = new String("hello"); , a new object is created, but the existing string literal may be reused on either the JVM level or bytecode level (at compile time).

You can check this by using .equals() method of java .

Anagh Hegde
  • 327
  • 2
  • 14
  • The JVM *must* pool string literals internally. [JLS #3.10.5](http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.5). – user207421 Oct 22 '16 at 06:32
  • suppose the same string is already present then it will not pool but usees the same, so i said it may pool internally – Anagh Hegde Oct 22 '16 at 06:37
  • That's what 'pool' means. Use the same string. The one from the pool. 'Will not pool but uses the same' is meaningless. In fact the whole thing is meaningless. The same string *is* already present: it was put there by the compiler. – user207421 Oct 22 '16 at 06:45
  • Your edit is no improvement. Leaving the grammar problem aside, the JVM doesn't decide. The decision was made by the JLS over 20 years ago. The JVM just does it. And you still have a 'may' that shouldn't be there; and neither the JVM level nor the bytecode level exists at complle time. – user207421 Oct 22 '16 at 07:26