Core Java

Boolean Operators Java Example

In this post, we feature a comprehensive article about boolean operators in Java.

1. Introduction

Boolean is the java data type. Boolean variables are declared using the boolean keyword, which accepts true or false. By default, it has the value false. It was used in the situation where you want one value out of two values. For example: On / Off, True / False, 1 /0, Yes / No.

  • Just like int, float, boolean is also the primitive data type. A boolean variable can store either the value true or false.
  • The operators such as logical operators, conditional operators, and comparison operators evaluate and return the boolean value.

2. Different types of operators

The following are different types of operators used in java expression to give boolean values.

Operator namesOperator
AND operator&
Short-circuit AND operator&&
OR operator|
Short-circuit OR operator || 
NOT operator!
XOR operator^
EQUAL TO operator==
NOT EQUAL TO operator  !=
Ternary operator?:
Lesser than<
Greater than>
Lesser than or equal to<= or ≤
Greater than or equal to>= or ≥

2.1. Logical AND Operator

  • Logical AND operators (& and &&) used to combine logical expression. Both combine two Boolean expressions and return true only if both expressions are true otherwise returns false.i.e: If both the operands are true, then the condition becomes true.
  • The && operator is similar to the & operator, but the && operator is more efficient than & operator. 
  • Left-hand side and Right-hand side expressions compared by the & operator must be true for all. Also, if the first expression returns false, then no reason to evaluate the second expression.
  •  The & operator always evaluates all expressions. The && operator evaluates the second expression only if the first expression is true. Otherwise, it stops after executing the first expression.
  • && is also called a short circuit logical AND operator

Code Example1

private static void checkUsingAndOperator() {
		boolean result1 = true;
		boolean result2 = false;
		boolean result3 = true;
		boolean result4 = false;
		
		System.out.println("AND Condition 1: "+(result1 && result2));
		System.out.println("AND Condition 2: "+(result2 && result3));
		System.out.println("AND Condition 3: "+(result1 && result3));
		System.out.println("AND Condition 4: "+(result2 && result4));
		System.out.println("AND Condition 5: "+(result2 & result4));
	}

Output

AND Condition 1: false
AND Condition 2: false
AND Condition 3: true
AND Condition 4: false
AND Condition 5: false

In the above code result2 && result3 statement evaluates only first operands because it is returning false, it returns false. In AND operation both operands should give true.

2.2 Logical OR Operator

  • Logical OR operators are | and ||, which gives true if any of the expression is true and gives false if all expression fails. i.e: If one of the operands is true, then the condition becomes true. If both operands fail, it returns false.
  • The || operator is similar to the | operator, but it is efficient than | operator.
  • || is also called a short circuit logical OR operator.

Code Example2

private static void checkUsingOROperator() {
		boolean result1 = true;
		boolean result2 = false;
		boolean result3 = true;
		boolean result4 = false;
		
		System.out.println("OR Condition 1: "+(result1 || result2));
		System.out.println("OR Condition 2: "+(result2 || result3));
		System.out.println("OR Condition 3: "+(result1 || result3));
		System.out.println("OR Condition 4: "+(result2 || result4));
	} 

Output

OR Condition 1: true
OR Condition 2: true
OR Condition 3: true
OR Condition 4: false

When the result is clearly known, boolean expressions stop evaluation. result1 || result2 When the statement executes when result1 gives true, the boolean expression evaluates to true. It does not even ready to evaluate for result2

2.3 NOT Operator

  • Logical NOT Operator is used to reversing the logical state of its operand. If a condition is true then Logical NOT operator will make false.

Code Example3

	private static void checkUsingNOTOperator() {
		boolean result1 = true;
		System.out.println("Not Condition: "+!result1);
	}
	

Output

Not Condition: false

2.4 XOR Operator

  • Logical Exclusive OR Operator returns true if and only if the operands are different. Returns false if two operands have the same value.

Code Example4

	private static void checXorOperator() {
		boolean result1 = true;
		boolean result2 = false;
		boolean result3 = true;
		System.out.println("Xor Condition 1: "+(result1 ^ result2));
		System.out.println("Xor Condition 2: "+(result1 ^ result3));
	}
	
	

Output

Xor Condition 1: true
Xor Condition 2: false

2.5 EqualTo Operator

  • == operator checks if the values of two operands/expressions are equal or not. If equal then condition becomes true, otherwise it is false.
  • Use this operator for primitive data types like int but not for objects like Employee or String.
  • With objects, it is most common to use the equals() method to test if two objects represent the same value.

