This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes System.out.Println with operator == 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 "System.out.Println with operator ==" Watch "System.out.Println with operator ==" New topic
Author

System.out.Println with operator ==

Shiva Kumar Shedad
Greenhorn

Joined: Jun 11, 2007
Posts: 10
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

Joined: Jun 07, 2007
Posts: 8
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


SCJP 5.0<br />SCWCD 5.0
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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!!


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]
Shiva Kumar Shedad
Greenhorn

Joined: Jun 11, 2007
Posts: 10
Hey thanks nav kaur, i didnt realize the operator precedence.
Abdul Kader
Ranch Hand

Joined: Apr 11, 2007
Posts: 115
Great Explanation. it very important to understand
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: System.out.Println with operator ==
 
Similar Threads
String Comparison
String intern (); method
Strings
Question Regarding Strings
solve StringBuilder problem