Logical Operators in Java

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.

OperatorMeaningConditionsResult
&&Conditional ANDfalse && falsefalse
false && truefalse
true && falsefalse
true && truetrue
| |Conditional ORfalse | | falsefalse
false | | truetrue
true | | falsetrue
true | | truetrue
!Logical NOT! falsetrue
! truefalse
&Boolean Logical ANDfalse & falsefalse
false & truefalse
true & falsefalse
true & truetrue
|Boolean Logical Inclusive ORfalse | falsefalse
false | truetrue
true | falsetrue
true | truetrue
^Boolean Logical Exclusive ORfalse ^ falsefalse
false ^ truetrue
true ^ falsetrue
true ^ truefalse

Note: The &, | and ^ operators are also bitwise operators when they are applied to integral operands.

Java Logical Operators
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)

Leave a Reply