• 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

Short-Circuit Evaluation

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per K&M book , Short circuit operators are applied to boolean opernads.

Follwing link is shwows an example where && operator is applied on integer operands?

This example is

from:http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op2.html


class ConditionalDemo1 {
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
if((value1 == 1) && (value2 == 2)) System.out.println("value1 is 1 AND value2 is 2");
if((value1 == 1) || (value2 == 1)) System.out.println("value1 is 1 OR value2 is 1");

}
}

Q1)Why this operator is applied on integer operands?
Q2)Can you tell me working of &,&&,|,|| and also the difference in
each of them? with code?
Q3) Is there any trick to remmber this stuuf
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

In the example you've pased, the && and || operators are applying to boolean expressions.

== is the equality operator; it evaluates the left and right hand operands, returning true if they are equal or false otherwise.

So, the expressions you have listed reduce as follows:

and


with regards to Q2:

& and | are bitwise operators, whereas && and || are boolean operators.

& and | apply at the bit level of the operands; that means that they evaluate every bit of the lhs operand against the corresponding bit in the rhs operand:


will print out


You can easilly check the above results by writing out the binary representation of 3 and 7, then doing bitwise anding and oring between the digits.

Q3: Not really, it's just something you should know.

Jeremy
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"&" and "|" can actually also be applied to boolean arguments, in which case they are equivalent to "&&" and "||" except that they don't do short-circuit evaluation: they always evaluate both arguments.
 
Jeremy Botha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True.

Jeremy
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic