• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operator Precedence

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ??
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy,

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
prints:
in 1
in 2
false
in 1 (but no in 2)
false
-----
in 1
in 2
false
in 1
in 2 <-- because of no shortcut
false


Yours,
Bu.
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic