4

Can any one explain Question 87 and 89 from this link http://www.javatpoint.com/corejava-interview-questions-3

87) How many objects will be created in the following code?

String s1 = "string1";
String s2 = "string1";
String s3 = "string1";

Answer is : only one object

89)How many objects will be created in the following code?

String s = new String("Welcome");  

Answer is : Two objects, one in string constant pool and other in non-pool(heap).

Mayank Pandya
  • 1,593
  • 2
  • 17
  • 40

3 Answers3

7

Although String is a class written in Java it is a kind of special class that has some special relationships with JVM. One of them is string literal (sequence of characters wrapped by quotes). When JVM sees "abc" it does something like the following:

String obj = stringLiteralsCache.get("abc");
if (obj == null) {
    obj = new String("abc");
    stringLiteralsCache.put("abc", obj);
}

So, in your first example the first line causes creation of the new instance but next 2 lines just get the already created instance from cache.

However cache works on literals only. It cannot prevent creation of new instance when you explicitly invoke constructor. So, new String("Welcome") creates 2 objects: one from literal Welcome because it is not in cache yet, second from explicit invocation of String constructor.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 1
    This answer is not correct. The Java *compiler* creates pooled String objects for literals in the .class file, and the JVM *class loader* loads them into memory. There is no `if (pobj == null)` step present whatsoever. And the code segment contains an unexplained circularity. – user207421 Mar 21 '20 at 10:47
1

In the first syntax(String string1= "string1";), just one String object is created for any define String .. = "...."; and one reference variable pointing to it. The object is created in the String constant pool maintained by JVM. In the second case String s = new String("Welcome"); , two String objects are created. Since new is called, one String object is created in the normal memory. Also, the string constant "newstring" will be placed in the String constant pool.

So when we don't have New Keyword, we just have one String object is created in the String constant pool.

more information in : Create New Strings in normal memory and in string pool

Community
  • 1
  • 1
1

The main tenet here is that strings are immutable in Java. That means that once created, they cannot be changed.

This allows a JVM to make optimisations. So a good JVM will optimise (87) to the creation of one object. But a poor one may not. There's nothing compelling a JVM to make such an optimisation. The JLS does not demand this.

So the sensible answer to (87) is probably 1, but no more than 3.

(89) is more difficult. In most cases you'll find one object being created in the global string pool and a second one (due to your using new) being created in normal memory. However, I can see no reason why this couldn't be optimised to just one string with s referring to the global string pool directly.

So the sensible answer to (89) is probably 2, but just might be 1.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    The *Java compiler* will ensure creation of one object, because the JLS *requires* it. There is no question of the JVM doing it at all, let alone a 'good JVM'. – user207421 Mar 21 '20 at 10:48