Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.out.Println with operator ==

 
Shiva Kumar Shedad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class TestOut
{
public static void main(String[] args)
{
String s1 = "shiva";
String s2 = s1;
System.out.println("Hello World! :: "+ s1==s2);
System.out.println("Hello World! :: "+ s1.equals(s2));
}
}

I'm not sure why the "Hello WOrld! :: " of first SOP does not print on console when i run this program?
 
nans nav
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in case of first SOP i.e
System.out.println("Hello World! :: "+ s1==s2);
comparing is made like this :
(("Hello World! :: "+ s1) == s2 which will return you false.
If you keep the s1==s2 in braces you will get the desired result:
System.out.println("Hello World! :: "+ (s1==s2));

output:
Hello World! :: true
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes as nav kaur said, its a silly but important thing which sometimes we tend to forget!!

Hope the answer given by nar kaur helps!!
 
Shiva Kumar Shedad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks nav kaur, i didnt realize the operator precedence.
 
Abdul Kader
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Explanation. it very important to understand
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic