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.
Login in to like
Login in to comment