Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
logicaloperator
communitycreator

What are the logical operators "and", "or", and "not" in Java?

harris910

A logical operator is a symbol or word that connects two or more expressions so that the value of the produced expression created is solely determined by the value of the original expressions and the operator’s meaning.

Following are the logical operators available in Java.

  • and
  • or
  • not

Description

The following table explains each of the logical operators in Java.

Operator Symbol

Operator Name

Example

Description

&&

Logical And

Operand-A && Operand-B

It returns True, when both the operand-A and operand-B is True, otherwise it returns False.

||

Logical Or

Operand-A || Operand-B

It returns True, when either the operand-A or operand-B is True, otherwise it returns False.

!

Logical Not

! Operand-A

It returns the operand's logical state in reverse.

The figure below shows the visual representation of the logical operators in Java.

Visual representation of Logical Operators in Java

Code

The following code illustrates how to use these logical operators in Java.

class Java {
    public static void main( String args[] ) {
        
        int A=10, B=20, C=30;

	    //And Operator
	    //Both True
        System.out.println("(A<B && B<C):");
        System.out.println((A<B && B<C));
        //First operand: True		Second Operand: False
        System.out.println("(A<B && B>C):");
        System.out.println((A<B && B>C));
        //Both False
        System.out.println("(A>B && B>C):");
        System.out.println((A>B && B>C));

        //OR operator
	    //Both True
        System.out.println("(A<B || B<C):");
        System.out.println((A<B || B<C));
        //First operand: True		Second Operand: False
        System.out.println("(A<B || B>C):");
        System.out.println((A<B || B>C));
        //Both False
        System.out.println("(A>B || B>C):");
        System.out.println((A>B || B>C));

        //Not operator
	    boolean D=true;
	    //reverse of true logical state
        System.out.println("(!D):");
        System.out.println((!D));

    }
}

RELATED TAGS

java
logicaloperator
communitycreator
RELATED COURSES

View all Courses

Keep Exploring