What Operators are used in Java Programming
Operators in Java are special symbols that perform specific operations on one, two, or three operands and then return a result. Java operators are categorized based on the type of operations they perform, such as arithmetic, logical, bitwise, relational, and more. Understanding operators is crucial because they form the building blocks of any Java expression.
1. Arithmetic Operators
Arithmetic operators are used to perform basic 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 a = 10;
int b = 5;
System.out.println(a + b); // Outputs 15
System.out.println(a - b); // Outputs 5
System.out.println(a * b); // Outputs 50
System.out.println(a / b); // Outputs 2
System.out.println(a % b); // Outputs 0
2. Assignment Operators
Assignment operators are used to assign values to variables. The most common assignment operator is =
.
Operator | Description | Example |
---|---|---|
= | Assigns a value to a variable | a = 10 |
+= | Adds and assigns a value | a += 5 |
-= | Subtracts and assigns a value | a -= 5 |
*= | Multiplies and assigns a value | a *= 5 |
/= | Divides and assigns a value | a /= 5 |
%= | Modulus and assigns a value | a %= 5 |
Example:
int a = 10;
a += 5; // a is now 15
a -= 3; // a is now 12
3. Comparison (Relational) Operators
Comparison operators are used to compare two values. They 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;
int b = 5;
System.out.println(a > b); // Outputs true
System.out.println(a == b); // Outputs false
4. Logical Operators
Logical operators are used to combine multiple boolean expressions or values and return a boolean result.
Operator | Description | Example |
---|---|---|
&& | Logical AND | a > b && a > c |
|| | Logical OR | a > b || a > c |
! | Logical NOT | !(a > b) |
Example:
int a = 10;
int b = 5;
System.out.println(a > b && a < 20); // Outputs true
System.out.println(a > b || a > 20); // Outputs true
System.out.println(!(a > b)); // Outputs false
5. Unary Operators
Unary operators operate on a single operand and are used to perform operations like incrementing, decrementing, negating, or inverting the value.
Operator | Description | Example |
---|---|---|
+ | Unary plus (promotes to int) | +a |
- | Unary minus (negates an expression) | -a |
++ | Increment (increases value by 1) | a++ or ++a |
-- | Decrement (decreases value by 1) | a-- or --a |
! | Logical complement (inverts boolean value) | !a |
Example:
int a = 10;
System.out.println(++a); // Outputs 11
System.out.println(a--); // Outputs 11, then a becomes 10
System.out.println(-a); // Outputs -10
6. Bitwise Operators
Bitwise operators perform operations on binary representations of integers. They are used to manipulate bits of a number.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise XOR (exclusive OR) | a ^ b |
~ | Bitwise complement | ~a |
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
>>> | Unsigned right shift | a >>> 2 |
Example:
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
System.out.println(a & b); // Outputs 1 (Binary: 0001)
System.out.println(a | b); // Outputs 7 (Binary: 0111)
System.out.println(a ^ b); // Outputs 6 (Binary: 0110)
7. Ternary Operator
The ternary operator is a shorthand for the if-else
statement. It takes three operands and is used to evaluate a boolean expression.
Operator | Description | Example |
---|---|---|
? : | Ternary (conditional) operator | condition ? trueValue : falseValue |
Example:
int a = 10;
int b = 5;
int max = (a > b) ? a : b; // Outputs the larger of a and b
System.out.println(max); // Outputs 10
8. Instanceof Operator
The instanceof
operator is used to test whether an object is an instance of a specific class or subclass.
Operator | Description | Example |
---|---|---|
instanceof | Tests object type | obj instanceof ClassName |
Example:
String str = "Hello";
boolean result = str instanceof String; // Checks if str is an instance of String
System.out.println(result); // Outputs true
Summary
Java operators are powerful tools that allow you to perform various operations on variables and values. From basic arithmetic to complex bitwise operations, understanding how to use operators effectively is essential for writing efficient and effective Java code.