• 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

Short circuit example

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am making my way through the SCJP for Java 5 book.
The book says alot of people have problems with chapter 4 operators.
That chapter seemed very easy, the assignment chapter with the widening, autoboxing, and some of the other chapters seemed more difficult. Alot of minor details and stuff to remember.

Anyway, I have a specific question.
Page 321 says

assume doStuff() allways returns true. we have:

int y =5;
int x = 2;
if ((x > 3) && (y < 2) | doStuff())
System.out.println("true");

This prints nothing because of short circuit "and".
However, I thought "and" is allways higher precedence
than "or" so that the compiler would evaluate
"(x > 3) && (y < 2)" as if there was parenthesis
around that part of the expression. Since it's short circuit,
I would expect that "(y < 2)" would not be evaluated, but
I am really surprised that the rest of the expression would be
dropped as well, unless this is a typo ?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ur question isnt clear..but from what i infer, i feel the answer should be "true" regardless of operator precedece, coz x>2 shud return true ..then the whole test returns true if u take the x and y tests in parenthesis.

if u take the y and dostuff() test in parenthesis and give precedence to that, then as dostuff() is true, the test returns true.

correct me if i got something wrong.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Logical OR without short-circuiting has higher precedence than AND with shrot-cicruiting, and since logical operations are evaluated from left to right, this is how it goes:

Left side of && will be: (x > 3)
Right side of && will be: (y < 2) | doStuff()

(x > 3) && ((y < 2) | doStuff())
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aleksander answer looks fine, thought this might be useful

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(x > 3) && ((y < 2) | doStuff())

Just to make things more clear, only (x > 3) will be evaluated, and then short circuit takes place. These problems can be very tricky.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think if we evaluate this code given the values of x and y , answer should be false and not true.



So I guess this is adding to more confusion , anyone who knows how to do this?

Thanks.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// checking wheter a short circuit opeartion works or not

public class ShortCircuit
{
public static void main ( String args [] )
{
int x = 3 ;
int y = 5 ;

if ( ( x > 3 ) && ( y > 2 ) | true )
{
System.out.println ( " The value is true " ) ;
}

else
{
System.out.println ( " The value is false " ) ;
}
}
}


I ran this Code , it printed flase

The reason could be ( 0 and 1 or 1 is 0 ) , so no matter wheter short circuit evluation occured or not , it would print false
 
Larry Guild
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alexander said that logical OR without short circuit has higher precedence than short circuit AND. That is the key point, otherwise it would return false. However, I don't understand the rationale as to why
logical OR should have higher precedance. (1 and 0 or 1) would normaly be true because the compiler would in effect do ((1 and 0) or 1) because
of the higher precedance of AND. We studied short circuit evaluation in college along time ago, but this kind of mixing of short circuit and non short circuit within an expression I have no recolection of haveing ever covered, and I'm not sure I'd ever actually use something that way anyway.


I get the impression that alot of stuff on the test is trick questions and sort of odd ball questions. I was amazed that there is an example of the default clause in a case statement being in the middle of the case instead of the end. Technically it's legal, but I've never seen it that way.
I'm almost wondering if I should try to obtain a java syntax diagram (BNF form) and study that. You have to memorize all kinds of details that you might normaly just look up in a book. The test is motivating me to learn and gives me something to work twards I have to admit.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Bhavesh.. Thanks for the example let me add my point on my understanding: On ur code
// checking wheter a short circuit opeartion works or not




when the compiler checks for the line:

it would first evaluate (x>3) and since the ouput is false it would check its an and operator associated to it and therefore it doesnt check for the right hand side check. It would invariably end up to else block and prints:
"The value is false "

-- Balaji.S
 
Larry Guild
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You say this is false:

if ( ( x > 3 ) && ( y > 2 ) | true )

but this would be true, correct ? I just changed
the Or to short circuit or:

if ( ( x > 3 ) && ( y > 2 ) || true )

This also I would expect to be true,
with no short circuits:

if ( ( x > 3 ) & ( y > 2 ) | true )

Mixing short circuit with non short circuit within an expression,
seems confusing to me, but hey I guess you learn something every day.
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Larry Guild:
I don't understand the rationale as to why logical OR should have higher precedance.



Yes, it can be a little confusing, but just for the first time Operators without short circuiting have higer precedence than operators with short circuiting. This is how it goes:

& - AND without short circuiting has highest precedence
| - OR without short circuiting
&& - AND with short circuiting
|| - OR with short circuiting has lowest precendece
[ January 26, 2006: Message edited by: Aleksander Zielinski ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"rshad,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc
 
Larry Guild
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alexander,

Thanks, I definitely get it that there's precedence going on here, I still don't understand the rationale or the "why" as to why it is that
way. That's just academic, if your in a class room setting the teacher will often explain to you why something is a ertain way with examples or whatever.

At any rate, I don't think I'd get into trouble with it in the real world as I can't think of any reason why I would mix it up like that, except by accident of course, and if that ever happens, I'll now know what to look for.
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic