In Java, operators are used to perform different operations on variables and values. These operators can be classified into four categories: arithmetic, relational, logical, and assignment. In this blog post, we will discuss these operators in detail and provide example code and commands to demonstrate their usage.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division. The arithmetic operators in Java are:
Operator | Description | Example |
---|---|---|
+ | Addition | int sum = 2 + 3; |
– | Subtraction | int diff = 5 - 3; |
* | Multiplication | int product = 2 * 3; |
/ | Division | int quotient = 10 / 2; |
% | Modulus | int remainder = 10 % 3; |
Here’s an example code that demonstrates the usage of arithmetic operators:
int a = 5, b = 2;
int sum = a + b;
int diff = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
System.out.println("Sum: " + sum);
System.out.println("Difference: " + diff);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
System.out.println("Remainder: " + remainder);
Relational Operators
Relational operators are used to compare two values or variables. These operators return a boolean value (either true or false) based on the comparison. The relational operators in Java are:
Operator | Description | Example |
---|---|---|
== | Equal to | if(a == b) |
!= | Not equal to | if(a != b) |
> | Greater than | if(a > b) |
< | Less than | if(a < b) |
>= | Greater than or equal to | if(a >= b) |
<= | Less than or equal to | if(a <= b) |
Here’s an example code that demonstrates the usage of relational operators:
int a = 5, b = 2;
if(a == b) {
System.out.println("a is equal to b");
}
else if(a > b) {
System.out.println("a is greater than b");
}
else {
System.out.println("a is less than b");
}
Logical Operators
Logical operators are used to combine two or more conditions and return a boolean value based on the result. The logical operators in Java are:
Operator | Description | Example |
---|---|---|
&& | Logical AND | if(a > 0 && b > 0) |
|| | Logical OR | `if(a > 0 |
! | Logical NOT | if(!(a > 0)) |
Here’s an example code that demonstrates the usage of logical operators:
int a = 5, b = -2;
if(a > 0 && b > 0) {
System.out.println("Both a and b are positive");
}
else if(a > 0 || b > 0) {
System.out.println("At least one of a and b is positive");
}
else {
System.out.println("Both a and b are negative");
}
Assignment Operators
Assignment operators are used to assign a value to a variable. The assignment operators in Java are:
Operator | Description | Example |
---|---|---|
= | Assign value | int a = 5; |
+= | Add and assign | a += 3; (Equivalent to a = a + 3; ) |
-= | Subtract and assign | a -= 3; (Equivalent to a = a - 3; ) |
*= | Multiply and assign | a *= 3; (Equivalent to a = a * 3; ) |
/= | Divide and assign | a /= 3; (Equivalent to a = a / 3; ) |
%= | Modulus and assign | a %= 3; (Equivalent to a = a % 3; ) |
Here’s an example code that demonstrates the usage of assignment operators:
int a = 5;
a += 3;
System.out.println("a after adding 3: " + a);
a -= 2;
System.out.println("a after subtracting 2: " + a);
a *= 4;
System.out.println("a after multiplying by 4: " + a);
a /= 2;
System.out.println("a after dividing by 2: " + a);
a %= 3;
System.out.println("a after taking modulus by 3: " + a);
Conclusion
In this blog post, we discussed the different categories of operators in Java: arithmetic, relational, logical, and assignment. We also provided example code and commands to demonstrate the usage of each type of operator. By understanding these operators and how to use them, you will be able to write more complex and efficient code in Java.