File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System.out.Println with operator ==" Watch "System.out.Println with operator ==" New topic

System.out.Println with operator ==

Shiva Kumar Shedad

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

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));

Hello World! :: true

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

Joined: Apr 20, 2006
Posts: 3381

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

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
I agree. Here's the link:
subject: System.out.Println with operator ==
jQuery in Action, 3rd edition