Operators in Java

Operators

In the last blog, we learned about the Variables in Java. If you want to learn more about variables visit Variables in Java. In this blog, we will go through the Operators in Java and the types of Operators we use for performing the operations. In general, operators are used for performing some calculations. The same is applicable in Java, operators are used for performing operations on variables like addition, subtraction, etc. There are the following types of operators:

  • Arithmetic Operators(+, -, *, /)
  • Relational Operators(== , >, <,>=, <=, !=)
  • Conditional Operators(&&, ||)
  • Assignment Operators(=, +=, -=, *=, /=, %=, &=, ^=, |=, >>=, <<=, >>>=)
  • Ternary Operators(?, :)
  • Unary Operators(+, -, ++, --, ~, !)
  • Bitwise Operators(&, |, ^, ~, <<, >>, >>>)

Arithmetic Operators

The operators that perform arithmetic operations like addition, subtraction, division, multiplication, etc. are called arithmetic operators. ‘+’ is used for addition, ‘-’ operator is used for subtraction of the two numbers. The ‘*’ operator is used for multiplication. The division operator(/) is used for integer division when having both the arguments as integers and also floating point division can be performed with the help of it. The remainder is denoted by %. If we try to use 0 for the division of integers i.e. 10/0, then it will give us an exception, and floating point division will give us an infinite result or NaN result.

Arithmetic OperatorsDescription
+Performs addition i.e. a+b
-Performs subtraction i.e. a-b
*Performs multiplication i.e. a*b
/Performs division i.e. a/b
%Returns the remainder of the division which is performed a%b (Modulo Operation)

e.g.

public class ArithematicOperatorsEx {
  public static void main(String args[]) {

    //Initialize veriable
    int value  = 10;
    int value1 = 25;

    //res holds final value
    //Addition Operators
    int res = value + value1;
    System.out.println("Sum is= " + res);

    //Subtraction
    res = res - 9;
    System.out.println("Subtraction is= " + res);

    //Multiplication
    res = res * 10;
    System.out.println("Multiplication is = " + res);

    //Modulo
    res = res/4;
    System.out.println("Division is= " + res);

    //Addition
    res = res%2;
    System.out.println("Remainder is= " + res);

    //Pre-Increments
    int val = 0;
    val = ++res;   //res return 1 incremented value(res + 1)
    System.out.println("val= " + val);
    System.out.println("Pre Increments= " + res);

    //Post-Increments
    val = res++;  //res return current value than res value is incremented by 1 (res + 1)
    System.out.println("val= " + val);
    System.out.println("Post Increments= " + res);

    //Pre-Decrements
    val = --res;  //res return 1 decremented value(res - 1)
    System.out.println("val= " + val);
    System.out.println("Pre Decrements= " + res);

    //Post-Decrements
    val = res--;  //res return current value than res value is decremented by 1 (res - 1)
    System.out.println("val= " + val);
    System.out.println("Post Decrements= " + res);
  }
}

Output:

Sum is = 35
Subtraction is   = 26
Multiplication is = 260
Division is = 65
Remainder is = 1
val = 2
Pre Increments = 2
val = 2
Post Increments = 3
val = 2
Pre Decrements = 2
val = 2
Post Decrements = 1

Relational Operators

Relational Operators are used for checking the relationship between the two operands. "==" operator is used for checking an equality. It returns the result in a boolean i.e. either true or false.

e.g.

int a = 10;
int b = 10;
System.out.println("The result is:" + (a==b));

Output:

true

In the above example a==b will return true value. In order to check inequality we use the != operator.

e.g.

int a = 10;
int b = 5;
System.out.println("The result is:" + (a!=b));

Output:

true

Here it will check whether a is not equal to b which is true, so it will return true.

In order to check which number is smaller than / greater than / equal to / greater than equal / smaller than equal amongst the two, we use the "<", ">", "==", "<=", ">=" operator.

e.g.

int a = 20;
int b = 90;
if(a < b) {
  System.outprintln("a is smaller than b");
} else {
  System.outprintln("b is greater than a");
}

Output:

a is smaller than b
Relational OperatorsDescription
==Is Equal to e.g. if(10 == 10) condition will be true
<Less than e.g. if(9 < 10) condition will be true
>Greater Than e.g. if(8 > 7) condition will be true
<=Less Than or Equal To e.g. if(20 <= 40) condition will be true
>=Greater Than or Equal To e.g. if(10 >= 10) condition will be true
!=Not Equal To e.g if(10 != 10) condition will be false

e.g.

public class DevRetationalOperatorsEx {
  public static void main(String args[]) {
    int a = 10;
    int b = 50;
    int c = 10;

    //Is Equal to
    if(a == c){
        System.out.println(a == c);
    }

    //Less than
    if(a < b){
        System.out.println(a < b);
    }

    //Less Than or Equal To
    if(a <= c){
        System.out.println(a <= c);
    }

    //Greater Than
    if(b > c){
        System.out.println(b > c);
    }

    //Greater Than or Equal To
    if(c >= a){
        System.out.println(c >= a);
    }

    //Not Equal To
    if(a != b){
        System.out.println(a != c);
    }
  }
}

