• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question from the SCJP 1.4 masterexam

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the KandB 1.4 book comes with some testing software.


Can I post a question verbatim from it on here?
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup
 
Michael Raymond Jr.
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
---------
public class Test{
public static void main(String[] args}{
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;

if(b1 & b2 | b2 | b3 & b2) //if1 statement
System.out.print("pig");

if(b1 & b2 | b2 & b3 | b2 |b1) //if2 statement
System.out.println("pen");

}//end main
}//end Test
------------

here's what the answer or reference button provides:

"

The answer is "pen". That's the output. The 'reference button'/tip, says "pig" is not output because it doesn't resolve to true.
\

if(b1 & b2 | b2 | b3 & b2) //if1 statement

So this is more like : (b1 & b2) = false, and next due to order of precednce (b3 & b2), then what??? the statements ends because that was the last of the comparisons within the if statement parentheses, or does it next evaluate OR (|) b2 (the middle b2) as = false as well, which is why it's false?

Basically, does the b2 in the middle get evaluated by itself as an | after the two &s are evaluated? Or (no pun intended), does the evaluation close with the last b3&b2 evaluation?

If the middle b2 is being evaluated lastly, is it looked at like from left to right...for instance ((b3&b2 = false) | b2)=false? Or like a lone ranger, not tied to the pre or post & comparisons? Or, if the middle b2 is evaluated, is it evaluted as b2|b2 (first and second b2's, then b2|b3 (second b2 and last b3)?
[ June 01, 2007: Message edited by: Michael Raymond Jr. ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael

Logical AND (&) has higher precedence than logical OR (|).

So (b1 & b2 | b2 | b3 & b2) becomes (false | b2 | false)
and (b1 & b2 | b2 & b3 | b2 | b1) becomes (false | false | b2 | b1)
 
Michael Raymond Jr.
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anthony O'Dea:
Michael

Logical AND (&) has higher precedence than logical OR (|).

So (b1 & b2 | b2 | b3 & b2) becomes (false | b2 | false)
and (b1 & b2 | b2 & b3 | b2 | b1) becomes (false | false | b2 | b1)



Thanks Anthony. I understand why it's false, but what I want to know is if the b2 in the first IF statement is evaluated after the second (and last) false (b3 & b2)? I was thinking that in that IF () since the last thing to be compared ends up being false, that the expression does not go back and search for more expressions to analyze so it just ends and in this case the whole expression is false.

I guess I could try out my theory by making b2 = true, but that would be soooo much work.


I think you answered my other question regardiong the union of the middle b2 or(|) with the left most and right most variables. The first b1&b2 = false first, then it becomes false|b2, not b2|b2.
 
Anthony O'Dea
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thanks Anthony. I understand why it's false, but what I want to know is if the b2 in the first IF statement is evaluated after the second (and last) false (b3 & b2)? I was thinking that in that IF () since the last thing to be compared ends up being false, that the expression does not go back and search for more expressions to analyze so it just ends and in this case the whole expression is false.



Once the ANDs are evaluated, the ORs are done (left-to-right, I assume). Not sure what you mean by evaluating b2, since its just a boolean value.

Also, since none of the operators are short-circuit operators (&& or ||), the entire expression would be evaluated.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic