-2
String s1=new String("Java");    /* 1st object created */
String s2="Tech";                /* 2nd Object */
s1+=s2;   

I'm confusing here whether new object created or result stored in the previous object.

How many Objects created

Didix
  • 567
  • 1
  • 7
  • 26
  • 1
    Only 2. If that occurs on the compilation. – msagala25 Dec 27 '16 at 06:16
  • There are many SO questions on this matter. didn't you search before posting? Check these: http://stackoverflow.com/questions/47605/string-concatenation-concat-vs-operator http://stackoverflow.com/questions/3297867/difference-between-string-object-and-string-literal http://stackoverflow.com/questions/26083383/java-string-object-creation – Azodious Dec 27 '16 at 06:25
  • 1
    Strings are immutable. They don't change. You always get a new one. – Dawood ibn Kareem Dec 27 '16 at 06:26
  • 2 objects are created! – amod Dec 27 '16 at 07:18

2 Answers2

0
str1 += str2 

is equivalent to doing the following:

str1 = new StringBuilder().append(str1).append(str2).toString();

Final call to toString will create a new object and the reference will be hold by variable str1 here in 3rd line of code. The earlier object in heap String("Java") will become ready for garbage collection.

Java is not similar to c in case of strings it creates a new object instead of modifying the existing objects. This is cause strings in java are immutable.

-1
String s1=new String("Java");    /* 2 objects created as 'new' is used - s1 (holds reference to new String) and string literal "Java" */
String s2="Tech";    /* 3rd Object - "Tech", s2 just holds reference to it */
s1+=s2; /* 4th Object created, which is concatenation of s1 and s2. s1 holds reference to it.

So total 4 objects created.

  • I would thought `s1` hold a reference so it is not a different instance. Could you provide some source on what you are saying ? – AxelH Dec 27 '16 at 07:03
  • That's exactly what I mentioned - "s1 (holds reference to new String)". You can have any number of references to the same object, but the object is the new String object here, the other one is the string literal "Java". String literals are stored in string pool in Java and if you have another line: String s3 = new String("Java") // then same string literal "Java" object would be used, so only one object would be created - new String. – Nitin Puri Dec 30 '16 at 07:32
  • But you said _2 objects created as 'new' is used_ but I doubt you can say a Literal is an Object since we do the difference between a `String object` and `String literal`. Even if, the Objects created would be `"Java"` and `new String("java")`, `s1` is not an object but a variable holding the reference to the object ;) – AxelH Dec 30 '16 at 08:15
  • I meant if new was not used then only one object would have been created, i.e. the string literal. And yes String literal is also a normal String object, just that JVM caches it in string pool. When you use 'new', it forces allocation of a new object. – Nitin Puri Dec 30 '16 at 08:36