Operators in Java

Introduction

Java provides operators to perform operations on data stored in variables. The characters used to perform operations are called operators, and the variables on which the operation is performed are called operands.

For example, c = a + b;

Here, c = a + b is an expression in which + is an addition operator and a and b are operands. Java supports different types of operators, which you can see in the following sections.

Assignment Operator

The assignment operator = is used to assign the value of the operand on the right to the operand on the left. You can assign a value to multiple variables at the same time.

For example, int a =10; int b = c = 20;

Java also supports compound assignment to assign the result of an expression to a variable.

For example,

int a = 10; a += 6; // this converts to a = a + 6;

Arithmetic Operator

Arithmetic operators work with numeric data. However, they can also be used with character data. They are binary operators, which means that they work on two operands.

Arithmetic operators in Java look like this,

  • Addition (+): Performs the operation of adding operands.
  • Subtraction (-): Performs an operand subtraction operation.
  • Multiplication (*): Performs an operand operation of multiplication.
  • Division (/): Performs a division operand.
  • Module (%): Returns the remainder of the division operation.

The following example shows the use of arithmetic and assignment operators,

public class OperatorsDemo {
  public static void main(String[] args) {
    int add, sub, mul, div, mod;
    add = 3 + 5;
    sub = 6 - 4;
    mul = 2 * 2;
    div = 6 / 3;
    mod = 4 % 2;
    System.out.println("Addition is " + add);
    System.out.println("Subtraction is " + sub);
    System.out.println("Multiplication is " + mul);
    System.out.println("Division is " + div);
    System.out.println("Modulo is " + mod);
  }
}

Output

Addition is 8
Subtraction is 2
Multiplication is 4
Division is 2
Modulo is 0

Note that the modulo operation returns 0 because 4 is directly divisible by 2.

Unary Operator

Unary operators operate on a single operand. Java supports different types of unary operators which are as follows,

  • Unary plus (+): indicates a positive value.
  • Unary minus (-): Negates an expression.
  • Increment (++): Increases the value of a variable by 1.
  • Decrement (–): Decreases the value of a variable by 1.
  • Logical complement (!): Inverts a Boolean value.

There are two ways to use the increment and decrement operators, i.e. prefix or postfix notation. Both increment the value of a variable by 1. However, the prefix version first increments the value and then assigns it, while the postfix version first assigns the value and then increments it.

The following code shows the use of unary operators,

public class OperatorsDemo {
  public static void main(String[] args) {
    int x = 3;
    int y = x++;
    int z = ++x;
    x = -x;
    boolean answer = false;
    answer = !answer;
    System.out.println("Value of y after postfix increment of x is " + y);
    System.out.println("Value of z after prefix increment of x is " + z);
    System.out.println("Value of x after negation is " + x);
    System.out.println("Logical complement of answer is " + answer);
  }
}

Output

Value of y after postfix increment of x is 3
Value of z after prefix increment of x is 5
Value of x after negation is -5
Logical complement of answer is true

Conditional/Relational Operator

Conditional operators work on two operands to test the relationship between them. The result of a conditional operator will either be true or false. This is a list of conditional operators supported in Java,

  • Equal to (==): Returns true if two values are equal, otherwise returns false.
  • Not equal to (!): Returns true if the two values are equal, otherwise returns false.
  • Greater than (>): Returns true if the value on the left is greater than the value on the right, otherwise returns false.
  • Less than (<): Returns true if the value on the left is less than the value on the right, otherwise returns false.
  • Greater than or equal to (>=): Returns true if the value on the left is greater than or equal to the value on the right, otherwise returns false.
  • Less than or equal to (<=): returns true if the value on the left is less than or equal to the value on the left, otherwise returns false.

Logical Operator

Logical operators work on two Boolean expressions. The logical operators supported in Java are as follows,

  • Logical AND (&&): Returns true if both boolean expressions are true, otherwise returns false.
  • Logical OR (||): Returns true if one or both operations are true, otherwise returns false.

Short-circuit behavior

The && logical operator will only check the second expression if the first expression is true. If the first expression is false, it will not check the second expression.

Similarly, the || logical operator will only check the second expression if the first expression is false. If the first expression is true, it will not check the second expression. This is called short-circuit behavior.

The following example shows the use of conditional and logical operators,

public class OperatorsDemo {
  public static void main(String[] args) {
    int num1 = 30,
    num2 = 23;
    // Using conditional operators 
    System.out.println("num1 == num2? " + (num1 == num2));
    System.out.println("num1 != num2? " + (num1 != num2));
    System.out.println("num1 > num2? " + (num1 > num2));
    System.out.println("num1 < num2? " + (num1 < num2));
    System.out.println("num1 >= num2? " + (num1 >= num2));
    System.out.println("num1 <= num2? " + (num1 <= num2));
    // Using logical operators 
    System.out.println("Result of logical AND is " + ((num1 > num2) && (num1 == num2)));
    System.out.println("Result of logical OR is " + ((num1 > num2) || (num1 == num2)));
  }
}

Output

num1 == num2? false
num1 != num2? true
num1 > num2? true
num1 < num2? false
num1 >= num2? true
num1 <= num2? false
Result of logical AND is false
Result of logical OR is true

Ternary Operator

The ternary operator (?:) is used as a short alternative to the if-else solution text. It saves lines of code and makes it clearer.

Syntax:

<expr1> ? <expr2> : <expr3>
  • expr1: An expression that evaluates to true or false. 
  • expr2: An expression that will be executed if expr1 returns true. 
  • expr3: An expression that will be executed if expr1 returns false.
public class OperatorsDemo {
  public static void main(String[] args) {
    int a = 10,
    b = 20;
    String result;
    result = (a > b) ? "a is greater": "b is greater";
    System.out.println(result);
  }
}

Here the expression (a>b) is calculated. Since the expression is evaluated as false, the second statement after the colon (:), i.e., b is greater, is stored in the result variable.

Output

b is greater

Leave a Comment