Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

A question about booleans

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Given the following code:
----------------------------------------------
boolean a = true;
boolean b= false;
if ( a = b ) System.out.println("true");
else System.out.println("false");
-----------------------------------------------
The output is false, can someone explain how this evaluated.
My understanding is that (a) is tested to be true or false but after the assignment. is this correct?
Thanks in advance.
Alexan
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alexan,
By using a=b, you are assigning the value of b to a, that's why you get the false output. I think you should be using == to test for equality with your booleans.
Cheers
Kem
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi
Given the following code:
----------------------------------------------
boolean a = true;
boolean b= false;
if ( a = b ) System.out.println("true");
else System.out.println("false");
-----------------------------------------------
The output is false, can someone explain how this evaluated.
My understanding is that (a) is tested to be true or false but after the assignment. is this correct?
Thanks in advance.
Alexan


Hi Alexan,
The = is an assignment operation not an evaluation operation. If you want to evaluate two conditions, use the == operation. Please keep in mind that the assignment comes before the variable is evaluated by the if condition.
Hope that makes sense!
Owee
 
Alexan Kahkejian
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kem
Actually I met this questions in one of mock exams, I know about == , but I want to know about this situation, is my given understanding (assignment then test) is correct?
Alexan
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alexan,
what you think is right it is the assignment which is first done and then the if().
And note, this is the only place where you are allowed to use an assignment operator inside an if()condition by the compiler.
If you try to assign an int variable to some value it never returns a boolean for the condition check to decide on, and in java it is always required that what we put inside condition checking stmt has to return a boolean
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you would be interested in seeing the answer straight from the JLS:

At run time, the result of the assignment expression is the value of the variable after the assignment has occurred. JLS 15.26


And of course the assignment expression must be evaluated before the result can be tested.
[ May 12, 2003: Message edited by: Marlene Miller ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic