Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

operators

 
Sai Ram9
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks latha. good and clear explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic