0

I'm confusing in the following example. I want to know how many objects will be created in memory after running this code. As possible as, please explain me details.

String firstName= "Mr.Z";
String secondName= firstName + "Beyond";
String thirdName= secondName + "Plus";
String newName= new String("Mr.Z");
shmosel
  • 49,289
  • 6
  • 73
  • 138
Zay Ya
  • 195
  • 1
  • 4
  • 15
  • 6.................. – Juned Ahsan Jun 10 '16 at 04:24
  • Why 6? Shouldn't it be 4... 3 in String pool and 1 in the heap... – Codebender Jun 10 '16 at 04:27
  • Could you explain me details how to calculate between Stringliteral and String Object.? – Zay Ya Jun 10 '16 at 04:28
  • @Zay Ya ... please look at this => http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext – Necromancer Jun 10 '16 at 04:31
  • @Codebender aren't `secondName`, `thirdName`, `newName` all created in the heap? – ajb Jun 10 '16 at 04:33
  • Don't forget about the StringBuilder objects created as well. I count 8 Objects. – Michael Markidis Jun 10 '16 at 04:39
  • 1
    @MichaelMarkidis I checked the JLS and it doesn't say anything about a requirement to use `StringBuilder` to evaluate the concatenation. If you've looked at the bytecode and seen `StringBuilder` references, fine, but that means your answer is compiler-dependent, and creating two `StringBuilder` objects for this code seems to be one of the less efficient ways to do things. – ajb Jun 10 '16 at 04:56
  • @ajb That is correct according to the spec, but why do you say that creating a StringBuilder object would seem to be one of the less efficient ways? – Michael Markidis Jun 10 '16 at 05:27
  • ...and then there is a `char[]` and an `ObjectStreamField[]` per `String`-instance. Should these be counted as well? – piet.t Jun 10 '16 at 05:31
  • 1
    @MichaelMarkidis I said creating **two** `StringBuilder` objects would be one of the less efficient ways. When you have multiple concatenations and don't need the intermediate results, using a `StringBuilder` can be more efficient by avoiding the need to create intermediate `String` objects. If you use one `StringBuilder` per `+` operation, there's no such advantage, and you just waste time by creating the `StringBuilder`. – ajb Jun 10 '16 at 13:50
  • @piet.t: the `ObjectStreamField[]` array exists only once in the entire `String` class. But the `char[]` array is an interesting aspect, as then it not only depends on the compiler, but also the actual JRE, as the last constructor may or may not copy the array. And before Java 5, the `StringBuffer` had a copy-on-write sharing with the created `String` instance, whereas recent JREs always copy the array, regardless of whether `StringBuffer` or `StringBuilder` is used… – Holger Nov 21 '16 at 18:46

1 Answers1

2

The below link should clarity all your doubts regarding the concept of String Literal and String Object Creation.

http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html

When you create a String using a String Literal, the object is created on the heap and the reference to that object is stored in the String Constant Pool. Hereafter, any references to that String literal will not create new object but the reference from the String Constant Pool is returned. When you create a String object using a new keyword, the object is created on heap but no references are stored in the String Constant Pool.

In your case: 4 objects shall be created on the heap but only the references of the first three objects will be stored in the String Constant Pool. The object created using the new keyword will not have its reference stored in the String Constant Pool

Community
  • 1
  • 1
  • While there haven't been many changes regarding to String pooling, it's still quite bold to link to an article from 2004. – Kayaman Jun 10 '16 at 05:28
  • Yes, the reason I linked this article is because there haven't been many changes in String Pooling and this one clearly explains the Pooling Concept very well :) – Pooja Dubey Jun 10 '16 at 05:51
  • Well, the proper approach is to vote to close as a duplicate, since this has been asked +1000 times. The explanations are also a lot better than in that article. They also describe the changes that *have* happened in regards to String pooling. – Kayaman Jun 10 '16 at 05:52