Java Tutorials - Java Logical Operators

The logical operators introduce the concept of boolean algebra to the Java language and is based on testing conditions based the results being true or false.

The concept of logical operators is simple. They allow a program to make a decision based on multiple conditions. Each operand is considered a condition that can be evaluated to a true or false value.

Operator

Result

&&

AND

&

Logical AND

||

OR

|

Logical OR

!

Logical unary NOT

==

Equals to

!=

Not Equals to

^

XOR


The AND operator (whose symbol is &&) is used evaluates to true if and only if both of its operands evaluate to true.

The OR "||" evaluates to true if either or both of its operands evaluate to true

The NOT "!" operator Performs a logical NOT of the operand.

Examples:


public class incrementdecrementSample{
	public static void main(String[] args) {
	
	firstVar = 10;
	secondVar = 9;
	if (firstVar == 10 && secondVar == 9){
		system.out.println("Logical AND - both sides of the equation are true");
	}
}
public class incrementdecrementSample{
	public static void main(String[] args) {
	
	firstVar = 10;
	secondVar = 9;
	if !(firstVar == 12){
		system.out.println("This statement is true as, 
firstVar is 10 which is false and the inverse of false is true"); } }

The ! operator is used to convert true values to false and false values to true. In other words, it inverts a value

All the above examples evaluate to true.

java increment decrement operators tutorial java bitwiser operators tutorial