This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes (s1==s5) Vs s1==s5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "(s1==s5) Vs s1==s5" Watch "(s1==s5) Vs s1==s5" New topic
Author

(s1==s5) Vs s1==s5

usha varadarajulu
Greenhorn

Joined: Aug 18, 2007
Posts: 2
Hi,

public class EqualsTest{

public static void main(String[] args){
String s1 = "abc";
String s2 = s1;
String s5 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
System.out.println("== comparison : " +(s1 == s5));
System.out.println("== comparison : " +(s1 == s2));
System.out.println("Using equals method : " +s1.equals(s2));
System.out.println("== comparison : " +s3 == s4);
System.out.println("Using equals method : " +s3.equals(s4));
}
}

//output
== comparison : true
== comparison : true
Using equals method : true
false
Using equals method : true
Finished executing

public class EqualsTest{

public static void main(String[] args){
String s1 = "abc";
String s2 = s1;
String s5 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
//if we remove the brackets around "s1 == s5' it gives a different result.
System.out.println("== comparison : " +s1 == s5);
System.out.println("== comparison : " +(s1 == s2));
System.out.println("Using equals method : " +s1.equals(s2));
System.out.println("== comparison : " +s3 == s4);
System.out.println("Using equals method : " +s3.equals(s4));
}
}
//output
false
== comparison : true
Using equals method : true
false
Using equals method : true
Finished executing

in the above output why the s.o.p message is not printed and why it returns false?



Thanks
usha
Collins Mbianda
Ranch Hand

Joined: Aug 11, 2007
Posts: 259
Hi!!!

The problem is not (s1==s5) Vs s1==s5.
It's a problem of Operator Precedence.

When you write:
System.out.println("== comparison : " +s3 == s4);

it's equivalent to:
System.out.println(("== comparison : " +s3) == s4);

First ("== comparison : " +s3) is evaluated. Then the result is compared to s4. And because the two are not equal using the "==" operation the result is false.

This is because the precedence + is higher.
Operator Precedence
[ August 18, 2007: Message edited by: Collins Mbianda ]

SCJP 5.0 | SCWCD 1.4
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hello usha varadarajulu ,

Welcome to JavaRanch.

Hope you got the answer for your query by Collins Mbianda.

Have a pleasant stay here


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]
usha varadarajulu
Greenhorn

Joined: Aug 18, 2007
Posts: 2
Hi,

Thanks a lot for your clear reply, i was able to make it out.


Bye
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: (s1==s5) Vs s1==s5