4.1 Arithmetic Operators:
In Java, arithmetic operators allow you to perform mathematical operations. Here are some common arithmetic operators:
- + Addition: Adds two values together.
- - Subtraction: Subtracts one value from another.
- * Multiplication: Multiplies two values.
- / Division: Divides one value by another.
- % Modulus: Returns the remainder of a division.
Let's look at an example that demonstrates the usage of arithmetic operators:
public class ArithmeticOperators{
public static void main(String[] args) {
int a = 10;
int b = 4;
int sum = a + b; // Sum: 14
int difference = a - b; // Difference: 6
int product = a * b; // Product: 40
int quotient = a / b; // Quotient: 2
int remainder = a % b; // Remainder: 2
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
System.out.println("Remainder: " + remainder);
}
}
- In this example, we perform various arithmetic operations using the values of a and b and store the results in separate variables.
- We then print the values of these variables.
4.2 Assignment Operators:
- Assignment operators are used to assign values to variables. The most common assignment operator is =
- Here's an example:
public class AssignmentOperators{
public static void main(String[] args) {
int x = 5;
int y = 10;
x += 3; // Equivalent to x = x + 3 -> x: 8
y *= 2; // Equivalent to y = y * 2 -> y: 20
System.out.println("x: " + x);
System.out.println("y: " + y);
}
}
4.3 Relational Operators:
Relational operators are used to compare values and determine the relationship between them. Here are some common relational operators:
- == Equality: Checks if two values are equal.
- != Inequality: Checks if two values are not equal.
- > Greater than: Checks if one value is greater than another.
- < Less than: Checks if one value is less than another.
- >= Greater than or equal to: Checks if one value is greater than or equal to another.
- <= Less than or equal to: Checks if one value is less than or equal to another.
Let's see an example that uses relational operators:
public class RelationalOperators{
public static void main(String[] args) {
int a = 5;
int b = 10;
boolean isEqual = (a == b); // Is Equal: false
boolean isNotEqual = (a != b); // Is Not Equal: true
boolean isGreater = (a > b); // Is Greater: false
boolean isLess = (a < b); // Is Less: true
boolean isGreaterOrEqual = (a >= b); // Is Greater or Equal: false
boolean isLessOrEqual = (a <= b); // Is Less or Equal: true
System.out.println("Is Equal: " + isEqual);
System.out.println("Is Not Equal: " + isNotEqual);
System.out.println("Is Greater: " + isGreater);
System.out.println("Is Less: " + isLess);
System.out.println("Is Greater or Equal: " + isGreaterOrEqual);
System.out.println("Is Less or Equal: " + isLessOrEqual);
}
}
4.4 Logical Operators:
Logical operators allow you to combine multiple conditions and perform logical operations. The common logical operators are:
- && Logical AND: Returns true if both conditions are true.
- || Logical OR: Returns true if at least one condition is true.
- ! Logical NOT: Negates the result of a condition.
Here's an example that demonstrates the usage of logical operators:
public class LogicalOperators{
public static void main(String[] args) {
int x = 5;
int y = 10;
boolean result1 = (x > 0) && (y < 20); // Result 1: true
boolean result2 = (x > 0) || (y > 20); // Result 2: true
boolean result3 = !(x > 0); // Result 3: false
System.out.println("Result 1: " + result1);
System.out.println("Result 2: " + result2);
System.out.println("Result 3: " + result3);
}
}
- In this example, we use logical operators to combine conditions and store the results in boolean variables.
- We then print the values of these variables.
4.5 Increment and Decrement Operators:
- The increment (++) and decrement (--) operators are used to increase or decrease the value of a variable by 1.
- Here's an example:
public class IncrementDecrementOperators{
public static void main(String[] args) {
int count = 5;
count++; // Increment count by 1 -> Count: 6
System.out.println("Count: " + count);
count--; // Decrement count by 1 -> Count: 5
System.out.println("Count: " + count);
}
}
- In this example, we increment the value of the count variable by 1 using the ++ operator and then decrement it by 1 using the -- operator.
4.6 Operator Precedence:
- Operator precedence determines the order in which operators are evaluated in an expression. It's important to understand the precedence rules to avoid unexpected results.
- Here's an example:
public class OperatorPrecedenceExample {
public static void main(String[] args) {
int result = 5 + 2 * 3; // Result will be 11, not 21
System.out.println("Result: " + result);
}
}
- In this example, even though multiplication (*) has higher precedence than addition (+), the addition operation is evaluated first because of the left-to-right associativity rule. To enforce the desired order, you can use parentheses to group expressions.