File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operators" Watch "operators" New topic
Author

operators

Sai Ram9
Greenhorn

Joined: Feb 13, 2001
Posts: 18
what will happen if you compile/run this code?
public class Q10
{
public static void main(String[] args)
{
int i = 10;
int j = 10;
boolean b = false;
if(b = i ==j)
System.out.println("true");
else
System.out.println("false");
}
}
Answer says True... Please explain!
Latha Kalaga
Ranch Hand

Joined: Nov 13, 2000
Posts: 96
Hi! Sai
In the statement
if(b = i ==j)
System.out.println("true");
else
System.out.println("false");

due to precedence of operators '==' has a higher precedence than '=' and so the statement can be read as b = (i==j);
i == j will be true and this gets assigned to the boolean b
hence the answer is 'true'.
Hope this helps.
Latha
Originally posted by Sai Ram9:
what will happen if you compile/run this code?
public class Q10
{
public static void main(String[] args)
{
int i = 10;
int j = 10;
boolean b = false;
if(b = i ==j)
System.out.println("true");
else
System.out.println("false");
}
}
Answer says True... Please explain!

Sai Ram9
Greenhorn

Joined: Feb 13, 2001
Posts: 18
Thanks latha. good and clear explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: operators
 
Similar Threads
Why is it printing true?
boolean confusion
Presidence in an IF statement
SCJP Question
SCJP Question