Java Operators

4 min read ·

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.
OperatorMeaning
+Addition
-Subtraction
*Multiplication
/Division
%Modulus

Example

Output Addition: 13 Subtraction: 7 Multiplication: 30 Division: 3 Modulus: 1
Note

Division between two integers removes decimal part.


Assignment Operators

Assignment operators are used to assign values to variables.
OperatorExampleMeaning
=a = 5Assign
+=a += 3a = a + 3
-=a -= 2a = a - 2
*=a *= 4a = a * 4
/=a /= 2a = a / 2
%=a %= 3a = a % 3

Example

Output 15 30

Comparison Operators

Comparison operators are used to compare two values.
OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than equal
<=Less than equal

Example

Output false true true false

Logical Operators

Logical operators are used with boolean values.
OperatorMeaning
&&Logical AND
Logical OR
!Logical NOT

Example

Output Can Drive: true
Pro Tip

Logical AND returns true only if both conditions are true.


Increment and Decrement Operators

Used to increase or decrease value by 1.
OperatorMeaning
++Increment
--Decrement

Example

Output 6 5

Pre and Post Increment Example

Output 6 6 7
Caution

Pre increment increases value first. Post increment uses value first then increases.


Ternary Operator

Used as a short form of if else.
Syntax condition ? valueIfTrue : valueIfFalse

Example

Output Pass
Real World Scenario

Ternary operator is commonly used in UI logic to display different messages based on conditions.


Exercise

  1. Write a program to check whether a number is even or odd using modulus operator.
  2. Create a program that checks if a person is eligible to vote.
  3. Use ternary operator to check whether a number is positive or negative.