Operators

Operators are nothing but symbols that perform specific operations. In this chapter let us discuss in detail the different types of operators Basic terminologies that we are going to use often are operator and operand If X+Y is the operation X and Y are the operands,+ is the operator

The following section will describe the types of operators

1. Arithmetic Operators

Arithmetic operators perform mathematical functions like addition, subtraction, multiplication, division, and modulor division. It requires a minimum of two operands.

 

Operator

Name

Description

     Additive operator

performs addition and is also used for String concatenation

-   

   Subtraction operator

performs subtraction of operands

*   

   Multiplication operator

performs multiplication of operands

/   

   Division operator

performs division of operands

%  

    Remainder operator

Performs the division of two operands x and y and returns the remainder value

 

Program:

public class SampleArithOperator {
    public static void main(String[] args) {
        int x = 10;
        int y = 5;
        int result = x + y;
        System.out.println("x+y=" + result); //+ is used for String concatenation
        System.out.println(x - y);
        System.out.println(x * y);
        System.out.println(x / y);
        System.out.println(x % y);
    }
}

 

OUTPUT:

x+y=15
5
50
2
0
 
 

2. Unary Operators

The unary operators perform various operations such as incrementing or decrementing a value by one, negating an expression, or inverting the value of a boolean. A unary operator requires only one operand.

 

 Operator

Name

Description

+         

Unary plus operator

 indicates positive value

-          

Unary minus operator

 indicates negative value

++       

Increment operator

 increments a value by 1

--         

Decrement operator

 decrements a value by 1

!          

Logical complement operator

 inverts the value of a boolean

 

Program:

public class SampleUnaryOperator {
    public static void main(String[] args) {
        int x = 10;
        int y = 5;
        int z = 3;
        boolean success = false;
        x++;
        y--;
        z = -z;
        System.out.println("x++ is: " + x);
        System.out.println("y-- is: " + y);
        System.out.println("-z is: " + z);
        System.out.println(success);
        System.out.println(!success);
    }
}

 

OUTPUT:

x++ is: 11
y-- is: 4
-z is: -3
false
true
 

 

3. The Equality and Relational Operators:

The equality and relational operators are used to determine whether one operand is greater than or less than equal to or not equal to another operand. Here equal to is represented as == not =

 

Operator

Name

Description

== 

    equal to

checks whether the value of two operands is equal

!= 

  not equal to

checks whether the value of two operands is equal

>   

   greater than

checks whether the value of one operand is greater than the second operand

>=   

  greater than or equal to

checks whether the value of one operand is greater than or equal to the second operand

<     

 less than

checks whether the value of one operand is less than the second operand

<=     

less than or equal to

checks whether the value of one operand is less or equal to the second operand

 

Program:

public class SampleRelationalOperator {
    public static void main(String[] args) {
        int x = 10;
        int y = 5;
        if (x == y)//false
            System.out.println("x == y");
        if (x != y)//true
            System.out.println("x != y");
        if (x > y)//true
            System.out.println("x > y");
        if (x < y)//false
            System.out.println("x < y");
        if (x <= y)//false
            System.out.println("x <= y");
    }
}

 

 

In the above sample, only the true statements will be executed.

OUTPUT:

x != y
x > y
 

 

4. The Conditional Operators:

 

operator

name

&&     

 Conditional-AND

||      

Conditional-OR

?:     

Ternary operator

 

The  && and || operators perform Conditional-AND(&&) and Conditional-OR(||) operations on two boolean expressions whereas the ternary operator is the shorthand of if-then-else-statement

the syntax for the ternary operator:

variable x = (expression) ? value if true : value if  false

 

The below example demonstrates the ternary operator

public class SampleTernaryOperator {
    public static void main(String[] args) {
        int x = 10;
        int y = 5;
        int result;
        int reuse;
        result = (x != y) ? x : y;//true
        reuse = (x == y) ? x : y;//false
        System.out.println(result);
        System.out.println(reuse);
    }
}

 

OUTPUT:

10
5

 

In the above example, the result variable is assigned with a true condition so it prints the value of x whereas reuse is assigned with a false statement so it returns the y value

5. Bitwise and BitShift Operators

 

operator

Name

Description

~  

     Unary bitwise complement

compliment operator inverts a bit of pattern

<< 

     Signed left shift

 left shift operator that moves the bits to the left

>> 

     Signed right shift

right shift operator that moves the bits to the right

&  

     Bitwise AND

compares corresponding bits of x and y and generates 1 if both bits are equal else it returns 0.

^  

     Bitwise exclusive OR

compares corresponding bits of x and y and generates 1 if they are not equal else it returns 0.

|  

    Bitwise inclusive OR

 compares corresponding bits of x and y and generates 1 if either bit is 1 else it returns 0. 

 

Program:

public class SampleBitOperator {
    public static void main(String[] args) {
        int x = 10;
        int y = 5;
        int result = 0;
        result = x & y;
        System.out.println("x & y: " + result);
        result = x | y;
        System.out.println("x | y: " + result);
        result = x ^ y;
        System.out.println("x ^ y: " + result);
        result = ~x;
        System.out.println("~x: " + result);
        result = x << 2;
        System.out.println("x << 2: " + result);
        result = x >> 2;
        System.out.println("x >> 2: " + result);
    }
}

 

OUTPUT:

x & y: 0
x | y: 15
x ^ y: 15
~x: -11
x << 2: 40
x >> 2: 2
 
 
 

Related Tutorials

Map