Java Logical Operators: Examples and Usage
☰Fullscreen
Table of Content:
The following program is simple examples which demonstrate the Logical operators. Copy and paste the following Java program in LogicalOperators.java file, and compile and run this program
Logical AND Operator
public class LogicalANDoperator { public static void main(String args[]) { boolean p = false; boolean q = true; System.out.println("p && q = " + (p&&q)); } }
Output
p && q = false Press any key to continue . . .
Logical OR Operator
public class LogicalOROperators { public static void main(String args[]) { boolean p = false; boolean q = true; System.out.println("p || q = " + (p||q)); } }
Output
p || q = true Press any key to continue . . .
Logical NOT Operator
public class LogicalONOTperators { public static void main(String args[]) { boolean p = false; boolean q = true; System.out.println("!(p && q) = " + !(p && q)); } }
Output
!(p && q) = true Press any key to continue . . .
Logical Operator all in all
public class LogicalOperators { public static void main(String args[]) { boolean p = true; boolean q = false; System.out.println("p && q = " + (p&&q)); System.out.println("p || q = " + (p||q) ); System.out.println("!(p && q) = " + !(p && q)); } }
Output
p && q = false p || q = true !(p && q) = true Press any key to continue . . .
What are the differences between bitwise and logical AND operators
Issues |
Bitwise AND | Logical AND |
Operator Symbol | A Bitwise And operator is represented as ‘&’ | logical operator is represented as ‘&&’ |
Operator Scope | The bitwise and operator ‘&’ work on Integral (short, int, unsigned, char, bool, unsigned char, long) values and return integral value. Example | The logical and operator ‘&&’ expects its operands to be boolean expressions (either 1 or 0) and returns a boolean value. Error if Solution |
The problem in Operator | If an integer value is used as an operand for ‘&&’ which is supposed to work. Example | If an integer value is used as an operand for ‘&&’ which is supposed to not work Example |
Operands behaviour | The bitwise ‘&’ and ‘|’ operators always evaluate their operands. | The '&&' operator doesn’t evaluate the second operand if the first operand becomes false. Similarly '||' doesn’t evaluate the second operand when the first operand becomes true. supposed to not work Example 1 Example 2 |
Example
public class LogicalANDproblem { public static void main(String args[]) { int x = 2; int y = 3; int c = x&y ; System.out.println(c); } }
Output
2 Press any key to continue . . .
Example Error
public class LogicalOperator { public static void main(String args[]) { int x = 5; int y = 3; int c = x&&y ; System.out.println(c); } }
Output
LogicalANDproblem.java:6: error: bad operand types for binary operator '&&' int c = x&&y ; ^ first type: int second type: int 1 error
Solution to this problem
public class LogicalANDsolution { public static void main(String args[]) { boolean x = true; boolean y = false; boolean c = x&&y ; System.out.println(c); } }
Output
false Press any key to continue . . .
Example
public class BitwiseAND { public static void main(String args[]) { int x = 6; int y = 5; int c = x&y ; System.out.println(c); } }
Output
4 Press any key to continue . . .
Example 1
class OperatorExample{ public static void main(String args[]){ int x=11; int y=6; int c=22; System.out.println(xOutput
false false Press any key to continue . . .Example 2
class OperatorExample{ public static void main(String args[]){ int x=11; int y=6; int c=22; System.out.println(xOutput
false 11 false 12 Press any key to continue . . .