Both & and && are the boolean and operators. The difference between the two is that && is the short-circuit operator. If the first test is false, java will not evaluate the second if( false & true ) // in this, both will be evaluated. if( false && true ) // only the false will be evaluated. This has particular meaning if you try to set something in the second expression. int i = 0; if( false & (i++) == 2) // i will now = 1 because both where evaluated. int i = 0; if( false && (i++) == 2) //i will still be 0 because the second expression was not evaluated. ------------------ Hope This Helps Carl Trusiak, SCJP2 [This message has been edited by Carl Trusiak (edited June 25, 2001).]

hi! nancy, The logical operator && produce a boolean value of true or false based on the logical relationship of its arguments. And you can apply it toboolean values only. You can�t use a non- boolean as if it were a boolean in a logical expression as you can in C and C++. One more interesting thing with logical operator is the phenomenon called �short circuiting.� And in the case of && operator the expression will be evaluated only till the truth of the expression can be determined. For eg. in the expression below if(test1(0) && test2(2) && test3(2)) first the test1(0) is evaluated and only if it is true then the next test2(2) will be evaluated and so. And & is Bitwise operator. The bitwise operators allow you to manipulate individual bits in an integral primitive data type. The bitwise AND operator (&) produces a one in the output bit if both input bits are one; otherwise it produces a zero. and there are lots of other things about & and && but i hope that the explanation above would help a bit. regds prateek

hi, && is the short circuit operator. if first expression is false, then second is'nt evaluated. usefull for null pointer testing in if clauses: String s = null; ... ... ... some code here if (s != null && s.equals("foo")) { } karl

Originally posted by Mikael Jonasson: If I'm not misstaken, thoose are the same as in C++, thus & is a bit-operator, that is 8&4=12 or as it is in bits: 1000 0100 ---- 1100 /Mike

Mike The boolean & will produce a 1 bit i the result if both operands have a 1 bit in the same location. Like this 00010100 20 00011011 25 & ------------- 00010000 16 the example you gave is for the boolean | (or) where a bit is set if either of the operands have the 1 bit set. Just wanted to avoid confusion Dave

Nancy Please change your name to be compliant with JavaRanch's naming policy. Your ID should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name. Thanks, Cindy

"JavaRanch, where the deer and the Certified play" - David O'Meara

Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158

posted

0

That is correct, sorry. Well to sumarize the above, and to answer the original question: && is a short-circuit operator used tow join two boolean expressions. Not that if the left side of this "join" fails, the right side is never evaluated. & is a bitwise operator, used to do the AND operation on an integer value. /Mike

Originally posted by Mikael Jonasson: & is a bitwise operator, used to do the AND operation on an integer value. /Mike

Actually, the & operator works for both int values and boolean values in Java. As Carl put it, && is the short circuit operator. It will only evaluate the second operand if it needs to (see Sheriff Carl's example). The &, when applied to boolean's, will always evaluate both operands. & also is a bitwise AND operator when applied to int's.

[This message has been edited by Bob Graffagnino (edited June 26, 2001).]

Nancy
Greenhorn

Joined: Jun 21, 2001
Posts: 23

posted

0

Thanks to all Nancy

Don't get me started about those stupid light bulbs.