• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble with Jqplus Question

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jq-plus Question 955386656992
boolean b1 = false;
int i1 = 2;
int i2 = 3;
if (b1 = i1 == i2)
{
System.out.println("true");
} else
{
System.out.println("false");
}

It will print true.
It will print false.
Correct ans given is false. I disagree because of precedence of operations. The two ints are unequal, which equal the boolean(false) which evaluates to true no? Thanks
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
* Assignment operators associate from right to left.
* Equality operators associate from left to right.
* Equality operators have greater precedence than assignment operators.
Thus, evaluate the right side first and then perform the assignment. So, i1 does not equal i2 and b1 is set to false. Hope that helps.

Originally posted by Mike Kelly:
Jq-plus Question 955386656992
boolean b1 = false;
int i1 = 2;
int i2 = 3;
if (b1 = i1 == i2)
{
System.out.println("true");
} else
{
System.out.println("false");
}

It will print true.
It will print false.
Correct ans given is false. I disagree because of precedence of operations. The two ints are unequal, which equal the boolean(false) which evaluates to true no? Thanks


[ May 13, 2002: Message edited by: Ricardo Cortes ]
 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With the code written the way you have it you end up with:

and the condition is false. Note that you are doing an assignment to b1 not comparing the value of b1 to the result of i1 == i2.
 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't the false evaluation of the two ints, equal the false boolean flag? Isn't it TRUE that false equals false?
 
Ricardo Cortes
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i1 == i2 is a boolean expression that returns the boolean value false. Then, a boolean variable b1 has it's value set to false. Then, another boolean expression is evaluated. This moves execution to the else statement and the value "false" is printed. Does that help?
[ May 13, 2002: Message edited by: Ricardo Cortes ]
 
Ricardo Cortes
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe this will help even more. This is the order of the steps executed at runtime:
1. Check to see if i1 is equal to i2. Return true if this is the case. Otherwise, return false. The latter (boolean value of false) is returned.
2. Set the value of the boolean variable b1 to the return value of the equality operation in step 1. So, b1 gets the boolean value of false.
3. Now, a new boolean expression is calculated as follows:
if (b1)
else
But b1 is false, so the if clause is skipped and execution continues inside the else statement.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Kelly:
Doesn't the false evaluation of the two ints, equal the false boolean flag? Isn't it TRUE that false equals false?


You are looking at it the wrong way. The first operation is an assignment, not a comparison. b1 is being assigned (=) the result of the comparison (==).
 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it guys, thanks alot. Seems I had a previous exam question that led me down that path. The Moose Saloon is a great resource for people trying to learn this very complicated subject. Very much appreciated.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic