Skip to content

Java Operators

Operators are like the building blocks of expressions in Java. They allow us to perform various operations on data, whether it's arithmetic calculations, logical comparisons, or bitwise manipulations. Think of operators as the tools in your coding toolbox - each serving a unique purpose and helping you achieve your programming goals.

Unary Operator

Unary operators operate on a single operand. Here are the unary operators in Java:

OperatorExplanationExample
++Increment: Increases the value of the operand by 1.int a = 5;
a++; // Now, a equals 6.
--Decrement: Decreases the value of the operand by 1.int b = 10;
b--; // Now, b equals 9.
+Unary plus: Indicates a positive value.int c = -5;
int d = +c; // Now, d equals -5.
-Unary minus: Negates the value of the operand.int e = 8;
int f = -e; // Now, f equals -8.
!Logical NOT: Negates the boolean value of the operand.boolean g = true;
boolean h = !g; // Now, h equals false.

Arithmetic Operator

Arithmetic operators perform mathematical operations on numerical operands. Here are the arithmetic operators in Java:

OperatorExplanationExample
+Addition: Adds two operands.int sum = 5 + 3; // sum equals 8
-Subtraction: Subtracts the second operand from the first.int difference = 8 - 4; // difference equals 4
*Multiplication: Multiplies two operands.int product = 6 * 2; // product equals 12
/Division: Divides the first operand by the second.int quotient = 10 / 2; // quotient equals 5
%Modulus: Returns the remainder of the division.int remainder = 10 % 3; // remainder equals 1

Shift Operator

Shift operators move the bits of a binary number to the left or right. Here are the shift operators in Java:

OperatorExplanationExample
<<Left shift: Shifts the bits of the left operand to the left by the number of positions specified by the right operand.int num = 5 << 2; // num equals 20
>>Right shift: Shifts the bits of the left operand to the right by the number of positions specified by the right operand.int num = 20 >> 2; // num equals 5
>>>Unsigned right shift: Shifts the bits of the left operand to the right by the number of positions specified by the right operand, filling the leftmost bits with zeros.int num = -20 >>> 2; // num equals 1073741823

Relational Operator

Relational operators compare the values of two operands and return a boolean result. Here are the relational operators in Java:

OperatorExplanationExample
==Equal to: Returns true if the values of the operands are equal.boolean isEqual = (5 == 5); // isEqual equals true
!=Not equal to: Returns true if the values of the operands are not equal.boolean isNotEqual = (6 != 3); // isNotEqual equals true
>Greater than: Returns true if the value of the left operand is greater than the value of the right operand.boolean isGreater = (8 > 3); // isGreater equals true
<Less than: Returns true if the value of the left operand is less than the value of the right operand.boolean isLess = (4 < 7); // isLess equals true
>=Greater than or equal to: Returns true if the value of the left operand is greater than or equal to the value of the right operand.boolean isGreaterOrEqual = (9 >= 9); // isGreaterOrEqual equals true
<=Less than or equal to: Returns true if the value of the left operand is less than or equal to the value of the right operand.boolean isLessOrEqual = (2 <= 5); // isLessOrEqual equals true

Bitwise Operator

Bitwise operators perform bit-level operations on binary representations of integers. Here are the bitwise operators in Java:

OperatorExplanationExample
&Bitwise AND: Performs a bitwise AND operation on the corresponding bits of the operands.int result = 5 & 3; // result equals 1
|Bitwise OR: Performs a bitwise OR operation on the corresponding bits of the operands.int result = 5 | 3; // result equals 7
^Bitwise XOR: Performs a bitwise XOR (exclusive OR) operation on the corresponding bits of the operands.int result = 5 ^ 3; // result equals 6
~Bitwise complement: Flips the bits of the operand.int result = ~5; // result equals -6

Logical Operator

Logical operators perform logical operations on boolean operands. Here are the logical operators in Java:

OperatorExplanationExample
&&Logical AND: Returns true if both conditions are true.boolean result = (true && false); // result equals false
||Logical OR: Returns true if at least one condition is true.boolean result = (true || false); // result equals true
!Logical NOT: Returns the opposite of a boolean value.boolean result = !true; // result equals false

Ternary Operator

The ternary operator is a conditional operator that evaluates a boolean expression and returns one of two values depending on whether the expression is true or false. Here's the ternary operator in Java:

OperatorExplanationExample
? :Ternary operator: Evaluates a boolean expression and returns one of two values based on the result.int result = (5 > 3) ? 10 : 20; // result equals 10 if the condition is true, otherwise 20

Assignment Operator

Assignment operators are used to assign values to variables. Here are the assignment operators in Java:

OperatorExplanationExample
=Assignment: Assigns a value to a variable.int num = 10;
+=Addition Assignment: Adds a value to a variable and assigns the result.num += 5; // Equivalent to num = num + 5;
-=Subtraction Assignment: Subtracts a value from a variable and assigns the result.num -= 3; // Equivalent to num = num - 3;
*=Multiplication Assignment: Multiplies a variable by a value and assigns the result.num *= 2; // Equivalent to num = num * 2;
/=Division Assignment: Divides a variable by a value and assigns the result.num /= 4; // Equivalent to num = num / 4;
%=Modulus Assignment: Performs modulus on a variable and assigns the result.num %= 3; // Equivalent to num = num % 3;

Understanding these operators and how they work is fundamental to becoming proficient in Java programming.

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.