Operators in Java
Operators in Java are symbols that perform operations on variables and values. Java supports various types of operators, such as arithmetic, assignment, comparison, and logical operators. Understanding these operators is essential for writing efficient and error-free code.
a. Arithmetic Operations
This method performs basic arithmetic and demonstrates increment/decrement operators.
- Parameters: Two integers (
a
andb
). - Operations:
- Addition (
a + b
) - Subtraction (
a - b
) - Multiplication (
a * b
) - Division (
a / b
) - Modulus (
a % b
) - Post-increment (
a++
): Value is used before incrementing. - Pre-increment (
++a
): Value is incremented before use. - Post-decrement (
b--
): Value is used before decrementing. - Pre-decrement (
--b
): Value is decremented before use.
- Addition (
- Output: Prints the result of each operation.
b. Assignment Operations
This method demonstrates compound assignment operators.
- Parameters: Two integers (
a
andb
). - Operations:
a += b
(Addition assignment)a -= b
(Subtraction assignment)a *= b
(Multiplication assignment)a /= b
(Division assignment)a %= b
(Modulus assignment)
- Output: Prints the result of each assignment.
c. Comparison Operations
This method demonstrates relational operators.
- Parameters: Two integers (
a
andb
). - Operations:
- Equality (
a == b
) - Inequality (
a != b
) - Greater than (
a > b
) - Less than (
a < b
) - Greater than or equal to (
a >= b
) - Less than or equal to (
a <= b
)
- Equality (
- Output: Prints the result of each comparison.
d. Logical Operations
This method demonstrates logical operators.
- Parameters: Two integers (
a
andb
). - Operations:
- Logical AND (
&&
): True if both conditions are true. - Logical OR (
||
): True if at least one condition is true. - Logical NOT (
!
): Negates the condition.
- Logical AND (
- Output: Prints the result of each logical operation.
Key Points to Remember
-
Arithmetic Operators:
- Postfix (
a++
,b--
) vs. Prefix (++a
,--b
) behavior. - Integer division truncates the result (e.g.,
5 / 2
results in2
).
- Postfix (
-
Assignment Operators:
- Compact syntax for updating variables (e.g.,
a += b
instead ofa = a + b
).
- Compact syntax for updating variables (e.g.,
-
Comparison Operators:
- Used to compare two values, returning a boolean result.
-
Logical Operators:
- Combine multiple conditions and return a boolean result.
Example Code
import java.util.Scanner;
public class Operators {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: ");
int a = input.nextInt();
System.out.print("Enter the second number: ");
int b = input.nextInt();
// Arithmetic Operations
System.out.println("Sum: " + (a + b));
System.out.println("Subtract: " + (a - b));
System.out.println("Multiply: " + (a * b));
System.out.println("Divide: " + (a / b));
System.out.println("Mod: " + (a % b));
System.out.println("Increment: " + a++);
System.out.println("Increment2: " + ++a);
System.out.println("Decrement: " + b--);
System.out.println("Decrement2: " + --b);
// Assignment Operations
a = 5;
b = 3;
a += b;
System.out.println("a += b: " + a);
a = 5;
b = 3;
a -= b;
System.out.println("a -= b: " + a);
a = 5;
b = 3;
a *= b;
System.out.println("a *= b: " + a);
a = 5;
b = 3;
a /= b;
System.out.println("a /= b: " + a);
a = 5;
b = 3;
a %= b;
System.out.println("a %= b: " + a);
// Comparison Operations
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 < b: " + (a < b));
System.out.println("a >= b: " + (a >= b));
System.out.println("a <= b: " + (a <= b));
// Logical Operations
a = 5;
b = 3;
System.out.println("a == b && a > b: " + (a == b && a > b));
System.out.println("a == b || a > b: " + (a == b || a > b));
System.out.println("!(a == b): " + !(a == b));
}
}
Example Run
Input:
Enter the first number: 5
Enter the second number: 3
Output:
Sum: 8
Subtract: 2
Multiply: 15
Divide: 1
Mod: 2
Increment: 5
Increment2: 7
Decrement: 3
Decrement2: 1
a += b: 8
a -= b: 5
a *= b: 15
a /= b: 5
a %= b: 2
a == b: false
a != b: true
a > b: true
a < b: false
a >= b: true
a <= b: false
a == b && a > b: false
a == b || a > b: true
!(a == b): true