aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assignment Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assignment Q" Watch "Assignment Q" New topic
Author

Assignment Q

Marc Bautis
Greenhorn

Joined: Sep 17, 2004
Posts: 6
I encountered this question on the JQ Mock Exam Application

What is printed when the following class is executed?
class Test
{
static boolean a;
static boolean b;
static boolean c;
public static void main (String[] args)
{
boolean bool = (a = true) || (b = true) && (c = true);
System.out.print(a + ", " + b + ", " + c);
}
}

The answer is true, false, false.
How is the statement boolean bool = (a = true) || (b = true) && (c = true);

I would think that first a is set to true. (b = true) is never evaluated because of the || statement and the fact that a is true. What I don't understand is why c is equals to false?
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
(c=true) is never evaluated either and has false as its initial value. The statement has all the information it needs to know to determine its truth value at the point (a=true).

The key to parsing this statement is to realize that these operations are evaluated from left to right and that && has a higher precedence (or "binds tighter") than ||. So,

boolean bool = (a = true) || (b = true) && (c = true);

could be written

boolean bool = (a = true) || ((b = true) && (c = true));

without changing the statement's meaning.
Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
I'm confused.

Should'nt we evaluate b and c first because of the precedance. Now since b && c already evaluates to true we wont evaluate a anymore.


SCJP 1.4 <br />SCWCD 1.4 (preparing)<br />SCBCD 1.3 (coming soon)
O Joseph
Ranch Hand

Joined: Jun 27, 2003
Posts: 75
Originally posted by Francis Palattao:
I'm confused.

Should'nt we evaluate b and c first because of the precedance. Now since b && c already evaluates to true we wont evaluate a anymore.


hi fran,
i took a look at the question and would advice that such questions should not be taken seriously. to me it was not properly constructed and its aim is probably distraction and detraction.
as per the operator precedence, you are right.
mercie


XX.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Should'nt we evaluate b and c first because of the precedance. Now since b && c already evaluates to true we won't evaluate a anymore.

Java evaluates operands strictly in the order defined by the associativity of their operators, usually left to right. The point of a shortcut operator is that the programmer can rely on the right-hand operand not being evaluated if the left-hand operand determines the result. The presence of parentheses does not change the left-right associativity of an operator, only the grouping of operands. "x||y" is no different from "x||(y)".

Try the code out and you will get "true, false, false".

By the way, FORTRAN worked differently here, but FORTRAN did not have guaranteed shortcut operators.


Mike Gershman
SCJP 1.4, SCWCD in process
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
"Should'nt we evaluate b and c first because of the precedance."

I much prefer to think in terms of "binding tighter" than "precedence" precisely for the reason illustrated by your confusion. Precedence seems to connote "being first" and "highest in importance".

Of course, "precedence table" sounds better than "binding tighter table".
Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78
boolean bool = (a = true) || (b = true) && (c = true);

Sorry...I am not able to interpret how the statement is executed returing the output true,false and false for a, b and c respectively.


SCJP 1.4<br />" Something is difficult doesn't mean you shouldn't try, it only means you should try harder "
seemapanth Joshi
Ranch Hand

Joined: Nov 28, 2004
Posts: 47
I don't understand too!! and I would appreciate it very much if a little more light was thrown on this topic.

Does what I take away from above discussion (as stated below) correct?

For boolean (shortcircuited) &&, || and ?:

1. Java will look for parentheses and if found work in that line or else
2. Evaluate for ONLY "boolean operators", Left to Right (because of the shortcircuited nature of && and ||) and ignores the precedence.

From whatever I have read so far, I understood that associative evaluation would come into play when all the operators have the same precedence and it cannot be decided which expression to evaluate first.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This is for the previous two posters:

The operator && has a greater precedence (high priority) than the operator ||. So the expression is taken to mean . Now the expression is examined from left to right and the first thing that is evaluated is the assignment . The result of this assignment (because a is a boolean) is true. Now, because operator || is a short cutting operator, nothing more need to be done. We know the result: it is true. The assignments and never got done. So variables b and c retain their default values of false. So when printing out a, b, c you get true (because a = true was executed), false, false.

Is that clearer now?
[ December 01, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Poonam Advani
Ranch Hand

Joined: Aug 25, 2004
Posts: 38
Hi,

I've also read that "( )" have higher precednce over the operator " ||". Doesn't this mean that the statement
((b = true) && (c = true))

should be evaluted first ?


thanks
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Kirti Singh:
Hi,

I've also read that "( )" have higher precednce over the operator " ||". Doesn't this mean that the statement
((b = true) && (c = true))

should be evaluted first ?


thanks


I'll ask a counter question :
What happens in:



or
or even

Do more () on the lefthand side force the lefthand side to be done before the righthand side?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Take a look at this portion of decompiled code (javap -verbose)

I've put // comments on the right to show the execution path.

By the way a,b,c are here local boolean variables set explicitly to false.
[ December 01, 2004: Message edited by: Barry Gaunt ]
Poonam Advani
Ranch Hand

Joined: Aug 25, 2004
Posts: 38
Thanks Barry,




I got that.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
On the other hand :
does alter the order of evaluation. Just like is different from . In these cases it's the operation order that is being altered. Drawing a "tree diagram" can help understanding this. Elementary Computer Science, dear Watson
seemapanth Joshi
Ranch Hand

Joined: Nov 28, 2004
Posts: 47
I am sorry sir, I did not want to know "how" it is printing true, false, false. I perfectly understood the logical evaluation. But I did not understand the "why" part. It hit me like a bullet when you explained the last post "on the other hand .......". My question was more in the line of what Kirti Singh asked.

Guess it takes some real pushing to get me "thinking" .Aha!

By the way I came across the following links for "elementary computer science" and thanks a million to clear it up. Deeply greatful !

http://www.chinalinuxpub.com/doc/oreillybookself/java/javanut/ch13_03.htm
http://www.cs.huji.ac.il/course/2004/introcsp/lecture_slides/pdf/handouts/unit03.pdf
http://www.cs.umd.edu/~clin/MoreJava/Intro/expr-assoc.html
Albert Bustamante
Greenhorn

Joined: Nov 29, 2004
Posts: 7
Thanks for the postings and links .. you are all helping me re-enforce my knowledge.... Yee - haa back on the ranch in the am!
prajkta patil
Ranch Hand

Joined: Nov 13, 2004
Posts: 49
if i use short ckt operator thjen why result is different?


then o/p is
a=true
b=true
c=true
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
if i use short ckt operator thjen why result is different?

boolean bool=(a = true) | (b = true) & (c = true)

then o/p is
a=true
b=true
c=true

| and & are the logical OR and AND operators. They are the opposite of the short circuit operators - they always evaluate both operands.
[ December 04, 2004: Message edited by: Mike Gershman ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assignment Q