Operators in Java
ICSE Class 9 · Computer Applications · Chapter 4
What this chapter covers
- Operators, operands and expressions
- Forms of operators: unary, binary and ternary
- Arithmetic operators
- Relational operators
- Logical operators
- Assignment and shorthand operators
- Increment and decrement operators
- The conditional (ternary) operator
- Counters and accumulators
- Hierarchy of operators: precedence and associativity
- The new and dot operators
- Simple output: print() and println()
Key points
- An operator acts on operands; an expression combines operators and operands to produce one value of a definite type.
- By form, operators are unary (one operand), binary (two) or ternary (three); `?:` is Java's only ternary operator.
- The arithmetic operators are `+`, `-`, `*`, `/` and `%`; integer division drops the fractional part, and `%` gives the remainder with the sign of the first operand.
- `+` joins strings when either operand is a `String`.
- The relational operators `<`, `>`, `<=`, `>=`, `==` and `!=` give `boolean` results; `==` compares, `=` assigns.
- The logical operators are `&&`, `||` and `!`; `&&` and `||` use short-circuit evaluation.
- `=` assigns the right-hand value to the left-hand variable and is right-associative; shorthand operators such as `+=` and `%=` combine an operation with assignment and convert the result to the variable's type.
- `++` and `--` change a variable by 1; prefix means change then use, postfix means use then change.
- A counter adds a fixed step to count events; an accumulator adds varying values to build a total; both are usually initialised to 0 (1 for a product).
- Precedence decides which operator is applied first; associativity decides the order among operators of equal precedence; brackets override both.
- The `new` operator allocates memory for an object at run time; the dot operator accesses an object's or a class's members.
- `System.out.print()` keeps the cursor on the same line, while `System.out.println()` moves to the next line.
- Bitwise and shift operators are not part of the ICSE syllabus.
Questions answered
Define an operator and an operand. Identify both in `perimeter = 2 * (l + b);`.
Classify the following by form: `!`, `%`, `?:`, `--`, `>=`.
If `int m = 47, n = 6;`, find (a) `m / n` (b) `m % n` (c) `m / (double) n` rounded to two decimal places (d) `-m % n`.
Write the Java expression for (a2 + b2)/(2ab).
Distinguish between `=` and `==`.
What is short-circuit evaluation? Why does `(x != 0) && (y / x > 1)` never cause a division-by-zero error?
Distinguish between the prefix and postfix forms of the increment operator, with an example.
If `int p = 8;`, find the value of `q` and the final value of `p` after `q = p++ * 2 + --p;`.
Past board exam questions
ICSE 2027 sample paper · 1 mark
Evaluate the given expression and identify the result 3*5%7+8/3*9;
- 15
- 19
- 18
- 19.0
Show answer
ICSE 2027 sample paper · 1 mark
What is the value stored in z: z *= z++ + ++z; where z = 2
- 6
- 4
- 12
- 10
Show answer
ICSE 2027 sample paper · 1 mark
Match the operators in Column A to the type of operators in Column B: | Column A | Column B | |---|---| | 1. + | (a) Logical | | 2. > | (b) Mathematical | | 3. && | (c) Relational |
- 1-(a), 2-(b), 3-(c)
- 1-(b), 2-(c), 3-(a)
- 1-(c), 2-(a), 3-(b)
- 1-(b), 2-(a), 3-(c)
Show answer
ICSE 2027 sample paper · 2 marks
Write the Java expression for the following: (mn + |m|)/(m + n)
Show answer
ICSE 2027 sample paper · 2 marks
Evaluate the following expression: 13*7%6+8/5*7;
Show answer
ICSE 2027 sample paper · 2 marks
Differentiate between = and = =.
Show answer
ICSE 2027 sample paper · 2 marks
Rose wrote the following code to check whether a number is an even number or not, and print an appropriate message: ```java if(n%2==0) System.out.println("even Number"); else System.out.println("Not an even number"); ``` She asked Raj to rewrite the code using the ternary operators. What would be the code written by Raj?
Show answer
ICSE 2027 sample paper · 2 marks
What is the value of ch? ```java int x = 65; char ch=(char) (x++ + 5); ```