wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 3415
    
  12
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: 37884
    
  22
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.
 
jQuery in Action, 2nd edition
 
subject: OUTPUT
 
Similar Threads
explain this ...please
System.out.println confusion with the quotation marks and + sign
question about properties and command line arguements
explain this ...please
Uniqueness of objects