Class 9th ICSE Notes: Operators in Java
Operators in Java
Operators are symbols that perform specific operations on variables and values. Java provides a
wide range of operators to handle different types of operations.
Arithmetical Expression
- An arithmetical expression is a combination of numbers, variables, and operators that evaluate to a
value.
Example: a + b * c
- Arithmetic Statements are used to perform computations and assign values to variables.
Example: int sum = a + b;
Types of Operators
1. Arithmetical Operators
Used for performing basic mathematical operations.
Types:
1. Unary Operators: Operate on a single operand.
a. Prefix: The operator comes before the operand.
Example:
int x = 5;
System.out.println(++x); // Output: 6
b. Postfix: The operator comes after the operand.
Example:
int x = 5;
System.out.println(x++); // Output: 5
2. Binary Operators: Operate on two operands.
Example:
int a = 10, b = 5;
System.out.println(a + b); // Output: 15
3. Ternary Operator: A conditional operator that uses three operands.
Syntax: condition ? expression1 : expression2
Example:
int a = 10, b = 20;
int max = (a > b) ? a : b; // Output: 20
2. Relational Operators
Used to compare two values.
Examples:
- > Greater than
- < Less than
- >= Greater than or equal to
- <= Less than or equal to
- == Equal to
- != Not equal to
Example:
int a = 5, b = 10;
System.out.println(a < b); // Output: true
3. Logical Operators
Used to perform logical operations.
Examples:
- && Logical AND
- || Logical OR
- ! Logical NOT
Example:
int a = 10, b = 20;
System.out.println(a > 5 && b < 30); // Output: true
Examples of Arithmetical Operators
Addition
int a = 10, b = 20;
System.out.println(a + b); // Output: 30
Subtraction
int a = 50, b = 30;
System.out.println(a - b); // Output: 20
Multiplication
int a = 10, b = 5;
System.out.println(a * b); // Output: 50
Division
int a = 20, b = 4;
System.out.println(a / b); // Output: 5
Modulo (Remainder)
int a = 29, b = 5;
System.out.println(a % b); // Output: 4
Key Points
- Unary Operators: Work on one operand; used for increment/decrement.
- Binary Operators: Require two operands; perform addition, subtraction, etc.
- Ternary Operator: Acts as a shortcut for if-else conditions.
- Relational Operators: Compare two values and return a boolean result.
- Logical Operators: Combine multiple conditions.