e.g.

true
true
true
true
true
false

Conditional Operators

Logical Operators are used to perform conditional operations like Conditional AND, OR, NOT on two boolean expressions. AND operator returns true if and only if both the conditions satisfy, otherwise it returns false. OR operator returns true even if one of the two conditions satisfies.

Logical OperatorsDescription
&&Conditional AND
||Conditional OR

e.g.

public class LogicalOperatorsEx {
  public static void main(String args[]) {
    int x = 10;
    int y = 20;
    int z = 10;

    if((x==z) && (y!=10)){
        System.out.println((x==z) && (y!=10));
    }

    if((x==z) || (x==y)){
          System.out.println((x==z) || (y!=10));
    }
  }
}

Output:

true
true

Assignment Operator

Assignment operator is the most commonly used operator. It is mostly used for assigning the values to the variables. On the left side of the = operator we write a variable name followed by the operator i.e. = and then on the right side, we assign the values to that variable. We can also combine two operators i.e. arithmetic operator and assignment operator which will give us compound assignments for example +=, -=, *=, /=, %=, &=, ^=, |=, <<=,>>=, >>>=, etc.

e.g.

public class AssignmentOperatorEx {
  public static void main(String args[]) {

    int xnumber = 10;
    System.out.println(" =   " + xnumber);

    //Compound Assignments +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=
    xnumber += xnumber;
    System.out.println("+=   " + xnumber);
    xnumber -= (xnumber - 10);
    System.out.println("-=   " + xnumber);
    xnumber *= xnumber;
    System.out.println("*=   " + xnumber);
    xnumber /= xnumber - 90;
    System.out.println("/=   " + xnumber);
    xnumber %= xnumber + 100;
    System.out.println("%=   " + xnumber);
    xnumber &= xnumber;
    System.out.println("&=   " + xnumber);
    xnumber ^= xnumber + 2;
    System.out.println("^=   " + xnumber);
    xnumber |= xnumber;
    System.out.println("|=   " + xnumber);
    xnumber <<= xnumber;
    System.out.println("<<=  " + xnumber);
    xnumber >>= xnumber;
    System.out.println(">>=  " + xnumber);
    xnumber >>>= xnumber;
    System.out.println(">>>= " + xnumber);
  }
}

Output:

=   10
+=   20
-=   10
*=   100
/=   10
%=   10
&=   10
^=   6
|=   6
<<=  384
>>=  384
>>>= 384

Ternary Operators

Ternary Operator is the only operator which accepts three operators. It is the replacement for if else statement and is used in Java mostly. It makes the code easy, and short because the statement ends in one line only.

Syntax:

datatype variablename = (condition) ? expression1 : expression2

Here if the condition will be true then it will execute the first expression i.e. expression1, but if the condition fails then it executes the second expression i.e. expression2.

e.g.

import java.io.*;
class TernaryOperatorEx {
  public static void main(String[] args){
          int num1 = 50, num2 = 90;
          System.out.println("The numbers are: " + num1 + num2);
          String res = (num1 > num2)? num1 + "is greater than"+ num2 : num2 + "is greater than" + num1;
          System.out.println("Result = " + res);
  }
}

Output:

The numbers are: 50 90
Result = 90 is greater than 50

Unary Operators

Unary operator performs the operation on one operand only. It can perform the increment, decrement operation on the value by one, can invert the values of the boolean, etc. Unary Operators are : +, -, ++, --, ~, !.

e.g.

public class UnaryOperatorsEx {
  public static void main(String args[]) {

    //Initialize veriable
    int value1    = 10;
    int value2 = -25;

    //Unary Minus - Convert to -ve number
    int res = -value1;
    System.out.println("Unary Minus = " + res);

    //Logical Complement Operator
    boolean bres = !true;
    System.out.println("Complement =    " + bres);

    res = 50;

    //Pre-Increments
    int val = 0;
    val = ++res;   //res return 1 incremented value(res + 1)
    System.out.println("val = " + val);
    System.out.println("Pre Increments = " + res);

    //Post-Increments
    val = res++;  //res return current value than res value is incremented by 1 (res + 1)
    System.out.println("val = " + val);
    System.out.println("Post Increments  = " + res);

    //Pre-Decrements
    val = --res;  //res return 1 decremented value(res - 1)
    System.out.println("val = " + val);
    System.out.println("Pre Decrements = " + res);

    //Post-Decrements
    val = res--;  //res return current value than res value is decremented by 1 (res - 1)
    System.out.println("val = " + val);
    System.out.println("Post Decrements  = " + res);
  }
}

Output:

Unary Minus = -10
Complement = false
val = 51
Pre Increments = 51
val = 51
Post Increments = 52
val = 51
Pre Decrements = 51
val = 51
Post Decrements = 50
Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.