difference between & and && operators in if.
Hi,
this question seems like silly, but I am confused when I used these two operators in "if "condition,
is these 2 operators are act like same in if statement. like for example
if( 10 == 10 & "a".equals("a")){
System.out.print("true");
}
if( 10 == 10 && "a".equals("a")){
System.out.print("true");
}
when I executed above statements both printing true.
Can you please clarify on this.
Thanks in advance
"&" operator:
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.22.1
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.22.2
"&&" operator:
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.23
& checks both expressions and && only until the evaluation is done.
here "a".equals("a") is evaluated
if( 10 == 9 & "a".equals("a")){
System.out.print("true");
}
here "a".equals("a") is not evaluated because the whole expression is false
if( 10 == 9 && "a".equals("a")){
System.out.print("true");
}
& is a bitwise operation. It used when you want to perform the AND operation between integers. For example:0101 & 0011 = 0001When you want to perform AND between booleans you should use &&
> & is a bitwise operation.
As well as a non-short-circuit boolean AND operator.
> It used when you want to
> perform the AND operation between integers. For
> example:0101 & 0011 = 0001
> When you want to perform AND between booleans you
> should use &&
Not necessarily true. One may want to use & in boolean expressions, when one does not want to short-circuit the whole expression. Such cases may involve expressions which invoke methods, and one still wants the methods to be invoked even though the result of the entire expression is going to evaluate to false.
I wouldn't do it that way, but the feature does exist to be used if one chooses.
Job security through code obscurity would be a good use for it :)
This is an old one but I still like it:PowerPlant harrisburg= new PowerPlant();
if (harrisburg.isSafe() && harrisburg.switchOn())
System.out.println("plant is operational");
...now change the '&&' operator to a '&' operator (which doesn't short circuit
its operand evaluation) and see for yourself.
kind regards,
Jos
> & is a bitwise operation.There's one in every bunch. :o)As mentioned, the & operator is both a bitwise operator and a logical operator; it's all in the operands.~
Usually true, but not entirely accurate. the single & operator is also used for boolean And, but it does not short circuit the evel. try this code ofr a test
String s = null;
String l = "l";
if(s == null && l.equals("l") )
{
System.out.println("test 1");
}
if(l.equals("a") && s.length() > 0)
{
System.out.println("test 2");
}
else
{
System.out.println("Short circuited, l != a");
}
try{
if(l.equals("a") & s.length() > 0)
{
System.out.println("test 3");
}
}
catch (NullPointerException npe)
{
System.out.println("Silly programmer, s is null");
}
~Tim
> DOGPILE!> > > ;o)> > ~I was trying to edit my post to add:Slow, and full of typos, must need caffeine, or sleep, but I was too slow to even do that much!~Tim
& is the short cut whats-it-a-called-thingie-mc-bob.
mlka at 2007-7-9 21:34:57 >
