How many objects are going to be created as a result of the following operation?
"a"+"b"
Explanation
As a matter of fact, all String literals are generating String objects. So, the intuitive answer would be 3 (the 2 String literals "a" and "b" plus the result of their concatenation). However, since the literals in the example are constants whose value is instantly exposed to the compiler - the Java optimizer performs the concatenation in advance, thus creating only one object.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes