Logical Operators in Java:
We can use the Eclipse IDE for this example. If you do not know about it then follow this link- How to Install Eclipse For Java and create a program in it. |
The relational operators are used to evaluate whether a condition relating two operands is true or false. However, by themselves, they serve only to test simple relationships. In programming, you often need to determine complex conditional expressions. The logical operators allow combining two or more conditional statements into a single expression. As is the case with relational expressions, expressions that contain logical operators return true or false.
Operator | Meaning | Conditions | Result |
---|---|---|---|
&& | Conditional AND | false && false | false |
false && true | false | ||
true && false | false | ||
true && true | true | ||
| | | Conditional OR | false | | false | false |
false | | true | true | ||
true | | false | true | ||
true | | true | true | ||
! | Logical NOT | ! false | true |
! true | false | ||
& | Boolean Logical AND | false & false | false |
false & true | false | ||
true & false | false | ||
true & true | true | ||
| | Boolean Logical Inclusive OR | false | false | false |
false | true | true | ||
true | false | true | ||
true | true | true | ||
^ | Boolean Logical Exclusive OR | false ^ false | false |
false ^ true | true | ||
true ^ false | true | ||
true ^ true | false |
Note: The &, | and ^ operators are also bitwise operators when they are applied to integral operands.

package com.java_tutorial;
public class Logical_operator {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 5, b = 8;
int x = 4, y = 9;
boolean z = a > b && x < y;
System.out.println("Conditional AND Result is " + z);
boolean z1 = a < b && x < y;
System.out.println("Conditional AND Result is " + z1);
boolean z2 = a > b || x > y;
System.out.println("Conditional OR Result is " + z2);
boolean z3 = a < b || x < y;
System.out.println("Conditional OR Result is " + z3);
boolean z4 = !(a > b);
System.out.println("Logical NOT Result is " + z4);
boolean z5 = !(a < b);
System.out.println("Logical NOT Result is " + z5);
}
}
Tutorial MySQL Tutorial Python Procedure-Oriented Programming Object-Oriented Programming Modular Programming Structured Programming WHILE Loop in C Language Java (programming language)– Wikipedia |
Comments (No)