• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Why is it printing true?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks john got it....
[ May 24, 2002: Message edited by: Junaid Khan ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic