This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

(s1==s5) Vs s1==s5  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello usha varadarajulu ,

Welcome to JavaRanch.

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

Have a pleasant stay here
 
usha varadarajulu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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


Bye
 
Villains always have antidotes. They're funny that way. Here's an antidote disquised as a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!