• Post Reply Bookmark Topic Watch Topic
  • New Topic

&& evaluation  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it in the JVM specs clear that it will be evaluated from left to right?

does the compiler have the right to shift the eval. order internally?
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Welcome to JavaRanch!
The operands to "&&" will always be evaluated left to right. Actually, it's a "short circuit" operator, meaning the left operand will be evaluated, and then the right one if and only if the left one was true. This is actually exactly how "&&" works in C.
But Java has much stricter and less ambiguous evaluation rules than does C in many other places -- for instance, all function arguments are evaluated left-to-right, guaranteed.
[Edited in agreement with criticism below -- thanks.]
[ December 23, 2003: Message edited by: Ernest Friedman-Hill ]
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The operands to "&&" will always be evaluated left to right. Actually, it's a "short circuit" operator, meaning the left operand will be evaluated, and then the right one if and only if the left one was false.

Don't you mean the right one will be evaluated only if the left one evaluates to true?
Angel
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!