• 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

Assignment Q

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Joe Borderi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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".
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry,




I got that.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 12
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the postings and links .. you are all helping me re-enforce my knowledge.... Yee - haa back on the ranch in the am!
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic