1

Possible Duplicate:
How many java objects generated by this code? and why?

I am very confused in the following

String s1 = new String("Java");
String s2 = new String("Java");

How many objects are created here?

String s1 = new String("Java");
String s2 = new String("Ruby");

How many objects are created here?

String s1 = new String("Java");
String s2 = "Java";

How many objects are created here?

String s1 = "Java";
String s2 = "Java";

How many objects are created here?

Community
  • 1
  • 1
coderslay
  • 13,960
  • 31
  • 73
  • 121
  • 1
    I'm not sure, but I think it's: 3, 4, 2, 1 – Augusto Jan 24 '13 at 08:59
  • @Augusto What do you think about the first one? – coderslay Jan 24 '13 at 09:00
  • 1
    Coder: your questions made me google this to be sure, and you can find quite a good explanation [here](http://www.xyzws.com/Javafaq/what-is-string-literal-pool/3). You can easily test this if you write some code that checks that the string are the same using the `==` operator. – Augusto Jan 24 '13 at 09:01
  • @Augusto Thanks for that. I was really getting confused by seeing different answers from people. Can you please post the answer here so that it will be helpful for other :) – coderslay Jan 24 '13 at 09:03

2 Answers2

0

Your first part of code:

2 objects are created. If "Java" is not present in String Pool, a string is created and added there.

Your 2nd part of code:

2 objects are created and if neither "Java" nor "Ruby" are present in String Pool, both strings are created and added to it.

Your 3rd part of code:

one object created. and same condition is executed to add to String Pool.

Your 4th part of code:

Only one object created, if "Java" not present in String Pool. else no new object created.

Azodious
  • 13,752
  • 1
  • 36
  • 71
  • 1
    Your answer is wrong, try running some code to prove your values, and you'll see that it's not correct. – Augusto Jan 24 '13 at 09:04
  • what's wrong in answer? i'm not counted strings added to string pool but only the objects on heap. – Azodious Jan 24 '13 at 09:07
  • 1
    @Azodious Pooled String might well be on the heap too (they are on hotspot 1.7+). – assylias Jan 24 '13 at 09:10
  • @assylias: noted. in that case there can't be one definite answer for this questions. will depend on StringPool state. (i.e. is certain string present or not) – Azodious Jan 24 '13 at 09:12
  • @Azodious Whether the pool is on the heap or not makes no difference. What makes a difference is indeed whether "Java" is already in the pool or not. You could rephrased "If "Java" is not present in String Pool, it is added there." into "If "Java" is not present in String Pool, the string is created and added to the pool." to make it clear. – assylias Jan 24 '13 at 09:15
  • 1
    String pool check is only performed when you create String as literal, if you create String using new() operator, a new String object will be created even if String with same content is available in pool. Read more: http://javarevisited.blogspot.com/2012/10/10-java-string-interview-question-answers-top.html#ixzz3JPlzkYcG – KNU Nov 18 '14 at 10:28
  • @KNU The string pool is implemented by the compiler and class loader. It doesn't have anything to do with execution of code. The Internet blog says you cited is wrong. – user207421 Mar 06 '16 at 08:38
-1
String s1 = new String("Java");
String s2 = new String("Java");

It will create 2 objects in heap.

String s1 = new String("Java");
String s2 = new String("Ruby");

This will create 2 objects in heap.

String s1 = new String("Java");
String s2 = "Java";

This will create one object in heap for first line. For second line, it will check whether "Java" exists in string pool or not.If exists, it wont create a new string.It will return the reference to already existing string in pool

String s1 = "Java";
String s2 = "Java";

First line will check whether "Java" exists in string pool or not.If exists, it wont create a new string.It will return the reference to already existing string in pool. Second line will get the reference to already created string.So no new objects if "Java" exists or maximum one object

user207421
  • 305,947
  • 44
  • 307
  • 483
Renjith
  • 3,274
  • 19
  • 39
  • The first line mentioned in the final paragraph checks nothing. The literal *is* in the string pool, placed there by the compiler and class loader. – user207421 Mar 06 '16 at 08:40