Java Operators
Java Operators
In Java, operators are special symbols or keywords used to perform specific operations on variables and values. These are essential building blocks of Java programs, enabling developers to execute computations, assign values, and make decisions. Operators in Java are categorized based on their functionality.
1. Arithmetic Operators
These operators are used to perform mathematical calculations 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:
public class ArithmeticExample { public static void main(String[] args) { int a = 15, b = 7; System.out.println("Addition: " + (a + b)); // 22 System.out.println("Subtraction: " + (a - b)); // 8 System.out.println("Multiplication: " + (a * b)); // 105 System.out.println("Division: " + (a / b)); // 2 System.out.println("Modulus: " + (a % b)); // 1 } }
2. Relational Operators
Relational operators evaluate the relationship between two values and produce a boolean outcome (true or false).
Operator | Description | Example |
---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Example:
public class RelationalExample { public static void main(String[] args) { int a = 25, b = 15; System.out.println(a == b); // false System.out.println(a != b); // true System.out.println(a > b); // false System.out.println(a < b); // true System.out.println(a >= b); // false System.out.println(a <= b); // true } }
3. Logical Operators
Operator | Description | Example |
---|---|---|
&& | Logical AND | a > 5 && b < 10 |
|| |
Logical OR | a > 5 || b < 10 |
! | Logical NOT | !(a > 5) |
Example:
public class LogicalExample { public static void main(String[] args) { int a = 7, b = 12; System.out.println((a > 5) && (b < 15)); // true System.out.println((a > 10) || (b < 15)); // true System.out.println(!(a > 5)); // false } }
4. Bitwise Operators
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
| |
Bitwise OR | a | b |
^ | Bitwise XOR | a ^ b |
~ | Bitwise Complement | ~a |
Example:
public class BitwiseExample { public static void main(String[] args) { int a = 5, b = 3; // Binary: 5 -> 0101, 3 -> 0011 System.out.println(a & b); // 1 (0001) System.out.println(a | b); // 7 (0111) System.out.println(a ^ b); // 6 (0110) System.out.println(~a); // -6 (inverts all bits) } }
5. Assignment Operators
Assignment operators assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assign | a = b |
+= | Add and assign | a += b |
-= | Subtract and assign | a -= b |
*= | Multiply and assign | a *= b |
/= | Divide and assign | a /= b |
%= | Modulus and assign | a %= b |
Example:
public class AssignmentExample { public static void main(String[] args) { int a = 10; a += ; // a = a + 6 System.out.println(a); // 16 int a = 12; a -= 5; // a = a - 5 System.out.println(a); // 7 } }
6. Unary Operators
These work on a single operand.
Operator | Description | Example |
---|---|---|
+ | Unary plus | +a |
- | Unary minus | -a |
++ | Increment (pre/post) | ++a , a++ |
-- | Decrement (pre/post) | --a , a-- |
! | Logical NOT | !a |
Example:
public class UnaryExample { public static void main(String[] args) { int a = 6; System.out.println(++a); // 7 System.out.println(a--); // 7 System.out.println(a); // 6 } }
7. Ternary Operator
The ternary operator provides a concise way to express an if-else condition.
Syntax | Example |
---|---|
condition ? value1 : value2 | a > b ? a : b |
public class TernaryExample { public static void main(String[] args) { int a = 12, b = 25; int max = (a > b) ? a : b; System.out.println("Maximum: " + max); // 25 } }
8. Shift Operators
These shift bits of a number to the left or right.
Operator | Description | Example |
---|---|---|
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
>>> | Unsigned right shift | a >>> 2 |
Example:
public class ShiftExample { public static void main(String[] args) { int a = 8; // Binary: 1000 System.out.println(a << 2); // 32 (Binary: 100000) System.out.println(a >> 2); // 2 (Binary: 0010) } }
By mastering Java operators, developers can efficiently write expressions for computations, decisions, and logic within their programs.