• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

&& versus &

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
marcoullisp
Asked:
marcoullisp
1 Solution
 
marcoullispAuthor Commented:
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
 
objectsCommented:

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
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
& -> 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
0
 
marcoullispAuthor Commented:
don't really see what I am looking for in any of these links... pity...

-PKM
0
 
wings_gauravCommented:
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

hope this answers your question.

-wings
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
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
 
marcoullispAuthor Commented:
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
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
       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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now