wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String literals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String literals" Watch "String literals" New topic
Author

String literals

Madhavan Santhanam
Greenhorn

Joined: Sep 24, 2007
Posts: 21


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
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy cowboys,

the example comes right from the Java Language Specification.

Here is an abridged version (due to the demand of isolating the problem):

output as indicated.

JLS says in 3.10.5 String Literals:
...
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!



-

What will happen if you make variable lo final?


Yours,
Bu.
[ September 25, 2007: Message edited by: Burkhard Hassel ]

all events occur in real time
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344


..What will happen if you make variable lo final?


That is a very good twist Bu Just a modifier changes the whole scenario. As such a final variable is treated as a compile time constant, it will also be true.

Good question you have asked which make us think!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String literals
 
Similar Threads
String a-go-go
inner class doubt
Strings
String questions
doubt in Strings