Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String count in the pool with println

I am preparing for the OCA SE 7 exam, and some of these questions are really (!) tricky.

In one of the books Im using I found an error I think, so I would like to confirm the following please...

public static void main(String... args) {
    String autumn = new String("autumn");      // line one
    System.out.println("autumn" == "summer");  // line two
}

After the println method executes, how many String objects are there in the pool?

It is my understanding that: - line one does not add the string to the pool - line two creates "autumn" and "summer" and adds them to the pool So the correct answer in the book is 2.

However, I also think... since Im supposed to be paranoid with the exam questions... that also the string "false" is created and added to the pool... So I think 3 should be the correct answer... or does some other black magic happen like... "true" and "false" are already put into the pool by the JVM by default or something?...

Can someone please confirm?


Edit: after some research I find that it was not fair of me to speak of an 'error' in the book; as a general tip: exam questions are usually formulated in terms of 'the following code'; so they are clearly interested in plain old simple calculation of what the code itself is locally doing. So the scope therefore does not allow inspection of the println(boolean b) implementation or compiler optimizations. Fair enough :)

like image 608
krimat_ Avatar asked Feb 26 '15 20:02

krimat_


People also ask

How do you check pool strings?

The string constant pool is there is create a bit of efficiency for constant strings that can be known at compile-time. Other than that it doesn't matter. You should always compare strings with equals().

How many strings are getting created in the string pool?

The answer is: 2 String objects are created. str and str2 both refer to the same object.

Is string pool part of heap?

As the name suggests, String Pool in java is a pool of Strings stored in Java Heap Memory. We know that String is a special class in java and we can create String objects using a new operator as well as providing values in double-quotes.

What is string pool explain with example?

String pool is an implementation of the String Interring Concept. String Interning is a method that stores only a copy of each distinct string literal. The distinct values are stored in the String pool. String pool is an example of the Flyweight Design Pattern.


Video Answer


1 Answers

It should be 2 strings: "autumn" and "false". The first is created by the first line. The second is created by the second line because the compiler would optimize it to just:

System.out.println(false);

which ends up calling PrintStream#print(boolean):

public void print(boolean b) {
    write(b ? "true" : "false");
}

This is what happens at runtime, i.e. after the code is executed. However, at the level of the constant pool stored in the bytecode, only 1 string constant is created which is "autumn" in the classfile of the class which contains your main method. You can verify this by running:

javap -c -verbose ClassName
like image 130
M A Avatar answered Oct 03 '22 05:10

M A