• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is it printing true?

 
Junaid Khan
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

As per my understanding i==j evaluates to true
and as b is false the next thing should be (false= true) which should evaluate to false
and hence print false but it prints true.Can any please let me know where I am going wrong...
 
john sham
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason why it is printing true is that comparison (==) has higher precedence than assignment (=) operator.
As such, b=i==j will be evaluated first as:
i == j which is 10 == 10, which returns true.
The next operation is then
b = true, which is assigning true to b.
The final code is then read as:
if(true)
System.out.println("True");
else
System.out.println("False");
}
So if (true) will print a "True".
 
Junaid Khan
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks john got it....
[ May 24, 2002: Message edited by: Junaid Khan ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind that = is an assignment operator. It does not do a comparison!
boolean a = true;
boolean b = false;
if (b = a) // will evaluate to true!
if (b == a) // will evaluate to false!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic