The logical(non-bitwise) operators && and || are short-circuit operators. In other words, with && if the LHS(Left Hand Side) is false, the RHS(Right Hand Side) will never be evaluated. With || if the LHS is true, then the RHS will never be evaluated. On the other hand, the bitwise operators & and | are non short-circuit, will always evaluate both the LHS and RHS.
This is especially useful in guarding against nullness.
The above condition is the best example for handle the null exception. If u put first condition is not equal to null then only it will check for other conditions.
In the above example (bitwise & operator) ,it will check the all conditions in the if statements, so it will throw the NullPointerException.
One main difference between & and && is, bitwise & you can use with both integral and boolean but && you can use with only boolean operands.
Output:- Inside else Statement
This is especially useful in guarding against nullness.
String x = null;
if ( x != null && x.equals("xyz") {
then do something with x...
}
The above condition is the best example for handle the null exception. If u put first condition is not equal to null then only it will check for other conditions.
String x = null;
if ( x != null & x.equals("xyz") {
then do something with x...
}
In the above example (bitwise & operator) ,it will check the all conditions in the if statements, so it will throw the NullPointerException.
One main difference between & and && is, bitwise & you can use with both integral and boolean but && you can use with only boolean operands.
Some Programs based on logical & bitwise operators:--
1) What is the output of given code?
class OperatorExOne {
public static void main (String[] args) {
String a = null;
if(a != null && a.equals("Java")) {
System.out.println("Inside If Statement");
} else {
System.out.println("Inside else Statement");
}
}
}
Output:- Inside else Statement
In the above code it will print else part i.e "Inside else Statement". In if statement there are two conditions with && operator. In logical AND operator it will check the first condition if it is false then it won't check the 2nd condition and it will return false. In the above code first condition is false so it won't check the 2nd condition. In second condition it is exception but it won't check the condition at all.
In the above code,if we changed the first condition of if statement, then it will throw NullPointerException, See below.
2) What is the output of given code?
Output:-java.lang.NullPointerException
Above code will throw NullPointerException because the first condition of if statement is true and it will check the second condition, and will throw NullPointerException.
3) What is the output of given code?
Output:- 2
4) What is the output of given code?
Output:- Compilation error,
In the above code,if we changed the first condition of if statement, then it will throw NullPointerException, See below.
2) What is the output of given code?
class OperatorExTwo {
public static void main (String[] args) {
String a = null;
if(a.equals("a") && a != null ) {
System.out.println ("Inside If Statement");
} else {
System.out.println ("Inside else Statement");
}
}
}
Output:-java.lang.NullPointerException
3) What is the output of given code?
class OperatorExThree {
public static void main (String[] args) {
int a = 2;
int b = 3;
int result = a & b;
System.out.println(result);
}
}
Output:- 2
4) What is the output of given code?
class OperatorExFour {
public static void main (String[] args) {
int a = 2;
int b = 3;
int result = a && b; //Compilation error
System.out.println(result);
}
}
Output:- Compilation error,
The operator && is undefined for the argument
type(s) int, int
Related Posts:--
1) String Interview Questions and Answers
2) Exception Handling Interview questions and answers
3) Interface and Abstract Class Interview Questions and Answers