DEV Community

Nanthini Ammu
Nanthini Ammu

Posted on

Operators

What are Operators in Java?

Operators are symbols used to perform operations on variables and values.
Enter fullscreen mode Exit fullscreen mode

How many types of Operators available in Java?

  1. Arithmetic Operators
  2. Assignment Operators
  3. Relational Operators
  4. Logical Operators
  5. Unary Operators
  6. Bitwise Operators

What are Arithmetic Operators?

- Arithmetic operators are used to perform arithmetic operations like Addition,Subtraction, Multiplication, Division, and Modulo on variables and data. Following are the list of arithmetic operators.
Enter fullscreen mode Exit fullscreen mode

For Addition + operator is used.
For Subtraction - operator is used.
For Multiplication ***** operator is used.
For Division / operator is used.
For Modulo % Operator (Remainder after division) is used.

What are the priority levels of arithmetic operation in Java?

  • There are two priority levels of arithmetic operation in java.
    1. High priority ⇒ * / %
    2. Low priority ⇒ + –

Example :

public class Operator {
public static void main(String args[])
{
int a = 13,b = 5;

    // addition operator
    System.out.println("a + b = " + (a + b));

    // subtraction operator
    System.out.println("a - b = " + (a - b));

    // multiplication operator
    System.out.println("a * b = " + (a * b));

    // division operator
    System.out.println("a / b = " + (a / b));

    // modulo operator
    System.out.println("a % b = " + (a % b));
}
Enter fullscreen mode Exit fullscreen mode

}

Output :
a + b = 18
a - b = 8
a * b = 65
a / b = 2
a % b = 3

What is Assignment Operator?

- Assignment operators are used to assign values to variables.
Enter fullscreen mode Exit fullscreen mode

Example :

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

    int a = 5;
    int b = 13;
    int var;

    // assign value using =
    var = b;
    System.out.println("var using =: " + var);

    // assign value using +=
    var = b;
    var += a;  //var = var+a;
    System.out.println("var using +=: " + var);

    // assign value using -=
    var = b;
    var -= a; //var = var-a;
    System.out.println("var using -=: " + var);

    // assign value using *=
    var = b;
    var *= a; //var = var*a;
    System.out.println("var using *=: " + var);

    // assign value using /=
    var = b;
    var /= a; //var = var/a;
    System.out.println("var using /=: " + var);

    // assign value using %=
    var = b;
    var %= a; //var = var%a;
    System.out.println("var using %=: " + var);
}
Enter fullscreen mode Exit fullscreen mode

}

Output :
var using =: 13
var using +=: 18
var using -=: 8
var using *=: 65
var using /=: 2
var using %=: 3

What are Relational Operators?

- Relational operators are used to compare two values and give result in BOOLEAN data type.
Enter fullscreen mode Exit fullscreen mode
  • == equal to(3 == 5 returns false)
  • != not equal to(3 != 5 returns true)
  • > greater than(3 > 5 returns false)
  • < less than(3 < 5 returns true)
  • >= greater than or equal to(3 >= 5 returns false)
  • <= less than or equal to(3 <= 5 returns true)

Example :
public class Operator {
public static void main(String args[])
{

    int a = 7, b = 11;

    System.out.println("a is " + a + " and b is " + b);

    // == operator
    System.out.print("is "+a +" equal to "+b+"? ");
    System.out.println(a == b);  // false

    // != operator
    System.out.print("is "+a +" not equal to "+b+"? ");
    System.out.println(a != b);  // true

    // > operator
    System.out.print("is "+a +" greater than "+b+"? ");
    System.out.println(a > b);  // false

    // < operator
    System.out.print("is "+a +" less than "+b+"? ");
    System.out.println(a < b);  // true

    // >= operator
    System.out.print("is "+a +" greater than or equal to "+b+"? ");
    System.out.println(a >= b);  // false

    // <= operator
    System.out.print("is "+a +" less than or equal to "+b+"? ");
    System.out.println(a <= b);  // true
}
Enter fullscreen mode Exit fullscreen mode

}

Output :
a is 7 and b is 11
is 7 equal to 11? false
is 7 not equal to 11? true
is 7 greater than 11? false
is 7 less than 11? true
is 7 greater than or equal to 11? false
is 7 less than or equal to 11? true

Unary Operators :

  1. ++ Increment operator
  2. -- Decrement operator
  3. ++a Preincrement operator
    • If you use the ++ operator as a prefix like: ++a, the value of a is incremented by 1; then it returns the value.
  4. --a Predecrement operator
    • If you use the -- operator as a prefix like: --a, the value of a is decremented by 1; then it returns the value.
  5. a++ postincrement operator
    • If you use the ++ operator as a postfix like: a++, the original value of a is returned first; then a is incremented by 1.
  6. a-- postdecrement operator
    • If you use the -- operator as a postfix like: a--, the original value of a is returned first; then a is decremented by 1.

Example :

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

    //post increment
    int a = 10;
    System.out.println(a++); //output is 10
    System.out.println(a);   //output is 11

    //post decrement
     a = 10;
    System.out.println(a--); //output is 10
    System.out.println(a);   //output is 9

    //pre increment
     a = 10;
    System.out.println(++a);  //output is 11

    //pre decrement
    a = 10;
    System.out.println(--a);  //output is 9


}
Enter fullscreen mode Exit fullscreen mode

}

Excercise Task:
public class Operator {
public static void main(String args[])
{

    int a =10;
    int b = 20;
    int c = 30;
    int result = a++ + --a - c++ - --c + a++; // 10 + 10 - 30 - 30 + 10
    System.out.println(result);


}
Enter fullscreen mode Exit fullscreen mode

}

Output : -30

Top comments (0)