Open In App

Basic Operators in Java

Java provides a rich operator environment. We can classify the basic operators in java in the following groups:

Let us now learn about each of these operators in detail.

1. Arithmetic Operators: Arithmetic operators are used to perform arithmetic/mathematical operations on operands. 

Note: The increment and decrement operators are called unary arithmetic operators as they work with a single operand whereas the rest of arithmetic operators are called binary arithmetic operators as they operate on two operands.




// Java Program to Illustrate Arithmetic Operators
  
import java.util.*;
  
// Class 1
class A {
  
    // Main driver method
    public static void main(String args[])
    {
  
        int a = 10, b = 4, res;
  
        // printing a and b
        System.out.println("a is " + a + " and b is " + b);
  
        res = a + b; // addition
        System.out.println("a+b is " + res);
  
        res = a - b; // subtraction
        System.out.println("a-b is " + res);
  
        res = a * b; // multiplication
        System.out.println("a*b is " + res);
  
        res = a / b; // division
        System.out.println("a/b is " + res);
  
        res = a % b; // modulus
        System.out.println("a%b is " + res);
    }
}

Output
a is 10 and b is 4
a+b is 14
a-b is 6
a*b is 40
a/b is 2
a%b is 2

2. Relational Operators: The relational operators determine the relationship that one operand has to the other. The relational operators evaluates the relation between two operations and returns true if the relation exists else false. Here we compare two operands like they are equal or not? or which one is greater etc…




// Java program for Relational Operators
  
import java.util.*;
  
class A {
  
    public static void main(String args[])
    {
  
        int a = 10, b = 4;
  
        // relational operators
        // check first operand is greater than the second
        // operand or not
        if (a > b)
            System.out.println("a is greater than b");
        else
            System.out.println(
                "a is less than or equal to b");
  
        // Checking first operand is less than the second
        // operand or not
        if (a < b)
            System.out.println("a is less than b");
        else
            System.out.println(
                "a is greater than or equal to b");
  
        // Check first operand is greater than or equal to
        // the second operand or not
        if (a >= b)
            System.out.println(
                "a is greater than or equal to b");
        else
            System.out.println("a is lesser than b");
  
        // Checking first operand is less than or equal to
        // the second operand or not
        if (a <= b)
            System.out.println(
                "a is lesser than or equal to b");
        else
            System.out.println("a is greater than b");
  
        // check both the operand is equal or not
        if (a == b)
            System.out.println("a is equal to b");
        else
            System.out.println("a and b are not equal");
  
        // Checking one operand is not equal or not
        if (a != b)
            System.out.println("a is not equal to b");
        else
            System.out.println("a is equal b");
    }
}

Output
a is greater than b
a is greater than or equal to b
a is greater than or equal to b
a is greater than b
a and b are not equal
a is not equal to b

3. Bitwise Operators: Java provides several bitwise operators to work with integer types, long, int, short, char, byte. Bitwise operators performs bit-by-bit operation on binary representation of integers. These operators act upon the individual bits of their operands.

For Example:

Assume a = 9 and b = 7.
In binary form,
a = 1001
b = 0111
----------
a & b = 0001
a | b = 1111
a ^ b = 1110

Different bitwise operators available in Java are as follows:

4. Assignment Operator: The assignment operator is used to assign value to a variable. The general form of an assignment operator is:

approach - var = expression

example:

int number = 35;
double db = 3.5456;
String str = "GeeksForGeeks";

Different ways of using assignment operator:

Similarly, we can also use operators like , ^= , &= , |=.

5. Logical Operators: Logical operators perform logical operations like logical AND, logical OR etc. Let us assume that variable a holds the boolean value true and b holds the boolean value false. Below are some logical operators we can use:

Sample truth table with two variables:

6. Other Operators

(Object or reference variable ) instanceof  (class or interface type)

This operator return a boolean value either true or false. If the object on the left side of the operator is of the type of class on the right side of the operator then the operator will true otherwise false.




// Java program for instanceof Operator
  
public class InstanceOf {
  
    // Main driver method
    public static void main(String args[])
    {
  
        String name = "GeeksforGeeks";
  
        // instanceof operator will return true here
        // as the object name is of type String
        boolean res = name instanceof String;
  
        System.out.println(res);
    }
}

Output
true
expression1 ? expression2 : expression3

expression1: expression that evaluates to a boolean value i.e. either true or false.
expression2: if expression1 evaluates to true then expression2 is evaluated
expression3: if expression1 evaluates to false then expression3 is evaluated.




// Java program to illustrate conditional operators
  
class Conditional {
    public static void main(String args[])
    {
        int num1 = 4;
        int num2 = 5;
  
        // using conditional operator
        num1 = num1 > num2 ? num1 - num2 : num1 + num2;
  
        // num1 stores the value after evaluation of either
        // second
        // or third expression depending on the condition
        // provided in the first expression
        // num1 becomes 4+5 = 9
  
        // printing num1
        System.out.println(num1);
    }
}

Output
9

Precedence Chart

Below table shows the precedence order of operators from highest to lowest. Operators in same row have equal precedence.


Article Tags :