• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

operators

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks latha. good and clear explanation.
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic