Solved

# && versus &

Posted on 2006-05-01
200 Views
I am looking for Java code that explains the difference between && and &. Specifically that uses a && compound state  that evaluates  to true in such a way that substitution of & for && results in the compound statement evaluating to false... if you could comment out thoroughly I would appreciate that greatly...

Am a novice to Java programming, but trying to learn fast.

-PKM
0
Question by:marcoullisp

Author Comment

I need to be able to actually play around with the code you submit so please please make sure I can just cut and paste it into eclipse and run it so I can learn... i would prefer not to have to clean up code.

-PKM
0

LVL 92

Expert Comment

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

&&        op1 && op2        Returns true  if op1 and op2 are both true; conditionally evaluates op2
&        op1 & op2        Returns true  if op1 and op2 are both boolean and both true; always evaluates op1 and op2; if both operands are numbers, performs bitwise AND operation

Don't think you can find a case where x && b is true, and x & y is false.
0

LVL 23

Expert Comment

& -> Bitwise And Operator.(Used to do bit wise anding opeartion)
&& -> logical AND Operation. (Used to logically anding two are more exprssions).

check out

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

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

Thank You.
0

LVL 23

Expert Comment

0

Author Comment

don't really see what I am looking for in any of these links... pity...

-PKM
0

LVL 5

Expert Comment

will try here

&& checks the whole value -
so - a && b evaluates as a != 0 && b != 0
a & b returns a result after "anding" all bits
101 && 110 return 100

will take the exaple forward setting a = 2(binary 10) and b = 1 binary (01)
a && b evatuate a is true because it is non zero and b is true because it is also not equal to zero
and the result is true
on the other hand a & b return 00 (anding bits) which is false

-wings
0

LVL 23

Expert Comment

I think that a goodexample Wings

And also note that && is use in combining the condition like

if(a==10&&b==20)
//do something.

and now the statements inside the if statement will be executed only if both the conditions (i.e a==10 and b==20)are satisfied.

Ok tell me some thing what kind of example are you looking for so that we can help.

Thank You.
0

Author Comment

I need a small program to understand how this would work... nothing fancy... Wings or Shiv could you do that or would it violate the EE rules?

PKM
0

LVL 23

Accepted Solution

put this code in the main program and see
int a=10;//0000 0000 0000 1010 in binary
int b=23;//0000 0000 0001 0111 in binary
int c = a&b;
System.out.println(c); //will print 2 (0000 0000 0000 0010).

//where as
//        c=a&&b;    //error as && is not valid directly and more on when you use a && operator it returns a Boolean
//not an integer so this can be used to check various conditions.
//Like in case if a==10 and also b==23 do soemthig.

i.e
if(a==10&&b==23)   //this will be compiled as (if (true)) as the conditions staisfy.
//do some thing.
System.out.println(a==10&&b==23);  //will print true.
System.out.println(a==10&&b==24); //will print false.
System.out.println(a==10&b==23);  //will print true as this will be evaluating true & true which is true.
System.out.println(a==10&b==24);  //will print true as this will be evaluating true & false which is false.

Thank you
0

## Featured Post

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.