Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt 1

 
Amitkumar Dhama
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all can any body explain me,
--------------------
Class A
{
public static void main(String [] args)
{
String s = new String("amit");
String s1 = s;
System.out.println(s.equals(s1)+" "+s==s1);
)
)
--------------------
output of this code is false,although both equals() and "==" are returning true and that too only single false
--------------------
System.out.println(s.equals(s1));

System.out.println(s==s1);
--------------------
this onme prints

true
true

thanking in advance
 
Glen Fernandes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Quite simply, it's an operator precedence issue.
Operator == has the lowest precedence in that expression.

What you want instead is:


Otherwise the s.equals(s1)+" "+s is evaluated, yielding a new String with the value "true ". This is then compared to s1 with the == operator, which obviously yields false.

Regards,
Glen

[ April 16, 2005: Message edited by: Glen Fernandes ]
[ April 17, 2005: Message edited by: Glen Fernandes ]
 
Amitkumar Dhama
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot.....
even i was also thinking so but i wasn't sure of it......any ways Thanks 4 ur fast reply
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic