0

How many objects are created when below statements are executed?

 String s1 = "abc";
 String s2 = "abc";
 String s3 = new String("abc");
 String s4 = new String("abc");

Given above java question - I think answer should be 3. First object is created for line# 1, assuming String "abc" didn't existed before. For 2nd line no extra object is created as String literals are interned. For 3rd and 4th statments 2 more objects are created. Hence in total 3 objects are created. This logic is inline with java specs String literal

A string literal is a reference to an instance of class String (§4.3.1, §4.3.3). Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.

What is confusing is java tutorial about String in String

Whenever it encounters a string literal in your code, the compiler creates a String object with its value—in this case, Hello world!.

The use of "whenever" is confusing here. So the real question what can we do to ask people to edit the tutorial to correct it. Otherwise it will confuse lot of people, after all this site is referenced by even experienced professionals.

nanosoft
  • 2,913
  • 4
  • 41
  • 61
  • Well if you see my question specifically says about conflicting Java docs which everyone refers. I know the answer which I mentioned as well... To m e the main question is how can we reach tutorial guyz and ask for correction.. Edited my post.. – nanosoft Jun 16 '15 at 11:02

5 Answers5

3

The Java Tutorial you mention is wrong as it fails to take String interning into account.

You're correct that it would be three objects

Phil Anderson
  • 3,146
  • 13
  • 24
0

You are correct about the creation of objects , it would create three objects!!!

Abhijit Bashetti
  • 8,518
  • 7
  • 35
  • 47
0

It would create 3 objects

  1. Objects# 1:

    String s1 = "abc";
    
    String s2 = "abc";
    
  2. Objects #2:

    String s3 = new String("abc");
    
  3. Object #3

    String s4 = new String("abc");
    
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
0

Thanks all for your answers. I just reported the inconsistency in documents( and hence possible error) to javasedocs_us@oracle.com

Hopefully they will respond and rectification would be done.

nanosoft
  • 2,913
  • 4
  • 41
  • 61
0

Read the second answer in this post by roger_that. It will clear everything.

String is immutable. What exactly is the meaning?

By the way, it will create 3 objects for String value. One "abc" in constant string pool, both s1 and s2 will refer same value. Two different "abc" value outside of string pool, s3 and s4 will refer to them each.

 String s1 = "abc";
 String s2 = "abc";
 String s3 = new String("abc");
 String s4 = new String("abc");

Regarding your confusion:

What is confusing is java tutorial about String in String

Whenever it encounters a string literal in your code, the compiler creates a String object with its value—in this case, Hello world!.

There is nothing wrong with above statement. It will create 4 objects of String class i.e. s1, s2, s3, s4 (above line is talking about these objects)

Here, both s1 and s2 will refer to the same value "abc", present in string pool while s3 and s4 will refer to different "abc" values, outside of string pool.

See the pictures in these two article for more clarification.

http://www.journaldev.com/797/what-is-java-string-pool

http://www.journaldev.com/4098/java-heap-memory-vs-stack-memory-difference

Community
  • 1
  • 1
kingAm
  • 1,755
  • 1
  • 13
  • 23
  • You said - s1 and s2 will refer to the same value "abc". Its just not the same value but same object. s1 and s2 are just references which point to same object. So in this case just one object is created. and other two objects for one for each s3 and s4. So in total just 3 objects. – nanosoft Jun 16 '15 at 14:39
  • Yes, you are correct. Don't get confused with objects of string value and reference objects. Read those links which have provided above. – kingAm Jun 16 '15 at 15:32