Back to Java Course

Java Course

Java operators are special symbols that perform operations on variables and values.

Operators in Java are categorized into several types:

  • Assignment Operators: =, +=, -=, *=, /=, %=
  • Relational Operators: ==, !=, >, <, >=, <=
  • Arithmetic Operators: +, -, *, /, %
  • Conditional (Ternary) Operator: condition ? expression1 : expression2
  • Logical Operators: &&, ||, !

I. Assignment Operators

Assignment operators are used to assign values to variables.

Java provides several assignment operators:

= Basic assignment operator

int a = 10; // Assigns 10 to a

+= Adds the right operand to the left operand and assigns the result to the left operand.

a += 5; // Equivalent to a = a + 5; a is now 15

-= Subtracts the right operand from the left operand and assigns the result to the left operand.

a -= 3; // Equivalent to a = a - 3; a is now 12

*= Multiplies the left operand by the right operand and assigns the result to the left operand.

a *= 2; // Equivalent to a = a * 2; a is now 24

/= Divides the left operand by the right operand and assigns the result to the left operand.

a /= 4; // Equivalent to a = a / 4; a is now 6

%= Takes the modulus of the left operand with the right operand and assigns the result to the left operand.

a %= 4; // Equivalent to a = a % 4; a is now 2

II. Relational Operators

Relational operators are used to compare two values and return a boolean result:

== Checks if two values are equal.

boolean isEqual = (a == 2); // true if a is 2

!= Checks if two values are not equal.

boolean isNotEqual = (a != 2); // true if a is not 2

> Checks if the left value is greater than the right value.

boolean isGreater = (a > 1); // true if a is greater than 1

< Checks if the left value is less than the right value.

boolean isLess = (a < 3); // true if a is less than 3

>= Checks if the left value is greater than or equal to the right value.

boolean isGreaterOrEqual = (a >= 2); // true if a is greater than or equal to 2

<= Checks if the left value is less than or equal to the right value.

boolean isLessOrEqual = (a <= 2); // true if a is less than or equal to 2

III. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations:

+ Addition

int sum = 5 + 3; // sum is 8

- Subtraction

int difference = 5 - 3; // difference is 2

* Multiplication

int product = 5 * 3; // product is 15

/ Division

int quotient = 15 / 4; // quotient is 3

IV. Conditional Operator (Ternary Operator)

The conditional (ternary) operator is a shorthand for if-else statements.

Its syntax is:

condition ? expression1 : expression2

If condition is true, expression1 is evaluated; otherwise, expression2 is evaluated.

int a = 10; int b = 20; int max = (a > b) ? a : b; // max is assigned 20 because a is not greater than b

V. Logical Operator

Logical operators are used to perform logical operations on boolean values:

&& Logical AND. Returns true if both operands are true.

boolean result = (a > 5) && (a < 15); // true if a is between 5 and 15

|| Logical OR. Returns true if at least one of the operands is true.

boolean result = (a < 5) || (a > 15); // true if a is less than 5 or greater than 15

! Logical NOT. Returns true if the operand is false.

boolean result = !(a > 5); // true if a is not greater than 5