• Post Reply Bookmark Topic Watch Topic
  • New Topic

SCJP  RSS feed

 
Anand Ko
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean b = 4==2 && 1 < 4;
first expn evaluates to false & so the second expn won't be evaluated. But Relational operators have higher precedence than the conditional &&?.
Please explain?.
I have read in earlier topics that all the operands are evaluated from left to right except for &&, ||, ?:. Is this criteria comes into picture?
If this is the case, then when the precedence comes into picture?
 
Igor Ko
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
=============================================================
Evaluation go from left to right, but with operator && ||
stopped if we already know result:
false && (not evaluated)
true || (not evaluated)
Precedence and special rules for && ||. Why not ?
=============================================================
It's old idea, the same operators exists in C, Ada, Algol-68.
With such expression you can write something likes:
if( timer != null && timer.getDelay() > 15 )
...
it should works if timer==null and if timer != null.
Without such expresions you have to write:
if( timer != null )
if( timer.getDelay() > 15 )
....
A little more text, but programmers don't like write a lot.
Java got C short operators likes: ++ -- && || += /= <<= ...
[ December 23, 2003: Message edited by: Igor Ko ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!