Java operators

In this lesson, you will learn what operators are in Java and what types exist.

An operator is a symbol that usually represents an action or process.

There are many types of operators in Java:

  • Unary operators
  • Arithmetic operators
  • Relation operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Ternary operators

Unary Operators

Java unary operators are the types that need only one operand to perform any operation like increment, decrement, negation, etc.

Operator Description Example
+ Indicates positive value +a
Negates an expression value (Converts positive number to negative) -a
++ Increments a value by 1 ++a
Decrements a value by 1 –a
! Inverts the value of a boolean type !true gives false
%= Assigns the remainder to the left operand when dividing the left and right operands c %= a is equivalent to c = (c % a)

Arithmetic operators

The basic arithmetic operators in Java correspond to the mathematical operations of addition, subtraction, multiplication, and division with the notations +, , *, and /.

These operators can be applied to values ​​of any numeric type (byte, short, int, long, float, and double).

Operator Description Example
+ A binary operator that returns the sum of two numbers a + b
A binary operator that returns the result of subtraction of a two numbers a – b
* A binary operator that returns the result of multiplication of a two numbers a * b
/ A binary operator that returns the result of a division of a two numbers a / b
% A binary operator that gives the remainder when dividing the two values a % b
++ A unary operator that gives a value increased by 1 a++

Relational operators

Relational operators are used to compare the values ​​of any numeric type or char type and test whether two values are equal or not equal or less than or greater than etc. 

These operators return true or false (boolean type).

Operator Description Example
== Checks if two values ​​are equal and if so returns true, otherwise false (a == b)
!= Checks if the two values ​​are different and if so returns the value true, otherwise false (a != b)
> Checks if the left value is greater than the right value and if so returns the value true, otherwise false (a > b)
< Checks if the left value is less than the right value and if so returns the value true, otherwise false (a < b)
>= Checks if the left value is greater than or equal to the right value and if so returns the value true, otherwise false (a >= b)
<= Checks if the left value is less than or equal to the right value and if so returns the value true, otherwise false (a <= b)

Logical operators

Logical operators are used to check conditional expression.

Operator Description Example
&& Returns true only if both expressions (a and b) are true a && b
|| Returns true when at least one of the expressions is true, otherwise false a || b
! Negation – a unary logical operator that returns true when applied to an operand that has a value of false and vice versa !(a && b)

Bitwise operators

Bitwise operators are used to perform operations bit by bit. Can be applied to the types int, long, short, char and byte.

Operator Description Example
& Overwrites units from the binary record to the result if they exist in both operands (a & b)
| Overwrites units from a binary record to the result if they exist in at least one operand (a | b)
^ Rewrites units from a binary record to the result if they exist in only one operand, not in another (a ^ b)
~ Gives the inverse binary record of a given number, where the units are in the result writes zeros and where they are zeros in the result writes the units (~a)
<< Left Shift – Using this operator, the bits of the first operand are shifted to the left by the number of positions specified as the second operand. (a << b)
>> Right shift – by using this operator, the bits of the first operand are moved to the right by the number of positions specified as the second operand. (a >> b)
>>> Right shift – by using this operator, the bits of the first operand are moved to the right by the number of positions specified as the second operand. (a >>> b)

Assignment operators

Java assignment operators are one of the most common operators.

The assignment operator assigns the value on its right to the operand on its left. It is marked with the symbol =.

For most binary operators, there are corresponding complex assignment operators + =– =/ =,% =& =, etc.

Operator Description Example
= Assigns the value of the operand or expression on the right to the left operand. c = (a + b)
+= Assigns a value of the sum of the left and right operands to the left operand c += a is equivalent to c = (c + a)
-= Assigns to the left operand the result of subtraction of left and right operands c -= a is equivalent to c = (c – a)
*= Assigns to the left operand the result of multiplication of left and right operands c *= a is equivalent to c = (c * a)
/= Assigns the left operand the value of the integer division of the left and right operands c /= a is equivalent to c = (c / a)
%= Assigns the remainder to the left operand when dividing the left and right operands c %= a is equivalent to c = (c % a)

Ternary operators

Java Ternary operator is used as a one-liner replacement for the if-else statement.

Example:

public class App {
  
  public static void main(String[] args) {
    int a = 10;
    int b = 12;
    System.out.println(a > b ? "Yes" : "No");
  }
}
Output: Yes

Precedence of Java Operators

The table below lists the precedence of operators in Java:

Operators Precedence
postfix increment and decrement ++ --
prefix increment and decrement, and unary ++ -- + - ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment = += -= *= /= %=
&= ^= |= <<= >>= >>>=


That’s it!

 

Leave a Reply

Your email address will not be published. Required fields are marked *