• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

operator's

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class popo
{
public static void main(String[]args)
{
int i=0;int j=0;
boolean t=true;
boolean r;
r=(t&&0<(i+=2));
System.out.println(i);
}
}
My question is that shortcircuit operator see the first operand if it's true, then it does not bother to check the other operand and just throw's the result
In the above question, the output is coming as 2.I would like to know when the && operator has higher precedence than the += operator, then how the result is 2.After evaluatiing and executing the L.H.S side of an &&(shortcircuit operator)the next expression on the side of && should not have executed.I suppose the result should have been 0.Please throw mashal (light) on it.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the short circuit operator "&&" checks if the first operand is false or true. Only if the first operand is false, the second operator will be unchecked.
wingo
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in my question, first part is true then, why is it executing the second expression, Please explain it.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
&& operator cannot be applied to Boolean and integer variables . So, If you try
r = t && 0 - The compiler complains that boolean and int cannot be used. So, Java tries to compile ur code like this
r = (t && (0 < (i+=2)) - It tries to get another boolean operand.
Here (0 < (i+=2)) evaluates to true. Looks like, eventhough the higest precedence is &&, when it comes to the type of primitives, that takes higher precedence than the short circuit operator.
Hope this would help.
Padma
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short circuit operators work as follows:
1. In case of '&&', if the first expression is true, then only the second expression is checked. This is because in an 'AND'logical operation, both expressions have to be true for the result to be true. If the first expression were false, the compiler knows that the result cannot be true. Therefore, it 'shortcircuits' the second expression and does not evaluate it.
2. In case of '| |', ... well I think you now know it.
[This message has been edited by Sanjeev Gupta (edited March 31, 2001).]
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
snajeev,
the definition of && operator is,if the first expression result's true then the second expression will never be executed.
What u have explained is the definition of & operator, even if the first expression is false it goes on to check the second expression.
 
Sanjeev Gupta
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nitin,
Could you please tell me your source of definition of '&&'?
[This message has been edited by Sanjeev Gupta (edited March 31, 2001).]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello nitin,
I think you are getting && and | | mixed up. The && operator checks the left side and if it is false it moves on with out checking the right otherwise (it is true) and it has to check the right side too. On the other hand, the | | operator checks the left side and if it is true it moves on without checking the right side. Here are some examples: (assume i was initialized previously)
THE && operator
<code>
if (false && ((i=5) > 0)) { .... } // will not try the right side
if (true && ((i=5) > 0)) {....} // will try the right side
// because left side is true
</code>
The | | operator:
<code>
if (true | | ((i=5) > 0)) { ....} //skips the right side
if (false | | ((i=5) > 0)) { ...} //will evaluate right side to
// see if it is true.
</code>
I hope this answers your question.
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i am sorry sanjeev,i read that part 2 month's back, so i am really missing little thing's now.U r right, if first is true only then the second part will be checked otherwise no way.I am really not enjoying this part of java.
Thank's for pointing my mistake.
sanjev,is there the parantheses playing any part in resolving this problem , see the given line below.
r=(t&&0 < (i+=2));

Bye
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guys
this question is majorly on operator precedene than short-circuit operator, in the expression (i+=2)portion will be executed, since paranthesis has high priority, next < has higher priority so 0<2 is checked which returns false, finally short circuit operator expr becomes true&&false which will be false.
and one more point is short circuit wont eval the second operand only if the first operand is false.
hope i clarified.
thanks
 
    Bookmark Topic Watch Topic
  • New Topic