6.4 Logical Operators in Java

Logical operators in Java play a fundamental role in controlling the decision flow within a program. They are used to form Boolean expressions, that is, expressions that result in a true or false value. These expressions are the basis for making decisions in conditional structures such as if, switch, while, and for. Let's explore the main logical operators available in Java.

AND operator (&&)

The logical operator AND is represented by two commercial waits (&&) and is used to check whether two or more conditions are true simultaneously. The expression will only be true if all conditions evaluated by the AND operator are true.

boolean result = (condition1 && condition2);

For example, if we want to check whether a number is positive and even, we could write:

int number = 6;
boolean ehPositivoEPar = (number > 0) && (number % 2 == 0); // true

OR operator (||)

The OR operator is represented by two vertical bars (||) and is used to check whether at least one of several conditions is true. The resulting expression will be true if any of the conditions evaluated by the OR operator are true.

boolean result = (condition1 || condition2);

For example, to check whether a number is positive or odd, we could write:

int number = -3;
boolean ehPositivoOrOdd = (number > 0) || (number % 2 != 0); // true

NOT operator (!)

The NOT operator is represented by an exclamation mark (!) and is used to invert the Boolean value of a condition. If the original condition is true, the NOT operator turns it into false, and vice versa.

boolean condition = true;
boolean result = !condition; // false

For example, to check if a number is not positive, we could write:

int number = -3;
boolean naoEhPositivo = !(number > 0); // true

Relational Comparison Operators

In addition to logical operators, we have comparison operators, which are often used in conjunction with logical operators. They include:

  • == (equal to)
  • != (different from)
  • > (greater than)
  • >= (greater than or equal to)
  • < (less than)
  • <= (less than or equal to)

For example, to check whether two numbers are equal, we could use the == operator:

int a = 5;
int b = 5;
boolean areIguais = (a == b); // true

To check if they are different, we would use the operator !=:

int a = 5;
int b = 10;
boolean areDifferent = (a != b); // true

Bitwise Logical Operators

Java also offers logical operators that operate at the bit level, known as bitwise operators. They are similar to Boolean logical operators, but are applied to bitwise integer operands. Bitwise operators include:

  • & (AND bit by bit)
  • | (bitwise OR)
  • ^ (bitwise XOR)
  • ~ (NOT bitwise)

These operators are mainly used in low-level programming, such as manipulating flags or coding communication protocols.

Short Circuit

It is important to mention the concept of short circuit in logical operators. In the case of the AND operator (&&), if the first condition is false, the second condition will not be evaluated, as the final result of the logical expression is already known to be false. Similarly, in the OR operator (||), if the first condition is true, the second condition will not be evaluated as the final result is already known to be true. This can improve program performance by avoiding unnecessary evaluations.

Conclusion

Logical operators are fundamental for constructing conditional expressions in Java. They allow developers to express complex conditions in a concise and readable way. Understanding how and when to use each of these operators is essential for creating efficient and reliable programs. Practicing using these operators in different scenarios will increase your familiarity with them and help you develop an intuition for solving logical programming problems.

In addition, it is important to remember that code readability is as important as its functionality. Therefore, when using logical operators, always consider whether the expression is clear to other developers who may read your code in the future. Comments and good code structure can help clarify the intent behind using certain logical operators.

In summary, the ability to use logical operators andeffectively is a crucial aspect of Java programming. It allows you to control the program flow and make decisions based on multiple conditions, making your code more dynamic and adaptable to different situations.

Now answer the exercise about the content:

_Which of the following logical operators in Java is used to check whether at least one of several conditions is true?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Operators in Java (arithmetic, logical, etc.): Increment and Decrement Operators

Next page of the Free Ebook:

35Operators in Java (arithmetic, logical, etc.): Increment and Decrement Operators

6 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text