Welcome to your next step in learning Java! In this tutorial, we’ll take a deep dive into two fundamental concepts:
- Operators (used to perform operations on variables and values)
- Type Casting (used to convert data types)
These are basic yet powerful tools that every Java programmer must understand.
What is an Operator?
An operator is a symbol that performs a specific operation on one, two, or more operands (variables or values). In Java, operators are used for performing arithmetic, comparison, logical, assignment, and other operations.
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, etc.
These operators perform basic mathematical operations:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 2 | 5 |
% | Modulus (remainder) | 10 % 3 | 1 |
Example:
int a = 10, b = 7;
System.out.println(a + b); // Output: 17
System.out.println(a - b); // Output: 3
System.out.println(a * b); // Output: 70
System.out.println(a / b); // Output: 1
System.out.println(a % b); // Output: 3
2. Unary Operators
Unary operators work on a single operand to perform various operations like increment, decrement, negation, etc.
Operators that works on a single operand.
| Operator | Meaning | Example |
|---|---|---|
+ | Unary plus | +a |
- | Unary minus | -a |
++ | Increment (adds 1) | a++ or ++a |
-- | Decrement (subtracts 1) | a-- or --a |
Pre vs Post Increment:
++a: Increments first, then uses the value.a++: Uses the value, then increments.
Example:
int a = 5;
System.out.println(+a); // Output: 5
System.out.println(-a); // Output: -5
System.out.println(++a); // Output: 6 (pre-increment)
System.out.println(a--); // Output: 6 (post-decrement, a becomes 5)
boolean flag = false;
System.out.println(!flag); // Output: true
3. Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Meaning | Example |
|---|---|---|
= | Assign | a = 5 |
+= | Add and assign | a += 3 (same as a = a + 3) |
-= | Subtract and assign | a -= 2 |
*= | Multiply and assign | a *= 2 |
/= | Divide and assign | a /= 2 |
%= | Modulo and assign | a %= 3 |
Example:
int a = 10;
a += 5; // a = 15
a -= 3; // a = 12
a *= 2; // a = 24
a /= 4; // a = 6
a %= 5; // a = 1
4. Relational (Comparison) Operators
Relational operators are used to compare two values and return a boolean result: true or false.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | a == b | true/false |
!= | Not equal to | a != b | true/false |
> | Greater than | a > b | true/false |
< | Less than | a < b | true/false |
>= | Greater than or equal to | a >= b | true/false |
<= | Less than or equal to | a <= b | true/false |
Example
int a = 10, b = 5;
System.out.println(a == b); // false
System.out.println(a != b); // true
System.out.println(a > b); // true
System.out.println(a < b); // false
System.out.println(a >= 10); // true
System.out.println(b <= 5); // true
5. Logical Operators
Logical operators are used to combine multiple boolean expressions and return a boolean result (true or false).
| Operator | Meaning | Example |
|---|---|---|
&& | Logical AND | a > 5 && b < 10 |
| ` | ` | |
! | Logical NOT | !(a > 5) |
Example:
int a = 8, b = 4;
System.out.println(a > 5 && b < 10); // true (both true)
System.out.println(a < 5 || b < 3); // false (both false)
System.out.println(!(a == b)); // true (a != b)
6. Bitwise Operators (Advanced)
Bitwise operators perform operations bit-by-bit on integer values. They are mostly used in low-level programming, such as device drivers, embedded systems, and performance-critical tasks.
| Operator | Name | Description | Example |
|---|---|---|---|
& | Bitwise AND | 1 if both bits are 1 | a & b |
| ` | ` | Bitwise OR | 1 if at least one bit is 1 |
^ | Bitwise XOR | 1 if bits are different | a ^ b |
~ | Bitwise Complement | Inverts each bit | ~a |
<< | Left Shift | Shifts bits left (multiplies by 2ⁿ) | a << 2 |
>> | Right Shift | Shifts bits right (divides by 2ⁿ) | a >> 2 |
>>> | Unsigned Right Shift | Fills 0s from the left | a >>> 2 |
Example
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
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)
System.out.println(a << 1); // 10 (0101 becomes 1010)
System.out.println(a >> 1); // 2 (0101 becomes 0010)
7. Ternary Operator
The ternary operator is a shorthand for if-else. It evaluates a condition and returns one of two values based on whether the condition is true or false.
Syntax
condition ? value_if_true : value_if_false;
| Part | Meaning |
|---|---|
condition | Boolean expression to evaluate |
? | Separates condition from true result |
: | Separates true result from false result |
Example
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Max is: " + max); // Output: Max is: 20
Test Your Knowledge
- What will be the result of
10 % 3? - Is
a += 5same asa = a + 5? - What is the difference between
a++and++a? - What does the ternary operator do?
- Will Java automatically convert a
floattoint? - Which operator is used to check equality?
- What is the output of: javaCopyEdit
int x = 5; System.out.println(x++ + ++x);