How to use the OR operator and the AND operator in Java
The Java operators AND and OR are used to check the accuracy of conditions in Java. The two operators work very similarly but differ when it comes to the details. In this article, we’ll explain everything 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 programming 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 understand for anyone learning programming.
The AND operator in Java evaluates two conditions and returns true
only when both statements or conditions are true. Otherwise, it will return false
. The operator is expressed with &&
. The OR operator in Java also evaluates two conditions. It returns true
when one or both of the statements or conditions is true. It only returns false
if both statements are false. It’s expressed using ||
.
The syntax of the two operations looks as follows:
statement1 operator statement2
javaBelow we’ll look at a few practical examples that show how Java’s AND and OR operators are used.
- 99.9% uptime and super-fast loading
- Advanced security features
- Domain and email included
How to use the AND operator in Java
Java’s AND operator is used to evaluate whether two statements are both true. Its syntax look like this:
statement1 && statement2
javaIt outputs true
if both conditions are true. If one or both of the conditions is false, it outputs false
.
In our first example, we’ll give the system two simple statements 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);
}
}
javaThe 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 statements 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);
}
}
javaAgain, 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 conditions are not true, the system will return false
.
In our third example, the evaluation 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);
}
}
javaHow to use the OR operator in Java
Java’s OR operator also evaluates two conditions. Its syntax looks like this:
statement1 || statement2
javaIn contrast to the AND operator, the OR operator returns true
if at least one of the two statements is true. It also returns true
if both statements are true. It will only return false
if neither of the statements is true. Below we’ll illustrate 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);
}
}
javaThe 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);
}
}
javaThe 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 statements is true, the system will return true
.
We’ll only get an output of false
when neither of the conditions 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);
}
}
javaIn this example, the system looks at the first statement and determines 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
.