0

Actually I am bit confused that how many object created in below "code processing".

String s=new String("A");
s=s+"B";

Actually someone said that here 4 objects will be created but in whole processing but how not understand.

Please anyone can give me detail description also included memory part such string pool etc.

Jens
  • 67,715
  • 15
  • 98
  • 113
ParikshitSinghTomar
  • 417
  • 1
  • 4
  • 28
  • 4
    This looks like a certification question ;) did you pass? – vikingsteve Oct 01 '14 at 12:36
  • I'm guessing `String s = new String` is one, `"A"` is another, `"B"` is one and the concatenation operator `+` will create the fourth – j.con Oct 01 '14 at 12:37
  • Not a direct duplicate since this asks how many `objects` created, which presumably can include `StringBuilder` also as per other's comments below. – vikingsteve Oct 01 '14 at 12:52
  • @vikingsteve - Its like running in a never-ending loop. The StringBuilder is again supported (backed) by a char array and other things. – TheLostMind Oct 01 '14 at 12:56
  • @vikingsteve,Actually I said three objects. But I don't know anything that String s=new String("ABC"); creating two objects. That's was the mistaken point. – ParikshitSinghTomar Oct 01 '14 at 13:14

2 Answers2

6
  • The first String created is literal "A", that is, if not interned prior.
  • The second String is the instance generated by the new keyword.
  • The third one is literal "B", again, if not interned prior.
  • The last one is the concatenation of s and "B".
Mena
  • 47,782
  • 11
  • 87
  • 106
  • Correct. worth noting is that the `new String()` around `"A"` is superfluous. – vikingsteve Oct 01 '14 at 12:38
  • @vikingsteve absolutely. I also agree this looks like a 1Z0-803 question, as you mention in the comment :) – Mena Oct 01 '14 at 12:39
  • There will also be A StringBuilder object created. Total 5 objects – TheLostMind Oct 01 '14 at 12:40
  • @vikingsteve Not necessarily. `"A" == "A"` but `"A" != new String("A")` – Elliott Frisch Oct 01 '14 at 12:41
  • 1
    @ElliottFrisch It is superfluous if you know how to compare `String` equality, through `String.equals`. The only valid usage of `new` with `String`s is when you are converting from an encoding to another in my opinion. – Mena Oct 01 '14 at 12:43
  • @TheLostMind really? Cool, I never knew that. Awesome. – vikingsteve Oct 01 '14 at 12:44
  • @vikingsteve ``? XD – Mena Oct 01 '14 at 12:45
  • 2
    @TheLostMind @Mena sorry, I was genuinely not being sarcastic, I meant that I `did` learn something new today - so thank you, honestly! – vikingsteve Oct 01 '14 at 12:46
  • @vikingsteve no need to apologize, personally I'm actually totally ok with sarcasm (especially when not directed at me ;). OK with non-sarcasm as well though :P – Mena Oct 01 '14 at 12:48
2

You have two String literal Objects, namely "A" and "B". Then you explicitly instantiate a new instance of "A" with new String("A");. Finally, the fourth instance is created when you perform the String concatenation s+"B"

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • The StringBuilder behind s + "B"? – TheLostMind Oct 01 '14 at 12:41
  • @TheLostMind Either a StringBuilder instance, or some other instance `s+"B"` will yield a new instance of `String` (and may well instantiate a new `StringBuilder`). If we counted the character array in each `String` instance we could get even more `Object` instances. – Elliott Frisch Oct 01 '14 at 12:44
  • Probably you are right.. In this case, the array supporting StringBuilder must also count and also the length of String changes things. But 4 is *accessible* number of objects. :P – TheLostMind Oct 01 '14 at 12:53