Introduction
Operators in Java are special symbols that perform operations on variables and values. They are fundamental to manipulating data and making decisions within a program. Java provides a rich set of operators that can be categorized into several types. In this chapter, we will explore each type of operator in detail, along with examples.
Table of Contents
- Introduction
- Arithmetic Operators
- Unary Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Ternary Operator
- Shift Operators
- Conclusion
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division.
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 = 10, b = 5;
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
}
}
Output:
a + b = 15
a - b = 5
a * b = 50
a / b = 2
a % b = 0
Unary Operators
Unary operators operate on a single operand to perform various operations such as incrementing/decrementing a value, negating an expression, or inverting the value of a boolean.
Operator | Description | Example |
---|---|---|
+ |
Unary plus (indicates positive value) | +a |
- |
Unary minus (negates an expression) | -a |
++ |
Increment operator (increases value by 1) | ++a or a++ |
-- |
Decrement operator (decreases value by 1) | --a or a-- |
! |
Logical complement operator (inverts boolean value) | !a |
public class UnaryExample {
public static void main(String[] args) {
int a = 10;
boolean b = false;
System.out.println("Unary plus: " + (+a));
System.out.println("Unary minus: " + (-a));
System.out.println("Pre-increment: " + (++a));
System.out.println("Post-increment: " + (a++));
System.out.println("After post-increment: " + a);
System.out.println("Pre-decrement: " + (--a));
System.out.println("Post-decrement: " + (a--));
System.out.println("After post-decrement: " + a);
System.out.println("Logical complement: " + (!b));
}
}
Output:
Unary plus: 10
Unary minus: -10
Pre-increment: 11
Post-increment: 11
After post-increment: 12
Pre-decrement: 11
Post-decrement: 11
After post-decrement: 10
Logical complement: true
Assignment Operators
Assignment operators are used to assign values to variables. The basic assignment operator is =
, and there are also compound assignment operators for performing arithmetic and assignment in one step.
Operator | Description | Example |
---|---|---|
= |
Simple assignment | a = b |
+= |
Addition assignment | a += b |
-= |
Subtraction assignment | a -= b |
*= |
Multiplication assignment | a *= b |
/= |
Division assignment | a /= b |
%= |
Modulus assignment | a %= b |
Example:
public class AssignmentExample {
public static void main(String[] args) {
int a = 10, b = 5;
a += b;
System.out.println("a += b: " + a);
a -= b;
System.out.println("a -= b: " + a);
a *= b;
System.out.println("a *= b: " + a);
a /= b;
System.out.println("a /= b: " + a);
a %= b;
System.out.println("a %= b: " + a);
}
}
Output:
a += b: 15
a -= b: 10
a *= b: 50
a /= b: 10
a %= b: 0
Relational Operators
Relational operators are used to compare two values. The result of a relational operation is a boolean value: 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 = 10, b = 5;
System.out.println("a == b: " + (a == b));
System.out.println("a != b: " + (a != b));
System.out.println("a > b: " + (a > b));
System.out.println("a < b: " + (a < b));
System.out.println("a >= b: " + (a >= b));
System.out.println("a <= b: " + (a <= b));
}
}
Output:
a == b: false
a != b: true
a > b: true
a < b: false
a >= b: true
a <= b: false
Logical Operators
Logical operators are used to perform logical operations on boolean values.
Operator | Description | Example |
---|---|---|
&& |
Logical AND | a && b |
|| |
Logical OR | a || b |
! |
Logical NOT | !a |
Example:
public class LogicalExample {
public static void main(String[] args) {
boolean a = true, b = false;
System.out.println("a && b: " + (a && b));
System.out.println("a || b: " + (a || b));
System.out.println("!a: " + (!a));
}
}
Output:
a && b: false
a || b: true
!a: false
Bitwise Operators
Bitwise operators perform bit-level operations on integer types.
Operator | Description | Example |
---|---|---|
& |
Bitwise AND | a & b |
| |
Bitwise OR | a | b |
^ |
Bitwise XOR | a ^ b |
~ |
Bitwise Complement | ~a |
public class BitwiseExample {
public static void main(String[] args) {
int a = 5, b = 3;
System.out.println("a & b: " + (a & b));
System.out.println("a | b: " + (a | b));
System.out.println("a ^ b: " + (a ^ b));
System.out.println("~a: " + (~a));
}
}
Output:
a & b: 1
a | b: 7
a ^ b: 6
~a: -6
Ternary Operator
The ternary operator is a shorthand for the if-else statement. It takes three operands and evaluates to a value based on a condition.
Operator | Description | Example |
---|---|---|
? : |
Ternary (conditional) operator | condition ? value1 : value2 |
Example:
public class TernaryExample {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Max value: " + max);
}
}
Output:
Max value: 20
Shift Operators
Operator | Description | Example |
---|---|---|
<< |
Left shift | a << b |
>> |
Right shift | a >> b |
>>> |
Unsigned right shift | a >>> b |
Example:
public class ShiftExample {
public static void main(String[] args) {
int a = 8; // Binary: 1000
System.out.println("a << 1: " + (a << 1)); // 16, Binary: 10000
System.out.println("a >> 1: " + (a >> 1)); // 4, Binary: 0100
System.out.println("a >>> 1: " + (a >>> 1)); // 4, Binary: 0100
}
}
Output:
a << 1: 16
a >> 1: 4
a >>> 1: 4
Conclusion
In this chapter, we covered the various types of operators in Java, including arithmetic, unary, assignment, relational, logical, bitwise, ternary, and shift operators. Understanding these operators and how to use them is fundamental to manipulating data and making decisions in your Java programs.