What will be the output of running class Test? Select 1 correct option (1) false false true false true (2) false true true false true (3) true true true true true (4) true true true false true (5) None of the above Answer :4 Whats the major difference between line 3 and 4
Okay, one thing to keep in mind is that String literal processing is not on the exam. That said, here's how this breaks down:
This line contains only String literals "Hel" and "lo". At compile time, the compiler can put these together to get the String literal "Hello". That String, of course, gets put into the String literal pool.
This line, however, contains the variable 'lo'. The compiler doesn't know what value lo will have when the line is executed so it can put the String literal "Hel" into the String literal table, but it can't do anything with lo. When this line is actually executed, a brand new String is created by concatenating the value of lo to the String literal "Hel". Because a new String is being created, even though their values are equal, their references are not. Therefore, the == operator returns false.
Line 5 goes on to take that newly created String object and resolves it to use a String that's already in the String literal pool. Hence, line 5 returns true.
... Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals.
This is the case in the line that prints "true". Both the "hel" and the "lo" are String literals and hence constants.
Strings computed by concatenation at run time are newly created and therefore distinct.
So if you concatenate the "hel" with variable lo (containing "lo") then you add one constant with one non-constant, one object is newly created and thus they are distinct.
[url=http://en.wikipedia.org/wiki/Minor_characters_from_The_Hitchhiker's_Guide_to_the_Galaxy#Deep_Thought]Deep Thought[/url] when encountered with this problem on a low priority thread outputted: "Almost Tricky."
To be tried by Madhavan - by the way, welcome to the Ranch!