
Java Operators in Java
Operators in Java
Operators in Java are special symbols used to perform operations on variables and values. Java supports a wide range of operators, which can be categorized into several types based on their functionality.
1. Arithmetic Operators
These operators are used to perform basic arithmetic operations on numeric values.
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (Remainder) | a % b |
Example:
int a = 10, b = 5;System.out.println(a + b); // Outputs: 15 (Addition)System.out.println(a - b); // Outputs: 5 (Subtraction)System.out.println(a * b); // Outputs: 50 (Multiplication)System.out.println(a / b); // Outputs: 2 (Division)System.out.println(a % b); // Outputs: 0 (Modulus)
2. Relational (Comparison) Operators
These operators are used to compare two values and return a boolean result (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:
int a = 10, b = 5;System.out.println(a == b); // Outputs: false (Equal)System.out.println(a != b); // Outputs: true (Not equal)System.out.println(a > b); // Outputs: true (Greater than)System.out.println(a < b); // Outputs: false (Less than)System.out.println(a >= b); // Outputs: true (Greater than or equal to)System.out.println(a <= b); // Outputs: false (Less than or equal to)
3. Logical Operators
These operators are used to combine multiple boolean expressions.
Operator | Description | Example |
---|---|---|
&& | Logical AND (both conditions must be true) | a && b |
` | ` | |
! | Logical NOT (inverts the boolean value) | !a |
Example:
boolean a = true, b = false;System.out.println(a && b); // Outputs: false (Logical AND)System.out.println(a || b); // Outputs: true (Logical OR)System.out.println(!a); // Outputs: false (Logical NOT)
4. Assignment Operators
These operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assigns a value | a = 10 |
+= | Add and assign | a += 5 |
-= | Subtract and assign | a -= 5 |
*= | Multiply and assign | a *= 5 |
/= | Divide and assign | a /= 5 |
%= | Modulus and assign | a %= 5 |
Example:
int a = 10;a += 5; // a = a + 5 => 15a -= 3; // a = a - 3 => 12a *= 2; // a = a * 2 => 24a /= 4; // a = a / 4 => 6a %= 3; // a = a % 3 => 0
5. Unary Operators
These operators are used to operate on a single operand, typically modifying its value.
Operator | Description | Example |
---|---|---|
+ | Unary plus (indicates positive value) | +a |
- | Unary minus (negates value) | -a |
++ | Increment (adds 1 to a variable) | a++ or ++a |
-- | Decrement (subtracts 1 from a variable) | a-- or --a |
! | Logical NOT (inverts a boolean value) | !a |
Example:
int a = 5;System.out.println(++a); // Outputs: 6 (Pre-increment)System.out.println(a++); // Outputs: 6 (Post-increment)System.out.println(--a); // Outputs: 5 (Pre-decrement)System.out.println(a--); // Outputs: 5 (Post-decrement)
6. Bitwise Operators
Bitwise operators are used to perform bit-level operations on integral types (e.g., int
, long
).
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
` | ` | Bitwise OR |
^ | Bitwise XOR | a ^ b |
~ | Bitwise complement (inverts bits) | ~a |
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
>>> | Unsigned right shift | a >>> 2 |
Example:
int a = 5; // 0101 in binaryint b = 3; // 0011 in binarySystem.out.println(a & b); // Outputs: 1 (Bitwise AND, 0101 & 0011 = 0001)System.out.println(a | b); // Outputs: 7 (Bitwise OR, 0101 | 0011 = 0111)System.out.println(a ^ b); // Outputs: 6 (Bitwise XOR, 0101 ^ 0011 = 0110)System.out.println(~a); // Outputs: -6 (Bitwise complement, ~0101 = 1010)System.out.println(a << 1); // Outputs: 10 (Left shift, 0101 << 1 = 1010)System.out.println(a >> 1); // Outputs: 2 (Right shift, 0101 >> 1 = 0010)
7. Ternary Operator
The ternary operator is a shorthand for an if-else
statement and is used to assign a value based on a condition.
Operator | Description | Example |
---|---|---|
? : | Ternary conditional operator | condition ? value_if_true : value_if_false |
Example:
int a = 10, b = 5;int max = (a > b) ? a : b; // If a > b, max = a, else max = bSystem.out.println(max); // Outputs: 10
8. Instanceof Operator
The instanceof
operator is used to check whether an object is an instance of a specific class or implements a particular interface.
Operator | Description | Example |
---|---|---|
instanceof | Checks if an object is an instance of a class or implements an interface | object instanceof ClassName |
Example:
String s = "Hello";System.out.println(s instanceof String); // Outputs: true
Conclusion
Java provides a variety of operators to perform different kinds of operations. Understanding and effectively using these operators will help you manipulate data and control the flow of your program. Here's a summary of the operator categories:
Arithmetic Operators: Used for mathematical calculations.
Relational Operators: Used for comparisons.
Logical Operators: Used for combining boolean expressions.
Assignment Operators: Used to assign values to variables.
Unary Operators: Used for modifying or operating on a single operand.
Bitwise Operators: Used for bit-level operations on integers.
Ternary Operator: A shorthand for
if-else
statements.Instanceof Operator: Used to test object types.
Let me know if you need more details on any of these operators! ?