Equality, Relational and Conditonal Operators

19 0 0
                                    

Equality and Relational Operators
    1. Equal to ( == )
    2. Not equal to ( != )
    3. Greater than ( > )
    4. Greater than or equal to ( >= )
    5. Less than ( < )
    6. Less than or equal ( <= )

=================================

int x = 100;
int y = 200;
boolean z = ( x == y );
System.out.println("x == y: " + z);

================================
Result:
x == y: false

Conditional operators
    - comparing two or more conditions.

    1. AND  ( && )
        - becomes true when all conditions were true.
    2. OR ( || )
        - becomes true when one or more conditions is/ are true.

===============================

int x = 100;
int y = 200;
int z = 300;
boolean b = ( x < y ) && ( y < z );
System.out.println(b);

==============================
Result:
True

First condition was true.
Second condition was true.
All conditions were true. So, in AND, if all conditions were true, then the result is true.

Ternary Operator
    - this function like an if else statement.
    - ( condition ) ? values1 : value2;
    - so it is read like this. If condition is true, give the value1  else give the value2.

================================

int x = 100;
int y = 200;
int z = ( x > y ) ? x : y;
System.out.println(z);

=================================
200

The result is 200 since x is not Greater than y.

instanceof
    - this compares if an object is an instance of the class, subclass or an interface.

===============================

class SuperClass{}
class Subclass extends SuperClass{
    public static void main(String[] args){
        SubClass sb = new Subclass();
        boolean z= sb instanceof SuperClass;
        System.out.println(z);
    }
}

==============================
Result:
true

Java ProgrammingWhere stories live. Discover now