aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operators and Precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operators and Precedence" Watch "Operators and Precedence" New topic
Author

Operators and Precedence

Virtual Machin
Greenhorn

Joined: Feb 28, 2001
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

Joined: Feb 12, 2001
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().

SCJP
Virtual Machin
Greenhorn

Joined: Feb 28, 2001
Posts: 5
Thanks for your reply,
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

Joined: Feb 28, 2001
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

Joined: Aug 30, 2000
Posts: 3141
Virtual Machin,
Please read the JavaRanch Name Policy and re-register with a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
huiying li
Ranch Hand

Joined: Feb 12, 2001
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

Joined: Feb 12, 2001
Posts: 68
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#18498
Procedence help you determine the meaning of an expression.
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

Joined: Feb 28, 2001
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Operators and Precedence