Академический Документы
Профессиональный Документы
Культура Документы
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Java FAQ
Recent Topics
Register / Login A friendly place for programming This week's giveaway is in the Jobs Discussion forum. greenhorns! We're giving away four copies of Presenting for Geeks
and have Dirk Haun on-line! See this thread for details.
Author
usha varadarajulu Greenhorn Joined: Aug 18, 2007 Posts: 2
(s1==s5) Vs s1==s5
posted 8/18/2007 12:02 PM
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
www.coderanch.com/t/407929/java/java/
1/4
7/16/13
== 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:
www.coderanch.com/t/407929/java/java/
2/4
7/16/13
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 ]
SC JP 5.0 | SC WC D 1.4 Raghavan Muthu Ranch Hand Joined: Apr 20, 2006 Posts: 3327
I like...
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! [C odeBarn] [Java C oncepts-easily] [C orey's articles] [SC JP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java C oding 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
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Similar Threads
www.coderanch.com/t/407929/java/java/ 3/4
7/16/13
string Doubt in Strings == & equals() Doubt with == ( double equals ) method. String and String Object Problem
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 15, 2013 20:44:16 .
www.coderanch.com/t/407929/java/java/
4/4