Operators

Operators are special kind of functions defined in Java programming languages. There are different types of them.

Arithmetic Operators

As can be guessed from their name, this group of operators represents some standard math operations:

  • +(add) – performs addition of left and right operand and returns the result;
  • -(subtract) – subtracts right operand from the left one and returns the result;
  • /(divide) – divides left operand by right operand and returns the result. Using 0 as right operand leads to exception at runtime. Division of two integers (short, int, long) leads to cutting away the fraction part (3/2=1);
  • *(multiply) – multiplies left operand on right operand and returns the result;
  • %(divide with remainder) – divides left operand by right operand and returns the remainder of division (5/3=2);
        int a = 7;
        int b = 2;
        System.out.println("a=" + a + ", b=" + b);

        //add
        int additionResult = a + b;
        System.out.println("a+b=" + additionResult);

        //subtract
        int subtractionResult = a - b;
        System.out.println("a-b=" + subtractionResult);

        //divide
        int divisionResult = a / b;
        System.out.println("a/b=" + divisionResult);

        //throws ArithmeticException
        //int divisionByZero = a/0;
        
        //multiply
        int multiplicationResult = a * b;
        System.out.println("a*b=" + multiplicationResult);

        //divide with remainder
        int remainder = a % b;
        System.out.println("a%b=" + remainder);

Assignment Operators

Here are the following operators in this category:

  • = – assignment operator assigns value of the right operand to the left operand;
  • += – shortcut for addition and assignment (x = x + 5 is the same as x+=5);
  • -= – shortcut for subtraction and assignment (x = x – 5 is the same as x-=5);
  • *= – shortcut for multiplication and assignment (x = x * 5 is the same as x*=5);
  • /= – shortcut for division and assignment (x = x / 5 is the same as x/=5);
  • %= – shortcut for division with remainder and assignment (x = x % 5 is the same as x%=5);
        //assign
        int e = 5;
        System.out.println("e=" + e);

        //add and assign
        e += 3;
        System.out.println("After adding 3 e=" + e);

        //subtract and assign
        e -= 2;
        System.out.println("After subtracting 2 e=" + e);

        //multiply and assign
        e *= 2;
        System.out.println("After multiplying by 2 e=" + e);

        //divide and assign
        e /= 3;
        System.out.println("After dividing by 3 e=" + e);

        //divide with remainder and assign
        e %= 2;
        System.out.println("After dividing by 2 remainder is e=" + e);

Bitwise Operators

Bitwise operators are responsible for actions on bits (those 1-s and 0-s).

  • >> – moves all the bits of the left operand for positions specified by the right operand to the RIGHT – method of fast division of right operand by 2left operand . For better understanding of this operation you just have represent number and binary form and move all it’s numbers to the right (far left element always becomes zero far right element always disappears). For example 1000 >> 2 = 0010 (In decimal 8 is converted to 2).
  • << – moves all the bits of the left operand for positions specified by the right operand to the LEFT – method of fast multiplication of right operand by 2left operand
  • >>> – moves all the bits of the left operand for positions specified by the right operand to the RIGHT (sign bit is also moved)
        int h = 8;

        //bitwise shift right
        int bitwiseShiftRightResult = h >> 1;
        System.out.println("Result of bitwise move right on h=8 is " + bitwiseShiftRightResult);


        //bitwise shift left
        int bitwiseShiftLeftResult = h << 1;
        System.out.println("Result of bitwise move left on h=8 is " + bitwiseShiftLeftResult);

        int e = -8;

        //bitwise shift right with sign
        var bitwiseShiftRightWithSign = e >>> 1;
        System.out.println("Result of bitwise move right with sign on e=-8 is "+ bitwiseShiftRightWithSign);

Comparison Operators

