This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OUTPUT

 
shruti patel
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic