Operators in Java are special symbols or keywords that are used to perform operations on variables and values. They are an essential part of any programming language and are used to manipulate data and perform various calculations. Java provides a rich set of operators to handle different types of operations, including arithmetic, logical, relational, bitwise, assignment, and more.
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.
+
(Addition): Adds two operands.-
(Subtraction): Subtracts the second operand from the first.*
(Multiplication): Multiplies two operands./
(Division): Divides the first operand by the second.%
(Modulus): Returns the remainder when the first operand is divided by the second.int a = 10;
int b = 5;
System.out.println("a + b = " + (a + b)); // 15
System.out.println("a - b = " + (a - b)); // 5
System.out.println("a * b = " + (a * b)); // 50
System.out.println("a / b = " + (a / b)); // 2
System.out.println("a % b = " + (a % b)); // 0
Unary operators operate on a single operand and perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.
+
(Unary Plus): Indicates a positive value (usually redundant).-
(Unary Minus): Negates an expression.++
(Increment): Increases a value by one.--
(Decrement): Decreases a value by one.!
(Logical Complement): Inverts the value of a boolean.int a = 10;
System.out.println("++a = " + (++a)); // 11 (pre-increment)
System.out.println("a++ = " + (a++)); // 11 (post-increment), then a becomes 12
System.out.println("--a = " + (--a)); // 11 (pre-decrement)
System.out.println("a-- = " + (a--)); // 11 (post-decrement), then a becomes 10
boolean flag = true;
System.out.println("!flag = " + (!flag)); // false
Relational operators are used to compare two values. They return a boolean result based on the comparison.
==
(Equal to): Checks if two values are equal.!=
(Not equal to): Checks if two values are not equal.>
(Greater than): Checks if the left operand is greater than the right.<
(Less than): Checks if the left operand is less than the right.>=
(Greater than or equal to): Checks if the left operand is greater than or equal to the right.<=
(Less than or equal to): Checks if the left operand is less than or equal to the right.int a = 10;
int b = 5;
System.out.println("a == b = " + (a == b)); // false
System.out.println("a != b = " + (a != b)); // true
System.out.println("a > b = " + (a > b)); // true
System.out.println("a < b = " + (a < b)); // false
System.out.println("a >= b = " + (a >= b)); // true
System.out.println("a <= b = " + (a <= b)); // false
Logical operators are used to perform logical operations on boolean values.
&&
(Logical AND): Returns true if both operands are true.||
(Logical OR): Returns true if at least one of the operands is true.!
(Logical NOT): Inverts the value of a boolean.boolean a = true;
boolean b = false;
System.out.println("a && b = " + (a && b)); // false
System.out.println("a || b = " + (a || b)); // true
System.out.println("!a = " + (!a)); // false
Bitwise operators are used to perform operations on individual bits of integer types.
&
(Bitwise AND): Performs a bitwise AND operation.|
(Bitwise OR): Performs a bitwise OR operation.^
(Bitwise XOR): Performs a bitwise XOR operation.~
(Bitwise Complement): Inverts all the bits of the operand.<<
(Left Shift): Shifts the bits to the left.>>
(Right Shift): Shifts the bits to the right.>>>
(Unsigned Right Shift): Shifts the bits to the right, filling with zeros.int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
System.out.println("a & b = " + (a & b)); // 1 (0001 in binary)
System.out.println("a | b = " + (a | b)); // 7 (0111 in binary)
System.out.println("a ^ b = " + (a ^ b)); // 6 (0110 in binary)
System.out.println("~a = " + (~a)); // -6 (inverts bits)
System.out.println("a << 2 = " + (a << 2)); // 20 (10100 in binary)
System.out.println("a >> 2 = " + (a >> 2)); // 1 (0001 in binary)
System.out.println("a >>> 2 = " + (a >>> 2)); // 1 (0001 in binary)
Assignment operators are used to assign values to variables.
=
(Assignment): Assigns the value of the right operand to the left operand.+=
(Addition Assignment): Adds the right operand to the left operand and assigns the result to the left operand.-=
(Subtraction Assignment): Subtracts the right operand from the left operand and assigns the result to the left operand.*=
(Multiplication Assignment): Multiplies the left operand by the right operand and assigns the result to the left operand./=
(Division Assignment): Divides the left operand by the right operand and assigns the result to the left operand.%=
(Modulus Assignment): Calculates the modulus of the left operand by the right operand and assigns the result to the left operand.int a = 10;
a += 5; // a = a + 5
System.out.println("a += 5: " + a); // 15
a -= 3; // a = a - 3
System.out.println("a -= 3: " + a); // 12
a *= 2; // a = a * 2
System.out.println("a *= 2: " + a); // 24
a /= 4; // a = a / 4
System.out.println("a /= 4: " + a); // 6
a %= 3; // a = a % 3
System.out.println("a %= 3: " + a); // 0
The ternary operator is a shorthand for the if-else
statement. It has the form ? :
and is used to evaluate a boolean expression and return one of two values.
int a = 10;
int b = 5;
int max = (a > b) ? a : b;
System.out.println("Max of a and b: " + max); // 10
The instanceof
operator is used to test whether an object is an instance of a specific class or subclass.
String str = "Hello";
boolean result = str instanceof String;
System.out.println("str is an instance of String: " + result); // true
Java provides a wide range of operators to perform various operations on variables and values. Understanding these operators is crucial for writing efficient and effective Java programs. They enable developers to perform calculations, make decisions, manipulate data, and much more.