Skip to main content

Command Palette

Search for a command to run...

Java Operators

Updated
3 min readView as Markdown
Java Operators

Operators in Java

The operator is the symbol that tells the compiler to perform some operation over the expression.

Example: 'sum = a + b' where a and b are the operands whereas + is the operator.

There are different types of operators in Java. Let's discuss them one by one.

  1. Arithmetic operators

  2. Assignment operators

  3. Comparison operators

  4. Logical operators

  5. Relational operators

  6. Bitwise operators

Arithmetic operators

They are further divided into two parts: Unary and Binary operators.

Unary operators are those which are performed on the number "1". They are ++ and -- whereas binary operators are those which are performed on the two operands. They are +, -, *, /, %.

So arithmetic operators are:

OperatorNameDescriptionExample
+AdditionAdds two valuesA + B
-SubtractionSubtracts one value from anotherA - B
*MultiplicationMultiplies two valuesA * B
/DivisionDivides one value by anotherA / B
%ModulusGives the division remainderA % B
++IncrementIncreases the value of a variable by 1A++
--DecrementDecreases the value of a variable by 1A--

Increment and decrement operators are of two types:

  • Pre-increment and pre-decrement

  • Post-increment and post-decrement

Assignment Operators

Assignment operators are used to assign values to variables.

Some of the assignment operators are:

OperatorExampleSame As
\=a = 3a = 3
+=a += 3a = a + 3
-=a -= 3a = a - 3
*=a *= 3a = a * 3
/=a /= 3a = a / 3
%=a %= 3a = a % 3

Comparison Operators

Comparison operators are used to compare two values (or variables). It is important because it helps us to find answers and make decisions.

The return value of a comparison is either true or false. These values are known as Boolean values. Some of the comparison operators are given as follows:

OperatorNameExample
\==Equal tox == y
!=Not equalx != y
\>Greater thanx > y
<Less thanx < y
\>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Logical Operators

Logical operators are used to determine the logic between variables or values. Some of the logical operators are:

OperatorNameDescriptionExample
&&Logical ANDReturns true if both statements are truex < 5 && x < 10
Logical ORReturns true if one of the statements is truex < 5x < 4
!Logical NOTReverse the result, return false if the result is true!(x < 5 && x < 10)

We will learn more about the operators in the coming lessons. For now, just remember these operators.