Welcome to The Coding College! In this guide, we’ll cover Java Operators, their types, and examples to help you master their usage. Operators are essential for performing operations on variables and values, making them fundamental in programming.
What Are Java Operators?
Operators are special symbols or keywords used to perform operations on variables or values. For example, +
is used for addition, =
for assignment, and &&
for logical operations.
Types of Java Operators
Java provides various operators grouped into the following categories:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Assignment Operators
- Unary Operators
- Bitwise Operators
- Ternary Operator
- Shift Operators
1. Arithmetic Operators
Used for performing mathematical operations:
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 = 3;
System.out.println("Addition: " + (a + b));
System.out.println("Subtraction: " + (a - b));
System.out.println("Multiplication: " + (a * b));
System.out.println("Division: " + (a / b));
System.out.println("Modulus: " + (a % b));
}
}
2. Relational (Comparison) Operators
Used to compare values and return a boolean (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 x = 5, y = 10;
System.out.println("x == y: " + (x == y));
System.out.println("x != y: " + (x != y));
System.out.println("x > y: " + (x > y));
System.out.println("x < y: " + (x < y));
System.out.println("x >= y: " + (x >= y));
System.out.println("x <= y: " + (x <= y));
}
}
3. Logical Operators
Used to combine multiple conditions or expressions.
Operator | Description | Example |
---|---|---|
&& | Logical AND | a > b && a < c |
` | ` | |
! | Logical NOT | !(a > b) |
Example:
public class LogicalExample {
public static void main(String[] args) {
int a = 10, b = 20, c = 30;
System.out.println("a > b && a < c: " + (a > b && a < c));
System.out.println("a > b || a < c: " + (a > b || a < c));
System.out.println("!(a > b): " + !(a > b));
}
}
4. Assignment Operators
Used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assign | a = b |
+= | Add and assign | a += b (a = a + b) |
-= | Subtract and assign | a -= b (a = a – b) |
*= | Multiply and assign | a *= b (a = a * b) |
/= | Divide and assign | a /= b (a = a / b) |
Example:
public class AssignmentExample {
public static void main(String[] args) {
int x = 5;
x += 3;
System.out.println("x += 3: " + x);
x *= 2;
System.out.println("x *= 2: " + x);
}
}
5. Unary Operators
Operate on a single operand.
Operator | Description | Example |
---|---|---|
+ | Positive number | +a |
- | Negative number | -a |
++ | Increment | a++ or ++a |
-- | Decrement | a-- or --a |
Example:
public class UnaryExample {
public static void main(String[] args) {
int x = 10;
System.out.println("x++: " + x++);
System.out.println("++x: " + ++x);
System.out.println("x--: " + x--);
System.out.println("--x: " + --x);
}
}
6. Bitwise Operators
Operate on bits and perform bit-by-bit operations.
Operator | Description | Example |
---|---|---|
& | AND | a & b |
` | ` | OR |
^ | XOR | a ^ b |
~ | Complement | ~a |
<< | Left shift | a << b |
>> | Right shift | a >> b |
Example:
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);
}
}
Practice Exercises
- Write a program to demonstrate the use of arithmetic and relational operators.
- Create a program to implement logical AND, OR, and NOT operations.
- Experiment with assignment operators and observe their effect on variable values.
Conclusion
Understanding Java operators is crucial for building logic in your programs. By mastering these operators, you’ll have a strong foundation for solving problems efficiently.
For more programming tutorials, visit The Coding College. Keep learning and coding! 🚀