Java Operators
Java operators are special symbols that perform operations on one or more variables or values. where variables or values are called operands. They are used to perform different operations.
Types of Java Operators:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators (Relational)
- Logical Operators
- Increment and Decrement Operators (Unary Operators)
- Ternary Operator
- Bitwise and Shift Operators
- Type Comparison Operator (Instanceof Operator)
1. Arithmetic Operators:
Arithmetic operators are used to perform basic mathematical calculations such as addition, subtraction, multiplication, division, and modulus on numeric values.
Example:
public class HelloWorld {
public static void main(String[] args) {
int firstNumber = 10;
int secondNumber = 3;
// Addition
int sum = firstNumber + secondNumber;
System.out.println("Addition: " + sum); // Output: 13
// Subtraction
int difference = firstNumber - secondNumber;
System.out.println("Subtraction: " + difference); // Output: 7
// Multiplication
int product = firstNumber * secondNumber;
System.out.println("Multiplication: " + product); // Output: 30
// Division
int quotient = firstNumber / secondNumber;
System.out.println("Division: " + quotient); // Output: 3
// Modulus (Remainder)
int remainder = firstNumber % secondNumber;
System.out.println("Modulus: " + remainder); // Output: 1
}
}2. Assignment Operators:
The assignment operator (=) is used to assign a value to a variable. It is also used to perform compound assignments, will see in examples below.
Example:
public class HelloWorld {
public static void main(String[] args) {
int number;
// = (Assignment operator)
number = 5;
// used to assign a value that is 5
System.out.println("= : " + number); // Output: 5
/*
Example of compound assignments
*/
// += (Addition assignment)
number = 10;
number += 3;
// similar like -> number = number + 3
System.out.println("+= : " + number); // Output: 13
// -= (Subtraction assignment)
number = 10;
number -= 3;
// similar like -> number = number - 3
System.out.println("-= : " + number); // Output: 7
// *= (Multiplication assignment)
number = 10;
number *= 3;
// similar like -> number = number * 3
System.out.println("*= : " + number); // Output: 30
// /= (Division assignment)
number = 10;
number /= 3;
// similar like -> number = number / 3
System.out.println("/= : " + number); // Output: 3
// %= (Modulus assignment)
number = 10;
number %= 3;
// similar like -> number = number % 3
System.out.println("%= : " + number); // Output: 1
// &= (Bitwise AND assignment)
number = 10;
number &= 3;
// similar like -> number = number & 3
System.out.println("&= : " + number); // Output: 2
// |= (Bitwise OR assignment)
number = 10;
number |= 3;
// similar like -> number = number | 3
System.out.println("|= : " + number); // Output: 11
// ^= (Bitwise XOR assignment)
number = 10;
number ^= 3;
// similar like -> number = number ^ 3
System.out.println("^= : " + number); // Output: 9
// >>= (Right shift assignment)
number = 10;
number >>= 1;
// similar like -> number = number >> 1
System.out.println(">>= : " + number); // Output: 5
// <<= (Left shift assignment)
number = 10;
number <<= 1;
// similar like -> number = number << 1
System.out.println("<<= : " + number); // Output: 20
}
}3. Comparison Operators (Relational):
Comparison operators are used to compare two values or variables and return a boolean result (true or false).
Example:
public class HelloWorld {
public static void main(String[] args) {
int a = 10;
int b = 20;
// == (equal to)
System.out.println(a == b); // false because 10 is not equal to 20
// != (not equal to)
System.out.println(a != b); // true because 10 is not equal to 20
// > (greater than)
System.out.println(a > b); // false because 10 is not greater than 20
// < (less than)
System.out.println(a < b); // true because 10 is less than 20
// >= (greater than or equal to)
System.out.println(a >= b); // false because 10 is neither greater nor equal to 20
// <= (less than or equal to)
System.out.println(a <= b); // true because 10 is less than 20
}
}4. Logical Operators
Logical operators are used to combine different conditions (boolean expressions) and always return a boolean value.
Example:
| Operator | Name | Description |
|---|---|---|
| && | Logical AND | Returns true if both conditions are true |
| || | Logical OR | Returns true if at least one condition is true |
| ! | Logical NOT | Reverses the result (true becomes false, false becomes true) |
Example:
public class HelloWorld {
public static void main(String[] args) {
int age = 18;
int marks = 75;
boolean hasIDCard = true;
// AND (&&)
System.out.println("AND Operator:");
System.out.println(age >= 18 && marks >= 60); // true
System.out.println(age >= 18 && marks >= 80); // false
System.out.println();
// OR (||)
System.out.println("OR Operator:");
System.out.println(marks >= 80 || hasIDCard); // true
System.out.println(marks >= 90 || age < 16); // false
System.out.println();
// NOT (!)
System.out.println("NOT Operator:");
System.out.println(!(age >= 18)); // false
System.out.println(!hasIDCard); // false
}
}5. Increment and Decrement Operators (Unary Operators)
Increment and decrement operators are also called as Unary Operators in Java used to increase or decrease a variable’s value by 1, It works with one operand.
| Operator | Name | Meaning |
|---|---|---|
| + | Unary Plus | Indicates positive value |
| - | Unary Minus | Changes sign of value |
| ++ | Increment | Increases value by 1 |
| -- | Decrement | Decreases value by 1 |
| ! | Logical NOT | Reverses boolean value |
Example:
public class HelloWorld {
public static void main(String[] args) {
int initialValue = 10; // Initial integer value
boolean booleanValue = true; // Boolean value for logical NOT example
// Unary plus: shows positive value (no change in value)
System.out.println("Unary Plus: " + (+initialValue));
// Unary minus: changes sign of number
System.out.println("Unary Minus: " + (-initialValue));
System.out.println("\nIncrement Operators:");
// Pre-increment: first increases value, then prints
System.out.println(++initialValue); // 11
// Post-increment: first prints value, then increases it
System.out.println(initialValue++); // 11
// After post-increment, value is updated
System.out.println(initialValue); // 12
System.out.println("\nDecrement Operators:");
// Pre-decrement: first decreases value, then prints
System.out.println(--initialValue); // 11
// Post-decrement: first prints value, then decreases it
System.out.println(initialValue--); // 11
// After post-decrement, value is updated
System.out.println(initialValue); // 10
System.out.println("\nLogical NOT Operator:");
// Logical NOT: reverses boolean value
System.out.println(!booleanValue); // false
}
}6. Ternary Operator
The ternary operator is a short form of if-else statement used to make basic decisions in one line.
Syntax:
condition ? firstvalue : secondvalue ;
// If the condition is true, the first value is selected.
// If the condition is false, the second value is selected.Example:
public class HelloWorld {
public static void main(String[] args) {
int marks = 60;
// Pass if marks >= 40
String result = (marks >= 40) ? "Pass" : "Fail";
// If condition is true → first value is picked ("Pass")
// If condition is false → second value is picked ("Fail")
System.out.println("Marks: " + marks);
System.out.println("Result: " + result);
// Output:
// Marks: 55
// Result: Pass
}
}7. Bitwise and Shift Operators
Bitwise and shift operators are operators that work directly on the binary (bit-level) representation of integer data types like int, long, short, char, and byte. Commonly used for low-level programming, performance optimization, encryption.
| Operator | Name | Meaning |
|---|---|---|
| & | AND | 1 if both bits are 1 |
| | | OR | 1 if at least one bit is 1 |
| ^ | XOR | 1 if bits are different |
| ~ | NOT | Flips bits (0 ↔ 1) |
| << | Left Shift | Shifts bits left (multiply by 2) |
| >> | Right Shift | Shifts bits right (divide by 2) |
| >>> | Unsigned Right Shift | Shifts bits right with zero fill |
1. Bitwise AND (&)
Returns 1 only when both bits are 1.
Example:
public class HelloWorld {
public static void main(String[] args) {
int a = 5; // 0101
int b = 3; // 0011
System.out.println(a & b);// output : 1
}
}Calculation:
0101 = (5)
& 0011 = (3)
-------
0001 = (1)- OR (|) Returns 1 if at least one bit is 1. Example:
public class HelloWorld {
public static void main(String[] args) {
int a = 5;
int b = 3;
System.out.println(a | b);// output : 7
}
}Calculation:
0101 = (5)
| 0011 = (3)
-------
0111 = (7)- XOR (^) Returns 1 when bits are different. Example:
public class HelloWorld {
public static void main(String[] args) {
int a = 5;
int b = 3;
System.out.println(a ^ b); // output : 6
}
}Calculation:
0101 = (5)
^ 0011 = (3)
-------
0110 = (6)- NOT (~) Reverses all bits.
public class HelloWorld {
public static void main(String[] args) {
int a = 5;
System.out.println(~a); //output : -6
}
}Calculation:
Rule: ~n = -(n + 1)
~5 = -(5+1)
= -6- Left Shift (
<<) Moves bits to the left. Example:
public class HelloWorld {
public static void main(String[] args) {
int a = 5;
System.out.println(a << 1); //output : 10
}
}Calculation:
5 = 0101
0101 << 1
1010 = 10or
Another way of calculation:
n << 1 = n × 2
n << 2 = n × 4
Example:
5 << 1 = 10
5 << 2 = 20- Right Shift (>>) Moves bits to the right. Example:
public class HelloWorld {
public static void main(String[] args) {
int a = 20;
System.out.println(a >> 2);// output: 5
}
}Calculation:
20 = 10100
10100 >> 2
00101 = 5or
Another way of calculation:
n >> 1 = n ÷ 2
n >> 2 = n ÷ 4
Example:
20 >> 2 = 5- Unsigned Right Shift (>>>) Similar to >>, but always fills left side with 0. Example:
public class HelloWorld {
public static void main(String[] args) {
int a = -20;
System.out.println(a >>> 2);//output :1073741819
}
}Calculation:
20 = 00010100
20 >>> 2
00000101 = 58. Type Comparison Operator (instanceof Operator)
The instanceof operator in Java is used to check whether an object is an instance of a specific class, subclass, or interface during runtime.
That is it returns true if the object is an instance of the specified type otherwise false.
Example:
public class HellowWorld {
public static void main(String[] args) {
Cat cat = new Cat();
Car car = new Car();
System.out.println(cat instanceof Dog); // true
System.out.println(cat instanceof Animal); // true
System.out.println(car instanceof Animal); // false
System.out.println(car instanceof Object); // true
System.out.println(cat instanceof Object); // true
}
}
class Animal {
System.out.println("Animal class");
}
class Cat extends Animal {
System.out.println("Dog class");
}
class Car {
System.out.println("Car class");
}
See you in next page....🙂