This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes && and & Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "&& and &" Watch "&& and &" New topic

&& and &


Joined: Dec 18, 2000
Posts: 1
Hi! I am a beginner.Can you tell me the difference of between && and &? thanks!
elavazhagan s

Joined: Dec 12, 2000
Posts: 4

&--- 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.

David Harrigan
Ranch Hand

Joined: Dec 12, 2000
Posts: 52
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...
Donald King

Joined: Dec 08, 2000
Posts: 16
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

Joined: Mar 02, 2000
Posts: 3143
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).]

Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
Grant Crofton
Ranch Hand

Joined: Nov 08, 2000
Posts: 154
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).
Jacquie Barker
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
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).]

Author of Beginning Java Objects, Beginning C# Objects, and Taming the Technology Tidal Wave
raj sekhar
Ranch Hand

Joined: Oct 16, 2001
Posts: 117
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)))) ) {
This gave me an IndexOutOfBoundsException cos the length of 'a' was less than 6.
Just my piece...
I agree. Here's the link:
subject: && and &
It's not a secret anymore!