Java Operators
Java Operators

Java Operators

An operator in Java is a symbol that is used to perform operations. Operators are used to perform operations on variables and values. Example: +, -, *, /, %, etc.

We can divide all the Java Operators into the following groups:

1.    Arithmetic Operators
2.    Relational Operators
3.    Unary Operators
4.    Bitwise Operator
5.    Shift Operators
6.    Logical Operators
7.    Assignment Operators
8.    Ternary Operators

1. Arithmetic Operators
Arithmetic operators are used in mathematical expressions. Such as addition, subtraction, multiplication, division, etc.

List of arithmetic operators


Example program to compute the addition, subtraction, multiplication, and division of two numbers.

Code:

public class OperatorDemo1{
  public static void main(String[] args){
    double num1 = 12.0;
    double num2 = 3.0;
    System.out.println(“Num1 = “+num1);
    System.out.println(“Num2 = “+num2);
    //compute addition
    double add = num1 + num2;
    //print addition
    System.out.println(“Addition = “+add);
    //compute subtraction
    double subtract = num1 – num2;
    //print subtraction
    System.out.println(“Subtraction = “+subtract);
    //compute multiplication
    double multiply = num1 * num2;
    //print multiplication
    System.out.println(“Multiplication = “+multiply);
    //compute division
    double division = num1 / num2;
    // compute division
    System.out.println(“Division = “+division);
  }
}

Save this program as ‘OperatorDemo1.java’
Compile: $javac OperatorDemo1.java
Execute: $java OperatorDemo1

Output:

Num1 = 12.0
Num2 = 3.0
Addition = 15.0
Subtraction = 9.0
Multiplication = 36.0
Division = 4.0

Note: All the arithmetic operators are binary since they operate on two operands at a time.

2. Relational Operator

These operators are used to check for relations like equality, greater than, less than, etc. they return Boolean result after the comparison and are extensively used in looping as well as conditional if-else statements.

Syntax: variable relational_operator value


List of relational operators

3. Unary Operators

Unary operators need only one operand. They are used for increment, decrement or negate a value.

++: Increment operators: In Java programming language ++ operators are called increment operators. There are two types of increment operators:

à Prefix increment operators
à Postfix increment operators

Prefix increment operators
These operators add one value to the operands. This means this operator helps us to increase one value to the particular operand/variable.

Syntax: ++var
Example: var num = 5
++num
1 + num
1 + 5 = 6

Postfix increment operator
These operators can also increase one value to the operand. But after executing the statement.

Syntax: var++
Example: var num = 10
num++
10 + 1 = 11

--: Decrement operators: In Java programming language -- operators are called decrement operators. There are two types of decrement operators:

à Prefix decrement operators
à Postfix decrement operators

Prefix decrement operators
These operators help us to decrease one value to the operand/variable before executing the statement.

Syntax: --var
Example: var x = 5
--x
1 - x = 1 - 5 = 4

Postfix decrement operators
These operators help us to decrease one value to the operand/variable after executing the statement.

Syntax: var++
Example: var x = 5
x--
x - 1 = 5 - 1 = 4

–: Unary minus, used for negating the values.

+: Unary plus, used for giving positive values. Only used when deliberately converting a negative value to positive.

4. Bitwise Operators

Bitwise operators are used to perform manipulation of individual bits of a number. They can be used with any of the integral types (char, int, short, etc).
This would enable us to perform machine-level operations. Bitwise operators are used in the bit-level programming.

Java provides four bitwise operators to perform bit-level operations.










5. Shift Operators
These operators are used to shift the bits of a number left/right thereby multiplying/dividing the number by two respectively.
Java programming language provides three-bit shift operators.









6. Logical Operators
The logical operators work on the Boolean operand. It’s also called “Boolean Logical Operators”. It operates on two Boolean values as a result.

There are three types of logical operators in Java:























Truth table: Logical AND (&&)












Truth table: Logical OR (||)




Truth table: Logical NOT (!)








7. Assignment Operators
The assignment operators are represented as “equal sign” (=). The assignment operators are used in Java to assign values to the variables.
The assignment operator assigns the value on its right to the variable on its left.
















8. Ternary Operator
The ternary operator is also known as the “Conditional Operator”. This operator consists of three operands and it is used to evaluate Boolean expressions.

Syntax:  Variable = (expression)? value if true: value if false;

Example program of ternary operator

public class OperatorDemo2{
  public static void main(String[] args){
    int num1 = 50;
    int num2 = 40;
    String result;

    System.out.println("First number: "+num1);
    System.out.println("Second number: "+num2);

    result = (num1>num2) ? "The first number is greater than second." : "The second number is greater than first.";

    System.out.println(result);
  }
}

Save this file as ‘OperatorDemo2.java’

Compile: $javac OperatorDemo2.java
Execute: $java OperatorDemo2

Output:

First number: 50
Second number: 40
The first number is greater than second.