Arithmetic and Logical Operations in Java. Relational Operators
Introduction
In Java, operators are special symbols that perform operations on variables and values. Understanding the different types of operators is fundamental to mastering the language. This article will cover three major categories of operators in Java: arithmetic, logical, and relational operators.
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. Java supports the following arithmetic operators:
- Addition (
+
): Adds two operands.1
int sum = 5 + 3; // sum = 8
- Subtraction (
-
): Subtracts the second operand from the first.1
int difference = 5 - 3; // difference = 2
- Multiplication (
*
): Multiplies two operands.1
int product = 5 * 3; // product = 15
- Division (
/
): Divides the first operand by the second. If both operands are integers, the result is also an integer (the fractional part is discarded).1
int quotient = 5 / 3; // quotient = 1
- Modulus (
%
): Returns the remainder of the division of the first operand by the second.1
int remainder = 5 % 3; // remainder = 2
Logical Operators
Logical operators are used to perform logical operations on boolean values. These operators return a boolean value based on the logic of the operands. Java supports the following logical operators:
- AND (
&&
): Returnstrue
if both operands aretrue
.1
boolean result = (5 > 3) && (8 > 5); // result = true
- OR (
||
): Returnstrue
if at least one of the operands istrue
.1
boolean result = (5 > 3) || (8 < 5); // result = true
- NOT (
!
): Returns the opposite boolean value of the operand.1
boolean result = !(5 > 3); // result = false
Relational Operators
Relational operators are used to compare two values.
The result of a relational operation is a boolean value (true
or false
).
Java supports the following relational operators:
- Equal to (
==
): Returnstrue
if the operands are equal.1
boolean result = (5 == 3); // result = false
- Not equal to (
!=
): Returnstrue
if the operands are not equal.1
boolean result = (5 != 3); // result = true
- Greater than (
>
): Returnstrue
if the left operand is greater than the right operand.1
boolean result = (5 > 3); // result = true
- Less than (
<
): Returnstrue
if the left operand is less than the right operand.1
boolean result = (5 < 3); // result = false
- Greater than or equal to (
>=
): Returnstrue
if the left operand is greater than or equal to the right operand.1
boolean result = (5 >= 3); // result = true
- Less than or equal to (
<=
): Returnstrue
if the left operand is less than or equal to the right operand.1
boolean result = (5 <= 3); // result = false
Practical Examples
Here are some practical examples that combine arithmetic, logical, and relational operators in Java:
Example 1: Calculating the Average
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Main {
public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
int num3 = 30;
int num4 = 40;
int sum = num1 + num2 + num3 + num4;
double average = sum / 4.0;
System.out.println("Sum: " + sum); // Output: Sum: 100
System.out.println("Average: " + average); // Output: Average: 25.0
}
}
Example 2: Checking Conditions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Main {
public static void main(String[] args) {
int age = 25;
boolean isAdult = age >= 18;
if (isAdult && age < 65) {
System.out.println("You are an adult but not a senior citizen.");
} else if (age >= 65) {
System.out.println("You are a senior citizen.");
} else {
System.out.println("You are not an adult.");
}
}
}
Example 3: Combining Logical and Relational Operators
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 30;
boolean result = (a < b) && (b < c); // result = true
System.out.println("Result: " + result);
result = (a > b) || (b < c); // result = true
System.out.println("Result: " + result);
result = !(a == b); // result = true
System.out.println("Result: " + result);
}
}
Conclusion
Arithmetic, logical, and relational operators are essential tools in Java for performing a wide variety of tasks, from basic calculations to complex logical operations. Understanding how to use these operators effectively is crucial for writing robust and efficient Java code. By mastering these operators, you will be well-equipped to handle the logical and arithmetic needs of your applications.