permaculture playing cards
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator Precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operator Precedence" Watch "Operator Precedence" New topic

Operator Precedence

Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
class Opr

static boolean method1(boolean b){
System.err.println("in 1");
return b;
static boolean method2(boolean b){
System.err.println("in 2");
return b;

static boolean method3(boolean b){
System.err.println("in 3");
return b;

public static void main(String[] args)

boolean b = method1(true) && method2(false) & method3(true) ;
// System.err.println(b);



& has more priority than &&
so its has to print
in 2
in 3
in 1

but output is
---------- intepreter ----------
in 1
in 2
in 3

Output completed (0 sec consumed) - Normal Termination

How ??
Sheikh Sadiruddin

Joined: Aug 10, 2007
Posts: 21
Hi Ankith,
Expression evaluation occurs from left to right.
So first method1(true) is called. It returns true.
next method2(false) is called. It returns false.
now this two results are short circuit ANDed. and the result is false.
But finally you are using & operation , this will evaluate both sides of the operand.So method3(true) will also be called.

SCJP 6 (80%) || SCWCD 5 (92%)
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Hi Sheikh Sadiruddin

Please can you ellaborate and explain the above
it will be kinda of you...................

i am a bit confused with the below ......

Thanks in advance...........
[ October 03, 2007: Message edited by: dhwani mathur ]
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274

shortcut: &&
non-shortcut: &

shortcuts for logical AND only can take that shortcut if the left operand is false. Only then the result has to be false as well irrespective of what operands follow.
If you don't use the shortcut, both will be evaluated in any case.

Example in your package
in 1
in 2
in 1 (but no in 2)
in 1
in 2
in 1
in 2 <-- because of no shortcut


all events occur in real time
I agree. Here's the link:
subject: Operator Precedence
It's not a secret anymore!