This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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.
 
GeeCON Prague 2014
 
subject: operators