Home » Core Java » Logical Operators in Java

About Sergio Lauriano Junior

Avatar photo
Sergio is graduated in Software Development in the University City of São Paulo (UNICID). During his career, he get involved in a large number of projects such as telecommunications, billing, data processing, health and financial services. Currently, he works in financial area using mainly Java and IBM technologies.

Logical Operators in Java

In this article, we’re going to see the logical operators in Java, which are they, and how to use them through some examples.

1. Introduction

java logical operators

As a developer, I see a lot of similar things in a language and logical operators are one of them. Basically, logical operators are used to deciding or validating conditions and return a Boolean to determine if the condition is TRUE or FALSE.

In the next steps we’ll see the most important logical operators in Java: AND (&&), OR (||), and NOT (!).

2. Pre-requisites

The minimum Java version for executing the article’s example is JDK 8 (find here), but we can use the most recently released Java version (JDK 15).

Also, I’m using IntelliJ 2020.2, but you can use any IDE with support for the versions recommended above.

3. Java Logical Operators

3.1 AND Operator

The AND (&&) operator validates if two or more conditions are TRUE. If the first evaluated condition is FALSE, the code won’t validate the next condition and the operator returns FALSE as a result.AND operator example

boolean a = true;
boolean b = true;
// The return is only reached if a and b are true
if(a && b) {
    return "OK";
}

3.2 OR Operator

The OR (||) operator validates if one condition is TRUE, unlike the AND operator where all conditions need to be TRUE.

So, if the first evaluated condition is TRUE, the code won’t evaluate the next condition and the operator returns TRUE as a result.OR operator example

boolean a = true;
boolean b = false;
// The return is reached if a OR b is true
if(a || b) {
    return "OK";
}

3.3 NOT Operator

The NOT(!) operator is a little different from AND and OR operators. We use the NOT operator to accept a false condition as a TRUE condition.

As an example, if your validation needs to validate a condition that is NOT something, we can use the NOT operator to deny and the result of the validation is TRUE.NOT operator example

boolean a = true;
boolean b = false;
// The return is reached because a is TRUE and b is FALSE, but using NOT operator, we accept FALSE as a valid condition
if(a && !b) {
    return "OK";
}
// We can use with OR operator as well
if(!b || a) {
    return "OK";
}

4. Tips

As said before, we use these logical operators to validate two or more conditions. But, as a tip that I give, be careful on how you use the operators.

Because nowadays we pursuit the closest perfection in our code. So, we can use tools to evaluate or code like Sonarqube, DeepSource, etc…

These tools analyze our code validating some rules and the use of too many if/else with the operator (&&, ||, !) can be scored as a code smell.

That said, try to put your code in a compliant solution.Compliant code for validating condition

if(a && b || c && d && e || f) { // Non compliant because too much validations
    return "OK";
}

if((fooAnd(a,b) || fooOr(c,d)) && fooOr(e,f)) { // Complaint because 
// the validations are made in a sub-method in a cleaner way
    return "OK";
}

private boolean fooAnd(boolean x, boolean y) {
    return x && y;
}

private boolean fooOr(boolean x, boolean y) {
    return x || y;
}

5. Summary

In summary, we saw the most used Java logical operators. We got the existing ones (AND, OR, and NOT) and how to apply them to our code through some practical examples.

6. Download the source code

Download
You can download the full source code of this example here: Logical Operators in Java

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

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