Comparison operators return boolean values.

  • == – checks if primitive types have same values or for reference types (Objects) if they they refer the same Object (for object type value comparison prefer method equals());
  • > – checks if left operand has higher value than the right one;
  • < – checks if left operand has lower value than the right one;
  • >= – checks if left operand has higher value than the right one or they are equal;
  • <= – checks if left operand has lower value than the right one or they are equal;
        int f = 6;
        int g = 7;

        //equals
        boolean equalResult = f == g;
        System.out.println("Result of 6==7 is " + equalResult);

        //greater than
        boolean greaterThanResult = f > g;
        System.out.println("Result of 6>7 is " + greaterThanResult);

        //less than
        boolean lessThanResult = f < g;
        System.out.println("Result of 6<7 is " + lessThanResult);

        //greater or equals
        boolean greaterOrEqualsResult = f >= g;
        System.out.println("Result of 6>=7 is " + greaterOrEqualsResult);

        //less or equals
        boolean lessOrEqualsResult = f <= g;
        System.out.println("Result of 6<=7 is " + lessOrEqualsResult);

Logical Operators

Logical operators are used for combining multiple boolean expressions(values) to get single boolean value.

  • & – and or conjunction. Returns true if both left and right operands are true. If any of them or both are false – returns false;
  • | – or or disjunction. Returns true if any of the operands or both is true. If both of them are false returns false;
  • ^ – NOR. Returns true only if none of the operands is true. Otherwise if one of operands or both are true returns false;
  • && – same as &, but if left operand is false (that means expression can’t return true) the second operand is not evaluated. That is very useful if instead of variables your have methods that return boolean value. For example: x && heavyMethod() – heavyMethod won’t even be called if x is false, cause it doesn’t make sense.
  • || – same as |, but if left operand is true the second operand is not evaluated.
        boolean i = true;

        boolean j = false;

        //bitwise AND
        boolean bitwiseAndResult = i & j;
        System.out.println("Result of " + i + " AND " + j + " is " + bitwiseAndResult);

        //bitwise OR
        boolean bitwiseOrResult = i | j;
        System.out.println("Result of " + i + " OR " + j + " is " + bitwiseOrResult);

        //bitwise NOR
        boolean bitwiseNorResult = i ^ j;
        System.out.println("Result of NOT " + i + " OR NOT " + j + " is " + bitwiseNorResult);

        //logical AND
        boolean andResult = i && j;
        System.out.println("Result of " + i + " AND " + j + " is " + andResult);

        //logical OR
        boolean orResult = i || j;
        System.out.println("Result of " + i + " OR " + j + " is " + orResult);

Postfix Operators

That operators have only one operand and are applied after the value is returned.

        int c = 1;

        //increment
        c++;
        System.out.println("c after increment " + c);

        //decrement
        c--;
        System.out.println("c after decrement " + c);

UnaryOperators

That operators (also called prefix) have only one operand and are applied before the value is returned.

        int d = 4;

        //increment
        ++d;
        System.out.println("d after increment " + d);

        //decrement
        --d;
        System.out.println("d after decrement " + d);

Difference between postfix and prefix

Those operators have no difference when they are just called as expression as in the examples above. But if result of the expression is assigned to a variable here comes the difference. Using postfix operator means that value will be first assigned to a variable and than (incremented/decremented). Using prefix operator means that value would first be incremented(decremented) and than assigned to a variable. For code clearness and readability consider using prefix/postfix increment/decrement on the other line than variable assignment (as shown below in comments with heading “same result”). Mixing assignment and postfix/prefix operator is good topic for interviews, but should not appear in the production code.

        //postfix
        int i = 4;
        int m = i++;

        //same result
        //int i = 4;
        //int m = i;
        //i++;

        //prefix
        int j = 4;
        int n = ++j;

        //same result
        //int j = 4;
        //j++;
        //int n = j;

        System.out.println("i = " + i + ", j = " + j + ", m = " + m + ", n = " + n);

Precision

If you use arithmetic operators on different types, the type with lower precision gets automatically cast to the type with higher precision int -> long, int -> float, int -> double, long -> double, float -> double. If you would like to get the result of higher precision than the both values are just cast one of them to desired type.

        int a = 7;
        int b = 2;
        //divide
        float divisionResult = (float) a / b;
        System.out.println("a/b=" + divisionResult);

Source code can be found here.

Scroll to Top