GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes OUTPUT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "OUTPUT" Watch "OUTPUT" New topic
Author

OUTPUT

shruti patel
Ranch Hand

Joined: Mar 10, 2008
Posts: 52


Output is "Not Hello World".. please explain the output

[edit]Add code tags. CR[/edit]
[ August 14, 2008: Message edited by: Campbell Ritchie ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3599
    
  15
Don't use == to compare Strings. Use the String.equals method. Search the forum for the reasons why. It's been explained many times.

Actually having just re-read your post, you may have been wondering why x==y returned true while z==y returned false. If that's the case you need to search the forum (or read the Java language Specification) for an explanation of the string pool.
[ August 14, 2008: Message edited by: Joanne Neal ]

Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Please Use Code Tags when posting code; I have added them so you can see how much easier the code is to read. And also "OUTPUT" hardly tells people what the thread is about. Please read those two FAQs and also this one.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
In 'String x = "Java";', '"Java"' is a compile time constant, thus the String pool gets into action, resulting in x and y atually refering to the same object, not only to Strings that are equal. Therefore, x == y is true.

In 'String z = new String("Java");' in contrast, a new String object is created, which is not the same as the one referenced by x and y. Consequently, x == z is false.
 
GeeCON Prague 2014
 
subject: OUTPUT