Operators in Java

Operators In Java

Operators are used to performing mathematical and logical operations on the variables. You can use different operator for a different purposes. Some operator examples are + - * / etc.

Arithmetic Operator

It is used to perform common mathematical operations. They are:

  • + : For Addition
  • - : For Subtraction
  • * : For Multiplication
  • / : For Division
  • % : For Remainder After Division.
  • ++ : Increase Value By 1. For e.g a++;
  • -- : Reduce Value By 1. For e.g a–;
public class Main {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 5;

        int sum = num1 + num2;
        int diff = num1 - num2;
        int mul = num1 * num2;
        int div = num1 / num2;
        int mod = num1 % num2;

        // Printing Info
        System.out.println("The sum is "+sum);
        System.out.println("The diff is "+diff);
        System.out.println("The mul is "+mul);
        System.out.println("The div is "+div);
        System.out.println("The mod is "+mod);
    }
}
Show Output

Assignment Operators

It is used to assign some values to variables. Here we are assigning 24 to age variable.

int age = 24;
  • += adds a value to a variable,
  • -= reduces a value from a variable,
  • *= multiply a value by a variable, and
  • /= divide a value by a variable.
public class Main
{
  public static void main (String[]args)
  {
    int age = 24;
      age += 1;
      System.out.println ("Age is " + age);
  }
}
Show Output
public class Main
{
  public static void main (String[]args)
  {
    int age = 24;
    age-= 1;
    System.out.println("Age is "+age);
  }
}
Show Output
public class Main
{
  public static void main (String[]args)
  {
    int age = 24;
    age*= 2;
    System.out.println("Age is "+age);
  }
}
Show Output
public class Main
{
  public static void main (String[]args)
  {
    int age = 24;
    age/= 2;
    System.out.println("Age is "+age);
  }
}
Show Output

Comparison Operators

It is used to compare values.

  • == is used to check equal to.
  • != is used to check not equal to.
  • > is used to check greater than.
  • < is used to check less than.
  • >= is used to check greater than or equal to.
  • <= is used to check less than and equal to.
public class Main {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 5;

        // Printing Info
        System.out.println(num1 == num2); // print false.
        System.out.println(num1 < num2); // print false.
        System.out.println(num1 > num2); // print true.
        System.out.println(num1 <= num2); // print false.
        System.out.println(num1 >= num2); // print true.
    }
}
Show Output

Logical Operators

It is used to compare values.

  • && This is and. Return true if all conditions are true.
  • || This is or. Return true if one of the statements is true.
  • ! This will reverse the result. Return false if the result is true.
public class Main {
    public static void main(String[] args) {
        int userid = 123;
        int userpin = 456;

        // Printing Info
        System.out.println((userid == 123) && (userpin== 456)); // print true
        System.out.println((userid == 1213) && (userpin== 456)); // print false.
        System.out.println((userid == 123) || (userpin== 456)); // print true.
        System.out.println((userid == 1213) || (userpin== 456)); // print true.
        System.out.println((userid == 123) != (userpin== 456)); // print true.
    }
}
Show Output