The Java operators AND and OR are used to check the accuracy of con­di­tions in Java. The two operators work very similarly but differ when it comes to the details. In this article, we’ll explain every­thing you need to know about AND and OR in Java.

What are Java’s AND and OR operators?

Java operators are important tools for working with the pro­gram­ming language. There are different kinds of operators for different purposes. Java’s AND and OR are logical operators that are used to check the accuracy of a state. They return the *Boolean values “true” or “false”. They are important to un­der­stand for anyone learning pro­gram­ming.

The AND operator in Java evaluates two con­di­tions and returns true only when both state­ments or con­di­tions are true. Otherwise, it will return false. The operator is expressed with &&. The OR operator in Java also evaluates two con­di­tions. It returns true when one or both of the state­ments or con­di­tions is true. It only returns false if both state­ments are false. It’s expressed using ||.

The syntax of the two op­er­a­tions looks as follows:

statement1 operator statement2
java

Below we’ll look at a few practical examples that show how Java’s AND and OR operators are used.

Web Hosting
Hosting that scales with your ambitions
  • Stay online with 99.99% uptime and robust security
  • Add per­for­mance with a click as traffic grows
  • Includes free domain, SSL, email, and 24/7 support

How to use the AND operator in Java

Java’s AND operator is used to evaluate whether two state­ments are both true. Its syntax look like this:

statement1 && statement2
java

It outputs true if both con­di­tions are true. If one or both of the con­di­tions is false, it outputs false.

In our first example, we’ll give the system two simple state­ments and use the Java command System.out.println to return a Boolean value:

public class Main {
	public static void main(String[] args) {
		int x = 3;
		System.out.println(x > 1 && x < 7);
	}
}
java

The system will first evaluate whether the value 3 is larger than 1. It is. Then it evaluates the truth of the statement “3<7”. This is also true. Since both state­ments are true, it outputs “true”.

Now let’s see what happens when one statement is true but the other one isn’t. We’re going to make a slight change to the above example:

public class Main {
	public static void main(String [] args) {
		int x = 3;
		System.out.println(x > 1 && x > 7);
	}
}
java

Again, the system will first evaluate whether the value 3 is larger than 1 and find that it is true. However, this time the second statement is not true. Since both con­di­tions are not true, the system will return false.

In our third example, the eval­u­a­tion will be even shorter. Since the first condition isn’t fulfilled, the system doesn’t even look at the second condition. Instead, it just outputs false:

public class Main {
	public static void main(String [] args) {
		int x = 3;
		System.out.println(x > 3 && x < 7);
	}
}
java

How to use the OR operator in Java

Java’s OR operator also evaluates two con­di­tions. Its syntax looks like this:

statement1 || statement2
java

In contrast to the AND operator, the OR operator returns true if at least one of the two state­ments is true. It also returns true if both state­ments are true. It will only return false if neither of the state­ments is true. Below we’ll il­lus­trate how this works with a few simple examples.

public class Main {
	public static void main(String [] args) {
		int x = 3;
		System.out.println(x > 1 || x < 7);
	}
}
java

The system will return true for this example. Since the first statement is true, the condition of the OR operator is fulfilled. So far this looks pretty similar to the AND operator.

Now we’ll look at an example where OR starts to differ from AND:

public class Main {
	public static void main(String [] args) {
		int x = 3;
		System.out.println(x > 1 || x > 7);
	}
}
java

The first statement is true, since 3 is larger than 1. However, the second statement is not, since 3 is not larger than 7. Since one of the two state­ments is true, the system will return true.

We’ll only get an output of false when neither of the con­di­tions is true, as is the case below:

public class Main {
	public static void main(String [] args) {
		int x = 3;
		System.out.println(x > 3 || x > 7);
	}
}
java

In this example, the system looks at the first statement and de­ter­mines that it’s false. Then it turns to the second statement. If the second statement were true, the output would be true. However, since the second statement is in fact false, the output is false.

Go to Main Menu