Code Example5

	
	private static void checkUsingEqualToOperator() {
		boolean result1 = true;
		boolean result2 = false;
		boolean result3 = true;
		boolean result4 = false;
		
		System.out.println("EqualTo Condition 1: "+(result1 == result2));
		System.out.println("EqualTo Condition 2: "+(result2 == result3));
		System.out.println("EqualTo Condition 3: "+(result1 == result3));
		System.out.println("EqualTo Condition 4: "+(result2 == result4));
	}	

Output

EqualTo Condition 1: false
EqualTo Condition 2: false
EqualTo Condition 3: true
EqualTo Condition 4: true

2.6 Not Equal To Operator

  • != operator checks if the values of two operands/expressions are equal or not. If it is not equal then the condition becomes true, otherwise, it is false.
  • Use this operator for primitive data types like int  but not for objects like Employee or String

Code Example6

	
private static void checkUsingNOTEqualToOperator() {
		boolean result1 = true;
		boolean result2 = false;
		boolean result3 = true;
		boolean result4 = false;
		
		System.out.println("Not EqualTo Condition 1: "+(result1 != result2));
		System.out.println("Not EqualTo Condition 2: "+(result2 != result3));
		System.out.println("Not EqualTo Condition 3: "+(result1 != result3));
		System.out.println("Not EqualTo Condition 4: "+(result2 != result4));
		System.out.println("Not EqualTo Condition 5: "+!(result2 && result4));
		System.out.println("Not EqualTo Condition 6: "+!(result2 || result4));
		System.out.println("Not EqualTo Condition 7: "+!result2);
}

Output

Not EqualTo Condition 1: true
Not EqualTo Condition 2: true
Not EqualTo Condition 3: false
Not EqualTo Condition 4: false
Not EqualTo Condition 5: true
Not EqualTo Condition 6: true
Not EqualTo Condition 7: true

2.7 Ternary Operator

the Ternary operator is used to evaluate if-else conditions. It is an alternative or short form of if-else statement. It uses two operands such as ?:

Code Example7

	
private static void checkTernaryOperator (){
		int a = 5;
		int b = 10;
		int result = (a < b) ? a : b;
		System.out.println ("Ternary result : "+result);
}

Output

Ternary result : 5

2.8 Lesser than Operator

Lesser than operator checks if left-hand expression/operand is less than the right-hand expression/operand.

Code Example8

	
private static void checkLesserThanOperator() {
		int num1 = 5;
		int num2 = 10;
		boolean result = num1 < num2;
		System.out.println ("Lesser than Result : "+result);
}

Output

Lesser than Result : true

2.9 Greater than Operator

Greater than operator checks if left-hand expression/operand is greater than the right-hand expression/operand.

Code Example9

		
private static void checkGreaterThanOperator() {
		int num1 = 5;
		int num2 = 10;
		boolean result = num2 > num1;
		System.out.println ("Greater than Result : "+result);
}

Output

Greater than Result : true

2.10 Lesser than or equal to Operator

<= operator checks if left-hand expression/operand is less than (or) equal to the right-and expression/operand.

Code Example10

	
private static void checkLesserEqualToOperator() {
		int num1 = 10;
		int num2 = 5;
		int num3 = 5;
		boolean result1 = num3 <= num1;
		boolean result2= num2 <= num3;
		System.out.println ("<= Result1 : "+result1);
		System.out.println ("<= Result2 : "+result2);
}

Output

<= Result1 : true
<= Result2 : true

2.11 Greater than or equal to Operator

>= operator checks if left-hand expression/operand is less than (or) equal to the right-hand expression/operand.

Code Example11

		private static void checkGreaterEqualToOperator() {
		int num1 = 10;
		int num2 = 10;
		int num3 = 5;
		boolean result1 = num2 >= num1;
		boolean result2= num2 >= num3;
		System.out.println (">= Result1 : "+result1);
		System.out.println (">= Result2 : "+result2);
	}

Output

>= Result1 : true
>= Result2 : true

3. Boolean Operators Java Example – Conclusion

This article shows the different examples, which evaluates and return a boolean value. It is important to do comparisons and conditions. If you need only one of two values (1 or 0, true or false, on or off, yes or no, flag) you should use boolean data type. I hope this article helps you.

4. Download the Source Code

This was an example of a boolean operator in Java.

Download
You can download the full source code of this example here: Boolean Operators Java Example

Kavitha Ashok

Kavitha has a Master's degree in Information Technology and a Master's degree in Business Administration. She started her career as a Junior Software Developer trainee and moved up as a Technical lead. She has been involved with projects in various domains such as CMS, IMS, android and digital TV products, applications, web services, etc. , She is mainly involved in development, design, analysis, refactoring, etc., based on Java and other technologies.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button