• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals ?????

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Consider the following code.....
String str1 = "one";
String str2 = "two";
System.out.println( str1.equals(str1=str2) ); // here

System.out.println( "str1 : " + str1 + " satr2 : " + str2 );
I thought the output would be "true" but it is false.......
But the second print statment show that the value of 2 strings are same....
Can anyone explain.
Thanks,
Aruna




[This message has been edited by Aru (edited September 07, 2000).]
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do
str1 = str2;
System.out.println( str1.equals(str1) );
or
System.out.println( str1.equals(str2) );
or
System.out.println( str1.equals( (str1=str2) ) );
rather than:
System.out.println( str1.equals(str1=str2) );
it will work.
The third alternative indicates that this may be a problem related to the order of evaluation of an expression. Looks like in your original code Java took str2 before it assigned it to str1, and compared its string with str1 before the assignment!
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. It looks like its due to precedence order.
System.out.println( (str1 = str2).equals(str1));
prints true
 
Aru
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone answer this........?
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,
What is causing confusion here? The equals blindly evaluates left hand operand first. I don't think the assignment = operator is taking precedence over the equals method.
The following program might help us understand this better.

Hope this helps!
Ajith
 
reply
    Bookmark Topic Watch Topic
  • New Topic