• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Operators and Precedence

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
&& 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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.

 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
huiying li
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jane Griscti,
I am sorry. I already re-registered but I will use Virtual Machin for this thread.
============================================================
Thank You Huiying Li.
 
Right! We're on it! Let's get to work tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic