Originally posted by Karan Gulati:
String s1 = "java";String s2 = "exam";System.out.println(s1 + " " + s2);
How many String objects are created ?
I'm not 100% on this, but I think the answer is actually 5. Obviously, "java", "exam", and (space) Strings are created - they're String literals.
However, when you evaluate the expression in the println statement, you don't evaluate it all at once. Rather, you first evaluate s1 + " " to get the String "java " (so that's the 4th String) and then you perform the second concatenation to get "java exam" (which is the 5th String).
If I'm mistaken, someone please correct me, but I believe the correct answer is 5.