aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Trouble with Jqplus Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Trouble with Jqplus Question" Watch "Trouble with Jqplus Question" New topic
Author

Trouble with Jqplus Question

Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
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
Ricardo Cortes
Ranch Hand

Joined: Jan 23, 2002
Posts: 140
* 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 ]

Sun Certified J2EE Architect for the J2EE Platform (Part 1)<br />Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
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.


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
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

Joined: Jan 23, 2002
Posts: 140
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

Joined: Jan 23, 2002
Posts: 140
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.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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 (==).


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Trouble with Jqplus Question