2

I have been reading Java String object and I had this question -

String x="a";
String y="b";

Does it create two objects in Java?

aioobe
  • 413,195
  • 112
  • 811
  • 826
user3717604
  • 21
  • 1
  • 2

4 Answers4

4

Those two lines of code will not create any objects. String literals such as "a" are put in the string pool and made available upon class loading.

If you do

String x = new String("a");
String y = new String("b");

two objects will be created in runtime.

These questions/answers should cover follow-up questions:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 3
    Class loading time is still technically runtime, inasmuch as the string instances are created dynamically and not allocated statically by the compiler. – biziclop Sep 28 '14 at 09:21
  • 1
    The string literals in the string pool are wrapped by String objects, so the two lines in the question eventually lead to two String Objects being created. – Gonen I Sep 28 '14 at 09:38
  • @user889742, that seems to imply that `x = "a"; y = "a";` would result in `(x == y) == false` which isn't true. – aioobe Sep 28 '14 at 09:43
  • You're contradicting yourself. Now you say that the string pool contains string objects! – aioobe Sep 28 '14 at 09:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62065/discussion-between-user889742-and-aioobe). – Gonen I Sep 28 '14 at 09:58
4

When ever a String is initialized using new operator its new object is created. Like if you do

String s1= new String ("string");

String s2=new String ("string");

String s3=new String ("string");

All of the three will create a separate String object in the heap. Whereas if all the above strings are initialized without new operator then, firstly the string will be checked in string pool for its existence. If required string exist then the new reference will start pointing to the existing string.Otherwise it will create new sting in the pool. For example:

String s1= "string";

String s2="string";

String s3="string1";

In the above example only two string will be created in string pool ("string" and "string1"). Where String s1 and s2 will refer to single object "string" and s3 will refer to another string object "string1".

Ankit
  • 61
  • 3
  • In the above case , String s1= new String ("string"); String s2=new String ("string"); String s3=new String ("string"); Whether : 4 objects will be created- The first object will be created in java permanent heap memory as part of the argument we are passing - "string" . and it will be created in String Literal Pool. Or 3 Objects ? – AnswerDroid Sep 26 '16 at 12:26
1

String with literals gets created in String Pool. whereas String through new operators gets created in Heap Memory.

Advantage of creating String through literals is if that String value is already available in String Pool then you get the same reference where through new operator everytime you create a new object new reference.

In your case you will get same reference. so only object.

Suvasis
  • 1,451
  • 4
  • 24
  • 42
0

String object will be created by each line, unless they already exist in string pool...if they exist in string pool only a reference will be linked to your variable and no new objects will be created.