-
Hajipur, Bihar, 844101
Operators in Java are special symbols used to perform operations on variables and values. They are the foundation of any programming logic because they allow us to manipulate data, perform calculations, compare values, and make decisions within a program. Java provides a wide range of operators to handle arithmetic, logical, relational, and bitwise operations efficiently.
An operator is a symbol that tells the compiler to perform a specific operation. For example, the + symbol adds two numbers, and the == symbol checks whether two values are equal.
In Java, operators work with operands, which are the values or variables they act upon.
Example:
int a = 10, b = 5;
int sum = a + b;
Here, + is the operator, and a and b are operands.
Java operators are divided into several categories based on their functionality. Let’s explore each category in detail.
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, and modulus.
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus (remainder) | a % b |
Example:
int x = 15, y = 4;
System.out.println(x + y); // 19
System.out.println(x - y); // 11
System.out.println(x * y); // 60
System.out.println(x / y); // 3
System.out.println(x % y); // 3
Assignment operators are used to assign values to variables. The most common is =, but Java also supports compound assignment operators that combine arithmetic and assignment.
| Operator | Description | Example | Equivalent To |
|---|---|---|---|
| = | Assign value | x = 10 | — |
| += | Add and assign | x += 5 | x = x + 5 |
| -= | Subtract and assign | x -= 5 | x = x - 5 |
| *= | Multiply and assign | x *= 5 | x = x * 5 |
| /= | Divide and assign | x /= 5 | x = x / 5 |
| %= | Modulus and assign | x %= 5 | x = x % 5 |
Example:
int num = 10;
num += 5; // num = 15
num *= 2; // num = 30
System.out.println(num);
Relational operators are used to compare two values or expressions. They return a boolean result — either true or false.
| Operator | Description | Example |
|---|---|---|
| == | Equal to | a == b |
| != | Not equal to | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater or equal to | a >= b |
| <= | Less or equal to | a <= b |
Example:
int a = 10, b = 20;
System.out.println(a > b); // false
System.out.println(a < b); // true
System.out.println(a != b); // true
Logical operators are used to combine multiple conditions or boolean expressions. They are mostly used in decision-making statements like if, while, and for.
| Operator | Description | Example |
|---|---|---|
| && | Logical AND | (a > b) && (b < c) |
| ! | Logical NOT | !(a > b) |
Example:
int age = 25;
boolean hasLicense = true;
if (age >= 18 && hasLicense) {
System.out.println("You can drive.");
} else {
System.out.println("You are not eligible to drive.");
}
Unary operators work with a single operand. They are used for incrementing, decrementing, or inverting values.
| Operator | Description | Example |
|---|---|---|
| ++ | Increment by 1 | ++x or x++ |
| -- | Decrement by 1 | --x or x-- |
| + | Unary plus | +x |
| - | Unary minus | -x |
| ! | Logical complement | !x |
Example:
int num = 5;
System.out.println(++num); // 6 (pre-increment)
System.out.println(num--); // 6 (post-decrement, prints before decrement)
System.out.println(num); // 5
Bitwise operators perform operations on the binary representation of integers. These are often used in low-level programming or optimization.
| Operator | Description | Example |
|---|---|---|
| & | AND | a & b |
| OR | ||
| ^ | XOR | a ^ b |
| ~ | NOT | ~a |
| << | Left shift | a << 2 |
| >> | Right shift | a >> 2 |
| >>> | Unsigned shift | a >>> 2 |
Example:
int a = 5, b = 7;
System.out.println(a & b); // 5 (binary 0101 & 0111 = 0101)
System.out.println(a | b); // 7
System.out.println(a ^ b); // 2
The ternary operator is a shorthand form of the if-else statement. It has three operands.
Syntax:
variable = (condition) ? expression1 : expression2;
Example:
int age = 18;
String result = (age >= 18) ? "Adult" : "Minor";
System.out.println(result);
This operator is used to check whether an object belongs to a particular class or not.
Example:
String name = "Java";
boolean result = name instanceof String;
System.out.println(result); // true
When multiple operators are used in one expression, Java follows operator precedence to decide which operation to perform first.
Example:
int result = 10 + 5 * 2;
System.out.println(result); // 20 (multiplication before addition)
Parentheses can be used to change precedence manually.
Operators are one of the most important components of Java programming.
Arithmetic operators handle mathematical calculations.
Relational and logical operators control decision-making.
Assignment operators simplify variable manipulation.
Unary, bitwise, and ternary operators make code compact and efficient.
Understanding each operator’s purpose helps you write more logical and optimized Java code.
Write a Java program that takes two numbers and performs addition, subtraction, multiplication, division, and modulus operations using arithmetic operators. Print all results clearly.
Create a Java program that demonstrates the use of compound assignment operators like +=, -=, *=, /=, and %= on a single integer variable and prints its value after each operation.
Write a Java program that compares two integer variables using relational operators (>, <, >=, <=, ==, !=) and displays the result of each comparison as true or false.
Develop a Java program that uses logical operators to check if a student is eligible for an exam based on two conditions: attendance above 75% and internal marks above 40.
Write a program that demonstrates the working of increment (++) and decrement (--) operators in both prefix and postfix forms and prints the result after each step.
Create a Java program that uses bitwise operators (&, |, ^, ~, <<, >>) on two integer values and prints the output of each operation.
Write a Java program that determines the larger of two numbers using the ternary operator and prints the result.
Develop a Java program that checks whether a given object belongs to a specific class using the instanceof operator and prints an appropriate message.
Write a program that demonstrates operator precedence by combining different arithmetic and logical operators in a single expression and explains the result.
Create a Java program that takes three integer inputs and uses both logical and relational operators to check if all numbers are in increasing order. Print “In Order” or “Not in Order” accordingly.