MOCKSTACKS
EN
Questions And Answers

More Tutorials









Java Relational and Logical Operators


Relational Operators in Java :


Relational operators are used to evaluate conditions (true or false) inside the if statements. Some examples of relational operators are:

  • == (equals)

  • >= (greater than or equals to)

  • > (greater than)

  • < (less than)

  • <= (less than or equals to)

  • != (not equals)

  • Note: ‘=’ is used for an assignment whereas ‘==’ is used for equality check. The condition can be either true or false.

    Logical Operators :


    Logical operators are used to provide logic to our Java programs.
    There are three types of logical operators in Java :
  • && - AND

  • || - OR

  • ! – NOT

  • AND Operator :


    Evaluates to true if both the conditions are true.

  • Y && Y = Y

  • Y && N = N

  • N && Y = N

  • N && N = N

  • Convention: # Y – True and N - False


  • OR Operator :


    Evaluates to true when at least one of the conditions is true.

  • Y || Y = Y

  • Y || N = Y

  • N || Y = Y

  • N || N = N

  • Convention: # Y – True and N - False


  • NOT Operator :


    Negates the given logic (true becomes false and vice-versa)

  • !Y = N

  • !N = Y

  • Example


    package com.company;
    
    public class logical {
        public static void main(String[] args) {
            System.out.println("For Logical AND...");
            boolean a = true;
            boolean b = false;
            if (a && b){
                System.out.println("Y");
            }
            else{
                System.out.println("N");
            }
    
            System.out.println("For Logical OR...");
    
            if (a || b){
                System.out.println("Y");
            }
            else{
                System.out.println("N");
            }
    
            System.out.println("For Logical NOT");
            System.out.print("Not(a) is ");
            System.out.println(!a);
            System.out.print("Not(b) is ");
            System.out.println(!b);
        }
    }
    

    Output

    For Logical AND...
    N
    For Logical OR...
    Y
    For Logical NOT
    Not(a) is false
    Not(b) is true


    Conclusion

    In this page (written and validated by ) you learned about Java Relational and Logical Operators . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java Switch Case Statements.



    Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


    Share On:


    Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.