Operators
Operators are the backbone of programming in Java. They allow you to perform calculations, compare values, manipulate data, and control the flow of your code. Whether you’re adding numbers, checking conditions, or updating variables, you’re using operators. In this guide, we’ll break down every major type of operator in Java, explain how each one works, and provide simple examples to make your learning journey smooth and effective.
1. Arithmetic Operators
2. Relational Operators
1. Arithmetic Operators
Arithmetic operators are used for basic mathematical calculations. You’ll use them whenever you need to add, subtract, multiply, divide, or find the remainder of numbers.
List of Arithmetic Operators
How They Work
Example:
List of Arithmetic Operators
Operator | Operation | Example | Result |
---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 5 | 2 |
% | Modulus | 10 % 5 | 0 |
++ | Increment | a++ (if a=10) | 11 |
-- | Decrement | b-- (if b=5) | 4 |
How They Work
- Addition (+): Adds two numbers together.
- Subtraction (-): Subtracts the right number from the left.
- Multiplication (*): Multiplies two numbers.
- Division (/): Divides the left number by the right. If both numbers are integers, the result is also an integer (fractional part is discarded).
- Modulus (%): Returns the remainder after division.
- Increment (++): Increases a variable’s value by 1.
- Decrement (--): Decreases a variable’s value by 1.
Example:
int a = 10, b = 5;System.out.println(a + b); // 15System.out.println(a - b); // 5System.out.println(a * b); // 50System.out.println(a / b); // 2System.out.println(a % b); // 0a++;System.out.println(a); // 11b--;System.out.println(b); // 4
2. Relational Operators
Relational operators help you compare two values. The result is always either true or false. These are essential for making decisions in your code, such as inside if statements.
List of Relational Operators
How They Work
Example:
3. Logical Operators
4. Bitwise Operators
List of Relational Operators
Operator | Meaning | Example | Result |
---|---|---|---|
== | Equal to | a == b | false |
!= | Not equal to | a != b | true |
> | Greater than | a > b | true |
< | Less than | a < b | false |
>= | Greater than or equal to | a >= b | true |
<= | Less than or equal to | a <= b | false |
How They Work
- == checks if two values are exactly the same.
- != checks if two values are different.
- > checks if the left value is bigger than the right.
- < checks if the left value is smaller than the right.
- >= checks if the left value is bigger than or equal to the right.
- <= checks if the left value is smaller than or equal to the right.
Example:
int a = 10, b = 5;System.out.println(a == b); // falseSystem.out.println(a != b); // trueSystem.out.println(a > b); // trueSystem.out.println(a < b); // falseSystem.out.println(a >= b); // trueSystem.out.println(a <= b); // false
3. Logical Operators
Logical operators are used to combine or invert boolean values. They’re most often used in conditions where you want to check multiple statements at once.
List of Logical Operators
How They Work
Example:
List of Logical Operators
Operator | Meaning | Example | Result |
---|---|---|---|
&& | Logical AND | x && y | false |
|| | Logical OR | x | |
! | Logical NOT | !x | false |
How They Work
- && (AND): Returns true only if both sides are true.
- || (OR): Returns true if at least one side is true.
- ! (NOT): Flips the value; true becomes false and vice versa.
Example:
boolean x = true, y = false;System.out.println(x && y); // false (both must be true)System.out.println(x || y); // true (at least one is true)System.out.println(!x); // false (x is true, !x is false)
4. Bitwise Operators
Bitwise operators let you work with the individual bits of integer values. They’re especially useful in low-level programming, performance optimization, and graphics.
List of Bitwise Operators
How They Work
Example:
5. Assignment Operators
List of Bitwise Operators
Operator | Meaning | Example | Result |
---|---|---|---|
& | Bitwise AND | a & b | 1 |
| | Bitwise OR | a | b | 7 |
^ | Bitwise XOR | a ^ b | 6 |
~ | Bitwise NOT | ~a | -6 |
<< | Left Shift | a << 1 | 10 |
>> | Right Shift | a >> 1 | 2 |
>>> | Unsigned Right Shift | a >>> 1 | 2 |
How They Work
- & (AND): Each bit in the result is 1 only if both bits are 1.
- | (OR): Each bit in the result is 1 if at least one bit is 1.
- ^ (XOR): Each bit in the result is 1 if only one bit is 1.
- ~ (NOT): Flips every bit (1 to 0, 0 to 1).
- << (Left Shift): Shifts bits to the left (multiplies by 2 for each shift).
- >> (Right Shift): Shifts bits to the right (divides by 2 for each shift).
- >>> (Unsigned Right Shift): Like right shift, but fills leftmost bits with 0.
Example:
int a = 5; // 0101 in binaryint b = 3; // 0011 in binarySystem.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 (in binary, flips all bits)System.out.println(a << 1); // 10 (1010)System.out.println(a >> 1); // 2 (0010)System.out.println(a >>> 1);// 2 (0010)
5. Assignment Operators
Assignment operators are used to assign values to variables. Some also combine assignment with arithmetic operations, making your code shorter and easier to read.
List of Assignment Operators
How They Work
Example:
6. Conditional ( Ternary ) Operator
Example:
If a > b is true, max gets the value of a; otherwise, it gets the value of b.
7. Unary Operators
List of Assignment Operators
Operator | Meaning | Example | Result |
---|---|---|---|
= | Assign | a = 10 | 10 |
+= | Add and assign | a += 5 | 15 |
-= | Subtract and assign | a -= 3 | 12 |
*= | Multiply and assign | a *= 2 | 24 |
/= | Divide and assign | a /= 4 | 6 |
%= | Modulus and assign | a %= 3 | 0 |
How They Work
- = assigns a value to a variable.
- += adds a value to the variable and assigns the result.
- -= subtracts a value from the variable and assigns the result.
- *= multiplies the variable by a value and assigns the result.
- /= divides the variable by a value and assigns the result.
- %= finds the remainder and assigns the result.
Example:
int a = 10;a += 5; // a = a + 5; a is now 15a -= 3; // a = a - 3; a is now 12a *= 2; // a = a * 2; a is now 24a /= 4; // a = a / 4; a is now 6a %= 3; // a = a % 3; a is now 0System.out.println(a); // 0
6. Conditional ( Ternary ) Operator
The conditional or ternary operator is a shortcut for simple if-else statements. It evaluates a condition and returns one value if true, another if false.
Syntax:
Syntax:
result = (condition) ? valueIfTrue : valueIfFalse;
Example:
int a = 10, b = 20;int max = (a > b) ? a : b;System.out.println(max); // 20
If a > b is true, max gets the value of a; otherwise, it gets the value of b.
7. Unary Operators
Unary operators work with only one operand. They are used for tasks like incrementing, decrementing, negating, or inverting values.
List of Unary Operators
How They Work
Example:
List of Unary Operators
Operator | Meaning | Example | Result |
---|---|---|---|
+ | Unary plus | +a | 5 |
- | Unary minus | -a | -5 |
++ | Increment | a++ | 6 |
-- | Decrement | a-- | 5 |
! | Logical NOT | !b | false |
How They Work
- + indicates a positive value (rarely used, as numbers are positive by default).
- - negates a value (makes positive numbers negative, and vice versa).
- ++ increases a variable’s value by 1.
- -- decreases a variable’s value by 1.
- ! inverts a boolean value.
Example:
int a = 5;System.out.println(+a); // 5System.out.println(-a); // -5a++;System.out.println(a); // 6a--;System.out.println(a); // 5boolean b = true;System.out.println(!b); // false
Operators are essential tools in Java programming. They allow you to perform calculations, compare values, make decisions, and manipulate data efficiently. By mastering operators, you’ll write cleaner, more powerful, and more flexible code.
Tips: Experiment with each operator type in your own Java programs. Try combining them in different ways to see how they interact and affect your results.