0
String str = "Hello"+"World";
String str1 = str + "hello";

How many objects are created and how many references are created?

Siva
  • 61
  • 1
  • 9
  • Can you elaborate on what is that one object and 2 references based on the above example? – Siva May 17 '13 at 05:34
  • When I modified the value of str, the value of str1 also got modified. so u mean to say "str1" is a reference. – Siva May 17 '13 at 05:44
  • U mean to str and str1 is a reference and "hello"+"world" is a single object – Siva May 17 '13 at 05:46
  • 1
    None. It won't compile, as `str` is not defined. – cHao May 17 '13 at 05:50
  • @cHao what are the objects and what are the references in the above example? – Siva May 17 '13 at 06:25
  • The objects are "Hello", "World", "hello", while the references are str, str1 and the anonymous references to "Hello", "World" and "hello". – 0xCAFEBABE May 17 '13 at 07:03

1 Answers1

2

String is an immutable object. Whenever you manipulate a String, the JVM creates (at least) a new String and assigns it the new (concatenated) value.

As you did not specify you only care about String objects and references, we need to talk about StringBuffers. StringBuffers are (beside StringBuilders) a class that tries to work around the immutable nature of Strings. We all know, many times we just need to add two or more Strings together.

Imagine this code:

String sentence = "the " + "quick " + "brown " + "fox ";

Often times, when that happens, the Java Compiler will not create these Strings, one at a time adding them together, then forgetting about all intermediary Strings. What happens is that a StringBuffer is created. Then, all single Strings are added by using StringBuffer.append(String), then at the end one String is returned.

What you can say for sure is that 3 String references are created, referencing the inlined (and pooled) Strings "Hello", "World" and "hello". Each reference references a different String. That would have changed if the third word would have been "Hello" as well (uppercase h).

0xCAFEBABE
  • 5,576
  • 5
  • 34
  • 59
  • Last i heard, javac actually concatenates string literals at compile time. ie: `"HelloWorld"` is what you'd see in the .class file, and `"Hello"` and `"World"` wouldn't be in there unless you used those particular strings separately elsewhere. – cHao May 17 '13 at 12:31
  • That sounds like a sensible optimization, however, it might be an implementation detail (i.e. OpenJDK might handle this differently than Sun JDK). – 0xCAFEBABE May 17 '13 at 12:50
  • 1
    [JLS7](http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf), §3.10.5: "A long string literal can always be broken up into shorter pieces and written as a (possibly parenthesized) expression using the string concatenation operator `+` (§15.18.1)." Meaning `"a" + "b"` is actually just a different spelling of `"ab"`; both must refer to the same String object. – cHao May 17 '13 at 13:33
  • Thank you for looking it up and linking it. – 0xCAFEBABE May 17 '13 at 13:44