Arithmetic, relational, logical operators
Operators are fundamental components in programming languages that allow developers to perform various operations on variables and values. In Java, operators are classified into different categories, including arithmetic, relational, and logical operators. In this post, we will explore these operators, their functionalities, and provide Java code examples to illustrate their usage.
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations on numeric values. Java supports the following arithmetic operators:
- Addition (
+
) - Subtraction (
-
) - Multiplication (
*
) - Division (
/
) - Modulus (
%
)
Java Code Example:
public class ArithmeticOperatorsExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
// Addition
int sum = a + b;
System.out.println("Sum: " + sum);
// Subtraction
int difference = a - b;
System.out.println("Difference: " + difference);
// Multiplication
int product = a * b;
System.out.println("Product: " + product);
// Division
int quotient = a / b;
System.out.println("Quotient: " + quotient);
// Modulus
int remainder = a % b;
System.out.println("Remainder: " + remainder);
}
}
2. Relational Operators
Relational operators are used to compare two values and determine the relationship between them. These operators return a boolean result (true
or false
). Java provides the following relational operators:
- Equal to (
==
) - Not equal to (
!=
) - Greater than (
>
) - Less than (
<
) - Greater than or equal to (
>=
) - Less than or equal to (
<=
)
Java Code Example:
public class RelationalOperatorsExample {
public static void main(String[] args) {
int x = 10;
int y = 5;
// Equal to
System.out.println("Equal to: " + (x == y));
// Not equal to
System.out.println("Not equal to: " + (x != y));
// Greater than
System.out.println("Greater than: " + (x > y));
// Less than
System.out.println("Less than: " + (x < y));
// Greater than or equal to
System.out.println("Greater than or equal to: " + (x >= y));
// Less than or equal to
System.out.println("Less than or equal to: " + (x <= y));
}
}
3. Logical Operators
Logical operators are used to perform logical operations on boolean values. Java supports the following logical operators:
- Logical AND (
&&
) - Logical OR (
||
) - Logical NOT (
!
)
Java Code Example:
public class LogicalOperatorsExample {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
// Logical AND
System.out.println("Logical AND: " + (a && b));
// Logical OR
System.out.println("Logical OR: " + (a || b));
// Logical NOT
System.out.println("Logical NOT: " + (!a));
}
}
Understanding and mastering arithmetic, relational, and logical operators is essential for writing effective and efficient Java programs. These operators enable developers to manipulate and compare data, making them foundational tools in programming. The provided code examples should serve as a helpful reference for incorporating these operators into your Java projects.