Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

&& and &

 
zhengzhili
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I am a beginner.Can you tell me the difference of between && and &? thanks!
 
elavazhagan s
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

&--- It is the bitwise AND operator as well as boolean AND operator i.e, which checks both condition true or false.
&&--It is the boolean AND Operator. It acts as a Shortcircuit AND operator i.e, which checks only first condition whether true
or false.
regards,
elavazhagan
 
David Harrigan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this:-

In the first case, the JVM would need to check the condition of *both* a *and* b in order for it to evaluate the expression. However, in the second case, it looks at a, determines it's true, but in an AND condition, *both* sides of the expression need to be true for the whole thing to evaluate to true, so it checks b as well...
However, if a was false then it would stop checking at a because it doesn't matter what the value of 'b' is as the overall result will *always* be false (i.e.,
if (false && true) = false
if (false && false) = false
if (true && false) = false
if (true && true) = true
The && is a short-circuit. It allows the JVM to continue on without processing the other side of the expression since it knows what the first part of the expression is...
The best way is to write some code. Try all permutations and convince yourself of the result...
David.
 
Donald King
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Part of your confusion with & and && may have to do with the context in which they are used. As boolean operators, they both perform comparisons between two boolean expressions such as:
int x = 5 ;
int y = 6 ;
if ((x == 5) & (y == 6))
{
System.out.println("True") ;
}
if ((x == 5) && (y == 6))
{
System.out.println("True") ;
}
In examples of this sort, the results will be the same regardless of the operator, so it may not be clear why you would ever use & over &&.
As described in the previous responses, the & operator will cause both sides to be evaluated while the && will stop if the left side is false. Consider a change in the above example:
int x = 5 ;
int y = 6 ;
if ((++x == 5) & (++y == 6))
{
System.out.println("True") ;
}
In this example, the left half of the expression (++x == 5) will be false since x is being incremented to 6 before the comparison. Using &, since both halves of the expression are evaluated, both x and y are incremented. Their new values after the if loop will be 6 and 7 respectively.
If that code had used &&, the comparison would have stopped with a false reading of the left hand expression and y would not have been incremented. The x and y values would be 6 and 6 respectively.
On another note, & is also a bitwise operator that compares the individual bits of integers. To use an example from "Java Unleashed":
int x = 5 ;
int y = 6 ;
int z = x & y ;
The variable z will equal 4 in this example. The binary representation of 5 is 0101 and 6 is 0110. The & operator will compare each bit between 5 and 6 and put a 1 where they are both 1 and a 0 elsewhere. The result is 0100, which is 4.
I hope this helps.
- Don
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
zhengzhili & elavazhagan s your names do not comply to the javaranch naming rules outlined here. Please register again with valid names.
Thank you!

[This message has been edited by Angela Poynton (edited December 19, 2000).]
 
Grant Crofton
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you'd normally only use & when comparing bits.
one example is to get a certain byte from an integer, like extracting the 'blue' byte from an argb (alpha red green blue) int, like

If you don't want to be playing with hex & binary you shouldn't worry about it (just yet).
Grant.
 
Jacquie Barker
author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "shortcircuit" form of "Boolean and" (&&) comes in handy when evaluating all of the arguments in an "if" statement could potentially throw an exception. Consider the
following example:
// Declaration.
Book b;
// details omitted
if (b != null && b.getTitle().equals("Beginning Java Objects")) {
// details omitted
}
In the above example, if reference variable "b" happens to
never have been initialized to refer to a Book object, b will be equal to null. Using the && operator, we'll discover that b IS null, and will avoid evaluating the second half of the expression, which would throw a NullPointerException when we try to invoke the getTitle() method on the non-existent b object. Only if the first clause is true -- namely, b is NOT null -- will the second clause be executed.
On the other hand, had we written:
if (b != null & b.getTitle().equals("Beginning Java Objects")) {
then BOTH clauses would be evaluated regardless of the value of the first clause, and a NullPointerException would be thrown.
Hope this helps!

[This message has been edited by Jacquie Barker (edited December 20, 2000).]
 
raj sekhar
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well said Jacquie
Here's another case that I ran into sometime ago without much realisation and then found the error which was of course stupid on my part....
While playing with strings which is more of a routine
if ( (a.length() >=6) &
( !(isAlpha(a.substring(0,6)))) ) {
//something
}
This gave me an IndexOutOfBoundsException cos the length of 'a' was less than 6.
Just my piece...
Thanks
Raj
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic