1

I'm learning about Java and String and I want to ask you how many objects are created by JVM in this specific case:

String str = new String("USA");

Does JVM create one object in heap or 2 objects, one in heap and the other in String pooled area?

Thank you!

elvis
  • 956
  • 9
  • 33
  • 56
  • @Mat Terrible choice of duplicate. Nothing but wrong answers there. – user207421 Mar 21 '20 at 10:51
  • 1
    There is no such thing as a "String pooled area". Java objects are always created on the heap, as this is how "heap memory" is defined for Java; the memory where all objects are stored. The string pool is just referencing some of those objects, just like any Java Collection only contains references to objects. Questions of the "how may objects" kind are pointless, as they ignore the *encapsulation*. E.g. in typical implementations, a `String` object contains a reference to an array objects, so a single string may consist of two objects, but two equal strings may share the same array or not. – Holger Mar 23 '20 at 08:13
  • 1
    @user207421 your comment is now misleading, as it is not obvious that you changed the link – Holger Mar 23 '20 at 13:30

0 Answers0