-2

I'm new to Java, and have a question related to the creation of strings.

Case 1:

String a = "hello";
String b = "world";
a = a + b;
System.out.println(a);

Case 2:

String a;
String a = "hello";
a = new String("world");
System.out.println(a);

I would like to know how many objects are created in each case. Because String is immutable so once value is assigned to it that object cannot be reused (that's what I understand currently, please correct me if I'm wrong).

And I would be even more happy if anyone can explain the same with StringBuffer. Thanks.

Perception
  • 79,279
  • 19
  • 185
  • 195
Thiyagarajan
  • 1,271
  • 1
  • 14
  • 19
  • A previous post talk of this http://stackoverflow.com/questions/3297867/difference-between-string-object-and-string-literal – AurA Apr 04 '13 at 03:48
  • You can easily get lots of tutorials and articles on this topic that explain each and every thing very nicely and clearly. Don't ask such stupid silly questions for that you can easily get answer by just one hit on google. Do your homework properly and while doing something with honest efforts, if you have some problems you are welcome to put your queries. I don't have enough reputation score to vote down or close this. Don't expect spoon feeding. – Shailesh Saxena Apr 04 '13 at 04:22
  • This link may help you : http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html – Shailesh Saxena Apr 04 '13 at 04:48

3 Answers3

1
Case 1:

String a = "hello";  --  Creates  new String Object 'hello' and a reference to that obj
String b = "world";  --  Creates  new String Object 'world' and b reference to that obj
a        = a + b;     --  Creates  new String Object 'helloworld' and a reference to   that obj.Object "hello" is eligible for garbage collection at this point

So in total 3 String objects got created.

Case 2:

String a;  -- No string object is created. A String reference is created.
String a = "hello";  -- A String object "hello" is created and a reference to that
a        = new String("world");  -- A String object "world" is created and a reference to that. Object "hello" is eligible for garbage collection at this point


So in total 2 String objects got created
Joe2013
  • 1,007
  • 1
  • 9
  • 24
0

As you rightly mentioned that strings are immutable, the below creates 3 string literal objects

String a = "hello"; --first
String b = "world"; --second
a = a + b;          --third (the first is now no longer referenced and would be garbage collectioned by JVM)

In the second case, only 2 string objects are created

String a;
String a = "hello";
a = new String("world");

Had you used, StringBuffer instead of String in the first place, like

StringBuffer a = new StringBuffer("hello"); --first
String b = "world";                         --second
a.append(b);                                --append to the a
a.append("something more");                 --re-append to the a (after creating a temporary string)

The above would create only 3 objects as the string is internally concatenated to the same object, while using StringBuffer

Akash
  • 4,956
  • 11
  • 42
  • 70
  • I don't know why you would answer a question like this... but good job. Don't expect the OP to upvote or select this answer. He will read `3` and `2`, add them to his homework, and never be heard from again! – jahroy Apr 04 '13 at 04:05
  • Akash, as best of my knowledge JVM is not allowed for garbage collection inside the string pool(SCP area). all the Strings are destroyed only when entire JVM is shutdown. Please let me know if I am wrong. – Shailesh Saxena Apr 04 '13 at 04:18
  • ^^ Agreed. When Strings are created as literals, they are treated differently (and not GCed in this case). Also, in your third example I believe 3 Strings are created: **1.)** "hello" **2.)** "world" **3.)** "something more" – jahroy Apr 04 '13 at 04:22
  • yeah!. here is the link: http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html – Shailesh Saxena Apr 04 '13 at 04:47
  • @ShaileshSaxena right, not sure how I managed to miss that one :p – Akash Apr 04 '13 at 04:51
0

In case 1, 3 objects are created, "hello", "world" and "helloworld"

In case 2, two objects are created in String pool "hello" and "world" . The world object will be created new even if the "World" object is there in the string pool.

Deepak
  • 327
  • 1
  • 7