Win a copy of Penetration Testing Basics this week in the Security forum!

# Operators and Precedence

Virtual Machin
Greenhorn
Posts: 5
What will be output of following code...
<code>
class test {
public static void main(String[] s){
test t = new test();
t.methodA();
}
void methodA() {
if (aa() && !bb()) {
}
}
boolean aa() {
System.out.println("In aa()");
return false;
}
boolean bb() {
System.out.println("In bb()");
return false;
}
};
</code>
I thought it will be In bb() because of precedence of ! is higher then &&.
Pls explain ...

huiying li
Ranch Hand
Posts: 68
&& is a short-circuit operator,
after aa() is evaluated to false, the whole logical expression will evaluate to false, and bb() is not evaluated.
! has higher precedence than && means
( aa() && !bb() )
is equivalent to
( ( aa() ) && ( !bb() ) )
it does not mean bb() is evaluated before aa().

Virtual Machin
Greenhorn
Posts: 5
I might be wrong but I was thinking precedence in above case will be (aa() && (!bb())) and inner brackets will evalute first then outer brackets.

Virtual Machin
Greenhorn
Posts: 5
<code>
class test {
public static void main(String[] s){
test t = new test();
t.methodA();
}
void methodA() {
System.out.println(bb() | !aa() & aa());

}
boolean aa() {
System.out.println("In aa()");
return false;
}
boolean bb() {
System.out.println("In bb()");
return true;
}
};
</code>
Output is

---------

In bb()
In aa()
In aa()
true
Normal Termination
Output completed (0 sec consumed).

Can I safely conclude that method calls are based on associavity of left to write and boolean expression is evaluated based on pure arithmathic precedence.

Jane Griscti
Ranch Hand
Posts: 3141
Virtual Machin,
Please read the JavaRanch Name Policy and re-register with a name that complies with the rules.
------------------
Jane Griscti
Sun Certified Programmer for the Javaï¿½ 2 Platform

huiying li
Ranch Hand
Posts: 68
Hi,
| & are not short circuit operators. You really need to understand the short circuit concept. Also Nitin has helpfully posted your question on Marcus Green's site also and here is a reply from Gurpreet there, that should be helpful to you.
---
Nitin also try all these to see the difference between following:
//.................(1)
int x=0;
if(x==1 && x++==0)
{
System.out.println("x="+x);//Doesn't print any thing.
}
//.................(2)
int x=0;
if(x==1 && ++x==0)
{
System.out.println("x="+x);//Doesn't print any thing.
}
//.................(3)
int x=0;
if(x==0 && ++x==0)
{
System.out.println("x="+x);//Prints 1
}
So,the conclusion is operator precedence affect execution only when it is required for evaluation.
Regards
Gurpreet

huiying li
Ranch Hand
Posts: 68
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#18498
2 + 3 * 4 = 2 + ( 3 * 4 ) = 14
it does not mean
2 + 3 * 4 = ( 2 + 3 ) * 4 = 20
because * has higher procedence than +. However 2 is still evaluated before 3 * 4 because arithematic expressions evaluate left to right.
Short circuit logical operators ( &&, | | ) also evaluate left to right, if the result can be determined from the left operand, the right operand is not evaluated.
Here is a program that proves my point.

C:\JavaRanch>java Proc1
in f()
in g()
in z()
14
logical operations evaluate left to right, here is the left hand side
i = 2 the right operand is not evaluated
logical operations evaluate left to right, here is the left hand side
i = 2 the right operand is not evaluated

[This message has been edited by huiying li (edited March 09, 2001).]

Virtual Machin
Greenhorn
Posts: 5
Hi Jane Griscti,
I am sorry. I already re-registered but I will use Virtual Machin for this thread.
============================================================
Thank You Huiying Li.