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 :)