Operators in Java

An operator in java is a symbol that is used to perform operations on variables and values. The value is called an operand, while the operation is defined by an operator. Operands are nothing but the values or variables are undergoing operations

The type of the operator and its operands determines the kind of operation performed on the operands and the type of the result produced.

Java provides a rich set of operators environment. Java operators can be divided into the following categories:

Arithmetic operators in Java

Arithmetic operators help in mathematical calculation results of operations between the values. Arithmetic operations in Java take two operands and perform a calculation on them.

All the Arithmetic Operators take two values to perform any operation.

  • value1 + value2 : + adds two given values and returns the result, it is internally calls value1.plus(value2).
  • value1 - value2 : - subtracts the value2 from the value1, this function internally calls value1.minus(value2)
  • value1 * value2 : value1 will be multiplied by value2, internally calls value1.times(value2)
  • value1 / value2 : value1 will be divided by value2, internally calls value1.div(value2)
  • value1 % value2 : value1 will be divided by value2, and this method return the remainder, this internally calls value1.rem(value2)
  • value1..value2 : it will generate the numbers from the value1 to value2, this is range function and internally it calls value1.rangeTo(value2)
    public class Test {
       public static void main(String args[])
       {
               int a = 9;
            int b = 2;
            System.out.println("The addition value is : " +(a+b));
            System.out.println("The subtraction value is : " +(a-b));
            System.out.println("The multiply value is : " +(a*b));
            System.out.println("The divided value is : " +(a/b));
            System.out.println("The remainder value is : " +(a%b));
       }
    }​

java-operators-arithmetic

Comparison Operators in java

Comparison Operators compares the values of the variables or objects; we can also compare the reference of the objects.

All below operators compares value1 and value2

  • value1 > value2 : if value1 is greater than value2 then > operator returns true
  • value1 < value2 : if value1 is less than value2 then < operator returns true
  • value1 >= value2 : if value1 is greater than or equal to value2 then >= operator returns true,
  • value1 <= value2 : if value1 is lesser than or equal to value2 then <= operator returns true,
  • value1 == value2 : value1 is equal to value2
public class Test {
   public static void main(String args[])
   {
           int value1 = 9;
        int value2 = 2;
        System.out.println("value1 = " + value1+" and value2 = " +value2);
        System.out.println("value1 > value2: " + (value1>value2));
        System.out.println("value1 < value2: " + (value1<value2));
        System.out.println("value1 >= value2: " + (value1>=value2));
        System.out.println("value1 <= value2: " + (value1<=value2));
        System.out.println("value1 == value2: " + (value1==value2));  
   }
}

comparison-operators-in-java

Assignment Operators in Java

The assignment operator ('=') is used to assign values to the variables; Assignment operators are used to assigning value to a variable.

In the below code, we are assigning karthiq to name.

String name = "karthiq"; 

We can use the arithmetic operators along with assignment operators, Below example shows only foe addition operation, but you can understand the operation.

a += b  => a = a + b
  public static void main(String args[])
   {
           String name = "karthiq"; 
        int a = 9;
        int b = 2;
        System.out.println("Using Arithmetic Operator along with Assignment");
        a += b;
        System.out.println("The addition value is : " +a);
   }

assignment-operators

Logical Operators in Java

Logical Operators also called boolean operators; Logical Operators result in boolean values based on the boolean expressions.

There are two logical operators in Java: || and &&; logical operators accept two values. || true if either of the Boolean expression is true

&& true if all Boolean expressions are true At least one value is true then || operator will return true

OR Operator

true || true => true

true || false => true

false || true => true

false || false => false

Both values must be true for && operator will return true otherwise false.

AND Operator

true && true => true

true && false => true

false && true => true

false && false => false

You can combine both operators in the same expression, it is better to surround the unit logic with ()

var a =false

var b = true

(a || b) && (a&&b)

Java Unary Operators

The Java unary operators require only one operand. Unary operators are used to performing various operations, i.e.:

  • incrementing/decrementing a value by one
    ++ Increment operator; increments value by 1
    -- decrement operator; decrements value by 1
  • negating an expression
    + Unary plus (not necessary to use since numbers are positive without using it)
    - Unary minus; inverts the sign of an expression
  • inverting the value of a boolean
    ! Logical complement operator; inverts the value of a boolean
public class Test {
   public static void main(String args[])
   {
           double number = 7.7;
        boolean flag = false;
        
        System.out.println("+number = " + (+number));
        // number is equal to 7.7 here.
        
        System.out.println("-number = " + (-number));
        // number is equal to 7.7 here.
        
           // ++number is equivalent to number = number + 1
        System.out.println("number = " + (++number));
           // number is equal to 8.7 here.
        
           // -- number is equivalent to number = number - 1
        System.out.println("number = " + (--number));
           // number is equal to 6.7 here.
        
        System.out.println("!flag = " + (!flag));
        // flag is still false.
   }
}​

unary-operators-java

Ternary Operator in Java

Java ternary operator is the only conditional operator that takes three operands. Java ternary operator is a one-liner replacement for if-then-else statement and used a lot in java programming.

The first operand in java ternary operator should be a boolean or a statement with the boolean result. If the first operand is true, then java ternary operator returns second operand else it returns the third operand.

Syntax of java ternary operator is:

result = testStatement ? value1 : value2;
class TernaryOperatorExample {
    public static void main(String args[]) {
        int a = 2;
          int b = 5;
          int min = (a < b) ? a : b;
          System.out.println(min);
    }
}

You can perform not only arithmatic comparisons but also object-related things. For example, you might want to call a method if the object not null, but if the object is null, then you might need to assign an object.

public class Test {
    static Test t;
    public boolean someMethod() {
        System.out.println("Example of ternary operator");
        return true;
    }
    public static void main(String args[])
    {
           boolean result = (t!=null)? t.someMethod(): new Test().someMethod();
    }   
}

ternary-operator-java

index Operator in Java

index operator is used to getting a particular value in an array, or to set the values by referencing the specific index.

In Java as well the index starts from the 0, so if you have 5 elements in the array, the array will contain index till 4 as it starts from 0.

public static void main(String args[])
    {
           int[] arr = {1, 2, 3, 4, 5};
        arr[2] = 989; // set the value for the element which is at index 2
        System.out.println(arr[2]); // return value 989
    }
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions