Model G20 2027 at FLAME University, registrations now open

Operators in Java | ICSE Class 9 Computer Applications Notes

28 min read

On this page

This note covers operators and operands, unary, binary and ternary forms, arithmetic, assignment, increment and decrement, comparisons, logical conditions, counters and accumulators, precedence and associativity, object creation, member access and simple output in Java.

What are operators, operands and expressions?

An operator specifies an operation on a value. An operand is a value on which that operator acts. A variable is a named storage location whose value may change during a program, an ordered set of instructions executed by a computer.

An expression produces a value. It may contain literal values, variables and operators. A literal is a value written directly in the program. Evaluating an expression means working out its value using the operations and values involved.

Definition: An operator tells Java what operation to perform; its operands supply the values for that operation. Distinguishing the operation from the data is the first step in reading an expression.

How do symbols acquire meaning?

For numeric operands, + means addition, - means subtraction, * means multiplication, / means division and % means remainder. A remainder is the amount left after division into whole multiples. These symbols belong to the arithmetic operator group.

A data type specifies the kind of value and the operations permitted on it. Java's int type stores integers, meaning whole numbers including negative values and zero. The double type stores floating-point numbers, which can represent fractional values with finite precision. A type's range is the span of values it can represent.

A statement is an instruction that Java can execute. The assignment operator, =, places a value into a variable. In a simple assignment, Java evaluates the expression on the right and stores a compatible result in the variable on the left.

The semicolon, ;, ends the declaration, assignment and output statements used here. A declaration introduces a variable and its type. Initialisation gives it a starting value. These ideas matter because the same arithmetic symbols can behave differently with different operand types.

How do unary, binary and ternary operators differ?

The form of an operator describes how many operands it takes. Its type describes what it does. These classifications answer different questions. An arithmetic operator can have a unary form or a binary form, depending on its use.

FormNumber of operandsMeaning
UnaryOneActs on a single operand, as unary minus changes the sign of a numeric value.
BinaryTwoActs on two operands, as subtraction finds the left value minus the right value.
TernaryThreeUses a condition and two alternative expressions to select a value.

Can the same symbol have different forms?

Yes. Let x and y stand for numeric variables. In the syntax -x, the minus sign is unary: its operand is x. In x - y, it is binary: its operands are x and y.

Unary plus, written +x, retains the numeric value's sign. Unary minus produces its negative. Neither expression alone assigns a replacement value to x. To store a computed result, an assignment must specify the receiving variable.

The ternary conditional operator is written ? :. The question mark separates the condition from the expression chosen when that condition is true; the colon separates the two alternatives. A condition is an expression whose result is true or false.

Rule: Count operands to identify an operator's form. Identify the operation to classify its type. Do not count the number of printed characters in the operator symbol.

Java's boolean type has the values true and false. These represent whether a condition holds. They are Boolean values, not the quoted words of a message and not substitutes for numeric values.

How are the short programs and their output read?

A class is a specification for objects. An object is an instance of a class. A method is a named block of instructions associated with a class. The worked programs use a class named Operators and a method named main.

A class declaration defines a class in the program. The word class introduces that declaration. Braces, { }, enclose a block, meaning a group of declarations or statements. The outer braces enclose the class; the inner braces enclose the method's instructions.

What does the method header mean?

The header public static void main(String[] args) supplies the conventional application entry method, where execution starts. Here, public permits access, static associates the method with the class, and void means the method returns no value. main is the entry method's name.

String is Java's text type. A string is a sequence of characters. The square brackets in String[] specify an array, a structure holding elements of one declared type. args names the array receiving command-line text arguments, values supplied when launching the application.

Parentheses, ( ), enclose a method's parameter declaration or the arguments supplied when calling it. A parameter is a named input in a method declaration; an argument is a value supplied to a method call. Parentheses also group expressions.

How is a result displayed?

A field is a variable belonging to a class or object. The dot, ., selects a member, meaning a field or method belonging to a class or object. A library class is a reusable class supplied in a collection of programming facilities.

System names a Java library class; out names its standard output field, which refers to the object used for ordinary program output. In System.out, the dot accesses this field through the class.

System.out.println displays its argument and then ends the output line. System.out.print displays its argument without adding a line ending. In these calls, println and print name methods of the output object.

Read each worked program independently. Its declarations give all starting values. The stated output follows when its main method runs. The numbered explanation follows the displayed lines, including the opening declarations and closing braces.

How do addition, subtraction and multiplication work?

Addition combines numeric values; subtraction takes the right operand away from the left; multiplication forms their product. A product is the result of multiplication. The order of operands matters in subtraction, even though both operands are numbers.

When + has a String operand, it performs concatenation, meaning joining text. Let str1 hold "Hello" and str2 hold "India". The expression str1 + str2 produces "HelloIndia". The double quotation marks delimit string literals; they are not part of the text being joined.

Concatenation adds no space automatically between the pieces of text. Numeric addition and string concatenation therefore use the same symbol for different operations. Read the types and grouping of the operands before deciding what a printed expression will contain.

For a rectangle, its area is the product of length and breadth. Area measures the surface enclosed; length and breadth are the rectangle's perpendicular side measurements. Multiplying measurements expressed in the same unit gives an area in square units.

Worked example 1. Find the area of a rectangle whose length is 10 units and breadth is 20 units. The variables length and breadth store these measurements; area stores their product.

class Operators {
public static void main(String[] args) {
int length = 10;
int breadth = 20;
int area = length * breadth;
System.out.println(area);
}
}

Answer: The output is 200. The area is 200 square units.

  1. Line 1 declares the class Operators and opens its body.
  2. Line 2 declares and opens the main method.
  3. Line 3 declares the integer length and initialises it to 10.
  4. Line 4 declares the integer breadth and initialises it to 20.
  5. Line 5 multiplies length by breadth and initialises area with 200.
  6. Line 6 prints area and ends the output line.
  7. Line 7 closes the main method's body.
  8. Line 8 closes the class body.

Property: Subtraction takes the right operand from the left

The left operand is the starting value; the right operand is the value subtracted. In the next program, num1 names the first integer and num2 names the second integer. Their values appear in the declarations before either is used.

Worked example 2. Subtract num2, whose value is 6, from num1, whose value is 5.

class Operators {
public static void main(String[] args) {
int num1 = 5;
int num2 = 6;
System.out.println(num1 - num2);
}
}

Answer: The output is -1.

  1. Line 1 declares and opens Operators.
  2. Line 2 declares and opens main.
  3. Line 3 initialises num1 to 5.
  4. Line 4 initialises num2 to 6.
  5. Line 5 subtracts 6 from 5 and prints -1. It does not change either variable.
  6. Line 6 closes main.
  7. Line 7 closes Operators.

Notice the distinction between computing and storing. The area program stores its product in a variable before displaying it. The subtraction program sends the expression's result directly to the output method. Both methods of displaying a result are valid.

How do division and remainder differ?

The quotient is the result of division. With integer operands, Java's division operator produces an integer quotient by discarding the fractional part towards zero. With double operands, division can produce a fractional result. Check operand types before predicting the output.

Rule: The types of the operands determine whether division is integer division or floating-point division. Merely storing an integer-division result in a double variable does not restore a fractional part already discarded.

Worked example 3. Divide num2 by num1, where num1 is 8 and num2 is 4. Declare both variables as double so that the quotient can retain its fractional part.

class Operators {
public static void main(String[] args) {
double num1 = 8;
double num2 = 4;
System.out.println(num2 / num1);
}
}

Answer: The output is 0.5.

  1. Line 1 opens the class declaration.
  2. Line 2 opens the main method.
  3. Line 3 initialises the double variable num1 with the value 8.
  4. Line 4 initialises the double variable num2 with the value 4.
  5. Line 5 divides 4 by 8 using floating-point division and prints 0.5.
  6. Line 6 closes main.
  7. Line 7 closes the class.

Property: Modulus returns the remainder

The remainder operator, also called the modulus operator in elementary usage, returns the remainder rather than the quotient. For non-negative integer operands and a positive divisor, it answers how much is left after taking out whole multiples of the divisor.

Worked example 4. Find the remainder when num1, initialised to 13, is divided by num2, initialised to 5.

class Operators {
public static void main(String[] args) {
int num1 = 13;
int num2 = 5;
System.out.println(num1 % num2);
}
}

Answer: The output is 3.

  1. Line 1 opens Operators.
  2. Line 2 opens main.
  3. Line 3 initialises num1 to 13.
  4. Line 4 initialises num2 to 5.
  5. Line 5 calculates the remainder of the division and prints 3.
  6. Line 6 closes main.
  7. Line 7 closes Operators.

For integer arithmetic, division or remainder with a zero divisor causes an arithmetic exception, an error reported during execution. A remainder of zero with a non-zero integer divisor indicates exact divisibility. The symbol % does not directly calculate a percentage.

How do assignment and shorthand assignment update variables?

Assignment replaces the value stored in a variable. The right side may be a literal, another variable or a larger expression. Reading the right side does not, by itself, change those variables. The receiving variable is identified on the left.

Shorthand assignment, also called compound assignment, combines an operation with assignment. The symbols +=, -=, *=, /= and %= respectively add, subtract, multiply, divide or take a remainder, then store the result in the left variable.

How can shorthand be read in expanded form?

In this table, x is an int variable holding the value to update, and y is an int value used in that update. Division and remainder require y to be non-zero. The expanded forms describe these simple integer-variable cases.

Shorthand statementExpanded statementUpdate
x += y;x = x + y;Add y to the current x.
x -= y;x = x - y;Subtract y from the current x.
x *= y;x = x * y;Multiply the current x by y.
x /= y;x = x / y;Replace x with the integer quotient.
x %= y;x = x % y;Replace x with the remainder.

Worked example 5. Start with num1 equal to 10 and num2 equal to 2. Add num2 to num1 using shorthand assignment, then display both variables.

class Operators {
public static void main(String[] args) {
int num1 = 10;
int num2 = 2;
num1 += num2;
System.out.println(num1);
System.out.println(num2);
}
}

Answer: The output is:
12
2

  1. Line 1 declares Operators.
  2. Line 2 opens main.
  3. Line 3 initialises num1 to 10.
  4. Line 4 initialises num2 to 2.
  5. Line 5 adds num2 to the current num1 and stores 12 in num1.
  6. Line 6 prints the updated value 12.
  7. Line 7 prints num2, which remains 2.
  8. Line 8 closes main.
  9. Line 9 closes Operators.

Note: The expanded forms above concern simple int variables. Java compound assignment can include an implicit type conversion, an automatic change of type, so it is not an unrestricted textual replacement rule for every type and expression.

How do prefix and postfix changes support counters and accumulators?

Increment means increasing a numeric variable by one, using ++. Decrement means decreasing it by one, using --. A prefix operator appears before its operand; a postfix operator appears after its operand.

Using x as an already initialised int variable, ++x increases x and yields the updated value. x++ increases x but yields its previous value. The variable changes in both cases; the expression value distinguishes the two forms.

ExpressionChange to xValue yielded
++xIncrease by oneThe updated value
x++Increase by oneThe previous value
--xDecrease by oneThe updated value
x--Decrease by oneThe previous value

When the expression's value is discarded and the operation stands alone, prefix and postfix increment produce the same final value in x. In a larger expression, keep the value yielded separate from the value now stored. Postfix does not postpone its update until the next statement.

How does a counter differ from an accumulator?

A counter records how many events have been counted. Let count name an integer counter. Initialising count to zero represents no events counted yet; executing count++; records one additional event.

An accumulator stores a running result. Let total name a numeric accumulator and value the next compatible numeric contribution. The syntax total += value; adds that contribution to the existing total. The contribution need not equal one.

A sum accumulator usually starts at zero, because adding the first contribution to zero preserves that contribution. A product accumulator usually starts at one, because multiplication by one preserves the first factor. The starting value therefore depends on what is being accumulated.

Rule: Initialisation establishes the starting state. An update changes that state after an event or contribution. Reinitialising during every update loses the earlier count or accumulated result.

To trace an update, record the old variable value, the value used by the surrounding expression and the new stored value. A trace is a step-by-step record of execution. This distinction is especially useful when increment or decrement appears inside an assignment.

How do relational operators compare numeric values?

A relational operator compares operands. In numeric comparisons, the result states whether a specified relationship holds. Equality means that the compared values are equal; inequality means they are different. The resulting value has type boolean.

OperatorMeaningWhen the result is true
==Equal toThe two numeric values are equal.
!=Not equal toThe two numeric values differ.
>Greater thanThe left value exceeds the right value.
<Less thanThe left value is smaller than the right value.
>=Greater than or equal toThe left value exceeds or equals the right value.
<=Less than or equal toThe left value is smaller than or equals the right value.

Why are equality and assignment different?

== asks whether values are equal. = performs assignment. Comparing two integer variables does not replace either value. Treating these symbols as interchangeable changes the intended operation and can make the surrounding statement invalid.

Worked example 6. Compare the integers num1 equal to 10 and num2 equal to 0 for equality.

class Operators {
public static void main(String[] args) {
int num1 = 10;
int num2 = 0;
System.out.println(num1 == num2);
}
}

Answer: The output is false.

  1. Line 1 declares Operators.
  2. Line 2 opens main.
  3. Line 3 stores 10 in num1.
  4. Line 4 stores 0 in num2.
  5. Line 5 compares 10 with 0. They are unequal, so it prints false without changing either variable.
  6. Line 6 closes main.
  7. Line 7 closes Operators.

The meanings in the table apply to numeric comparisons. Do not extend them automatically to every kind of object. In particular, comparing references to objects and comparing their text contents are different operations. A reference is a value identifying an object.

How do logical operators combine conditions?

Logical operators combine or reverse Boolean conditions. Java uses && for conditional AND, || for conditional OR and ! for logical NOT. AND requires both conditions to hold; OR requires at least one; NOT reverses a Boolean value.

Let p and q represent Boolean conditions. A truth table lists the result for every combination of their truth values. The expressions p && q and p || q are binary; !p is unary.

pqp && qp || q!p
truetruetruetruefalse
truefalsefalsetruefalse
falsetruefalsetruetrue
falsefalsefalsefalsetrue

What does short-circuit evaluation mean?

Short-circuit evaluation skips the right operand when the left operand already determines the result. For &&, a false left operand makes the whole condition false. For ||, a true left operand makes the whole condition true.

Consequently, a skipped right operand performs no updates or method calls. A method call means invoking the named method's instructions. Java requires Boolean operands here; an integer variable is not automatically a Boolean condition.

Worked example 7. Evaluate the combined condition that 10 is not equal to 9 and 20 is greater than or equal to 20.

class Operators {
public static void main(String[] args) {
System.out.println(10 != 9 && 20 >= 20);
}
}

Answer: The output is true.

  1. Line 1 declares and opens Operators.
  2. Line 2 declares and opens main.
  3. Line 3 evaluates 10 != 9 as true, then 20 >= 20 as true. AND combines them to true, which println displays.
  4. Line 4 closes main.
  5. Line 5 closes Operators.

To negate a complete compound condition, group that condition with parentheses before applying !. Otherwise, the negation may apply only to the following operand. State which condition is being reversed before working out the final truth value.

How does the ternary conditional operator select a value?

The ternary conditional operator chooses between two expressions using a Boolean condition. It is useful when an assignment or output needs one of two alternative values. The condition is evaluated first, and its result determines which alternative is evaluated.

Syntax: condition ? valueIfTrue : valueIfFalse

Here, condition stands for a Boolean expression, valueIfTrue stands for the expression selected when the condition is true, and valueIfFalse stands for the expression selected when it is false. These are descriptive placeholders, not additional Java keywords.

What are its three operands?

  1. The first operand is the condition that Java tests.
  2. The second operand supplies the selected value when the condition is true.
  3. The third operand supplies the selected value when the condition is false.
  4. The resulting value can then be used by a surrounding expression, assignment or output call.

The question mark and colon work together as one operator. The unselected alternative is not evaluated. Its calculations or updates therefore do not take place merely because they appear in the source text. Source text means the program instructions as written.

The alternatives must satisfy Java's type rules, and the resulting value must be suitable for its use. When assigning the result, check the receiving variable's type as well as the values supplied by the alternatives.

Read the expression as “if the condition is true, use the first alternative; otherwise use the second”. It selects a value. Assignment, if present around it, performs the separate task of storing that value.

How do precedence and associativity organise expressions?

Precedence is the priority used to group different operators in an expression. Associativity determines grouping when operators at the same precedence level occur together. Parentheses make a chosen grouping explicit. Grouping determines which values become operands of which operations.

For the operators studied here, the table proceeds from higher to lower precedence. Postfix means the update follows its variable; prefix means it precedes its variable. The arithmetic symbols + and - appear at different levels according to their unary or binary use.

Operator groupSymbolsGrouping at that level
Postfix updatepostfix ++, postfix --Applies to the preceding variable
Unary operatorsprefix ++, prefix --, unary +, unary -, !Right to left
Multiplicative arithmetic*, /, %Left to right
Additive arithmeticbinary +, binary -Left to right
Ordering comparisons<, <=, >, >=Left to right
Equality comparisons==, !=Left to right
Conditional AND&&Left to right
Conditional OR||Left to right
Conditional selection? :Right to left
Assignment=, +=, -=, *=, /=, %=Right to left

Property: Parentheses override operator precedence

Worked example 8. Display the results of 20 + 30 * 40 and (20 + 30) * 40 in that order.

class Operators {
public static void main(String[] args) {
System.out.println(20 + 30 * 40);
System.out.println((20 + 30) * 40);
}
}

Answer: The output is:
1220
2000

  1. Line 1 opens Operators.
  2. Line 2 opens main.
  3. Line 3 groups the multiplication first: 30 * 40 is 1200, then adding 20 gives 1220.
  4. Line 4 groups the addition first: 20 + 30 is 50, then multiplying by 40 gives 2000.
  5. Line 5 closes main.
  6. Line 6 closes Operators.

For the expression 20 - 30 + 40, addition and subtraction have equal precedence. Left-to-right grouping gives (20 - 30) + 40, then -10 + 40, and finally 30. It does not group the addition before the subtraction.

Note: Associativity describes grouping, not permission to rearrange updates. Java evaluates operands from left to right, subject to conditional evaluation. Trace prefix, postfix and short-circuit behaviour instead of mechanically rearranging a mixed expression.

How do new, dot access and output work together?

The new operator creates an object and yields a reference to it. Dynamic memory allocation means allocating memory while a program runs. For class instances, new allocates the object and invokes a constructor, the special mechanism that initialises a newly created object.

Syntax: ClassName reference = new ClassName();

Here, ClassName stands for a declared class, and reference is a variable of that class type. The parentheses after the second ClassName indicate a constructor invocation with no arguments. This form requires an accessible constructor that can be called without arguments.

What is the difference between creation and member access?

Declaring a reference variable and creating an object are distinct actions. A declaration supplies the variable's name and type. In the displayed syntax, new creates the object, and assignment stores the resulting reference in the variable.

The dot operator accesses a member. In the syntax reference.member, member stands for an accessible field of the referenced object. In reference.method(), method stands for an accessible method called without arguments. The actual names must belong to the class being used.

In System.out.println, the first dot accesses out through System; the next dot selects println on the output object. The method's parentheses contain the expression to display. Java evaluates that expression before passing its value to the output method.

How do print and println affect layout?

print leaves subsequent output on the current line unless the printed content itself includes a line break. println adds a line ending after its output. A line break is a control character sequence that moves output to the next line.

When predicting output, track both the value and the layout. Two calls need not produce two lines if both use print. With println, each completed call ends its output line. Creating an object with new does not itself print the object or its fields.

Glossary

  • Operator — A symbol or keyword specifying an operation to perform on its operand or operands.
  • Operand — A value or expression used as input to an operator's operation.
  • Expression — A value-producing part of a program, possibly combining variables, literals and operators.
  • Unary operator — An operator taking one operand, such as numeric negation or logical NOT.
  • Binary operator — An operator taking two operands, such as numeric addition or an equality comparison.
  • Ternary operator — The conditional operator using a Boolean condition and two alternative value expressions.
  • Assignment — Storing a compatible value in the variable identified on the left of the assignment operator.
  • Prefix increment — An update that increases a variable by one and yields its updated value.
  • Postfix increment — An update that increases a variable by one but yields its previous value.
  • Counter — A variable updated to record how many events have been counted.
  • Accumulator — A variable holding a running result as successive contributions are incorporated.
  • Precedence — The relative priority determining grouping when different operators occur in an expression.
  • Associativity — The grouping direction for operators that share a precedence level within an expression.
  • Short-circuit evaluation — Evaluation that skips an operand when the result is already determined by the preceding condition.
  • Reference — A value identifying an object, through which its accessible members can be used.

Common errors and misconceptions

  • Misconception: = and == both compare values. Correct: = assigns a value; == tests equality. Choose the symbol according to the intended operation.
  • Misconception: Division with integer operands retains a fraction when assigned to double. Correct: Integer division discards the fractional part before that assignment occurs.
  • Misconception: % directly calculates a percentage. Correct: In Java arithmetic it calculates a remainder; a percentage requires the appropriate arithmetic expression.
  • Misconception: Prefix and postfix increment yield the same expression value. Correct: Both update the variable, but prefix yields the updated value and postfix the previous value.
  • Misconception: Every part of a logical condition is evaluated. Correct: && and || can skip their right operand when the left operand settles the result.
  • Misconception: A counter and an accumulator must both increase by one. Correct: A counter records events; an accumulator combines contributions, which can have different values.
  • Misconception: All operators group from left to right. Correct: Assignment and conditional selection group from right to left; grouping also depends on precedence and parentheses.
  • Misconception: print and println always produce identical layout. Correct: println adds a line ending, whereas print does not add one automatically.

Exam-style questions with model answers

Q1. Define an operator and an operand in Java. [2 marks]
  1. An operator is a symbol or keyword specifying an operation to perform.
  2. An operand is a value, or an expression producing a value, on which that operator acts.
Q2. A Java program declares int num1 = 13; and int num2 = 5; and executes System.out.println(num1 % num2);. State the output and explain the operator. [2 marks]
  1. The output is 3, followed by a line ending from println.
  2. The % operator returns the remainder when the left operand, 13, is divided by the right operand, 5.
Q3. A Java program declares int num1 = 10; and int num2 = 2;, executes num1 += num2;, then prints num1 and num2 using separate println calls in that order. Give the expanded update and both output lines, with reasons. [3 marks]
  1. For these int variables, the update expands to num1 = num1 + num2;. It adds the old value of num1 to num2 before storing the result.
  2. The first output line is 12, because num1 receives the sum of 10 and 2.
  3. The second output line is 2. Reading num2 during the addition does not replace its stored value.
Q4. Explain four differences or similarities between prefix increment ++x and postfix increment x++, where x is an already initialised int variable whose increment stays within the int range. Consider their position, update, yielded value and effect when used alone. [4 marks]
  1. Prefix increment places ++ before x, whereas postfix increment places ++ after x.
  2. Both forms increase the value stored in x by one during evaluation of the increment expression.
  3. Prefix increment yields the updated value; postfix increment yields the value x held before the update.
  4. When used as standalone statements and their expression values are discarded, both forms leave x with the same final value from the same starting value.
Q5. A Java program executes System.out.println(20 + 30 * 40); followed by System.out.println((20 + 30) * 40);. Explain the grouping, intermediate calculation and result for each line. [6 marks]
  1. In the first expression, multiplication has higher precedence than addition. The grouping is 20 + (30 * 40), so the multiplication supplies the right operand of the addition.
  2. The product 30 * 40 is 1200. The remaining addition therefore combines the original left value 20 with this product.
  3. Adding 20 and 1200 gives 1220. The first println call displays 1220 and then ends the output line.
  4. In the second expression, parentheses explicitly group 20 + 30 together. That grouped sum becomes the left operand of multiplication.
  5. The grouped addition produces 50. The remaining calculation is multiplication of that result by the unchanged right operand, 40.
  6. Multiplying 50 by 40 gives 2000. The second println call displays 2000 on the next line and ends that line.
Q6. Evaluate System.out.println(10 != 9 && 20 >= 20);. Explain each comparison, whether the right comparison is evaluated, and the final output. [4 marks]
  1. The left comparison, 10 != 9, is true because the two numeric values are unequal.
  2. Because the left operand of && is true, Java must evaluate the right operand to determine the combined result.
  3. The right comparison, 20 >= 20, is true because the operator includes equality as well as a greater value.
  4. Both conditions are true, so their conditional AND is true. The call prints true and ends the output line.
Q7. Explain the roles of new, the dot operator and println in Java object creation, member access and output. Give one separate point for each. [3 marks]
  1. For a class instance, new allocates an object, invokes its constructor and yields a reference that can be stored in a suitable variable.
  2. The dot operator selects an accessible member, such as a field or method, through a class name or an object reference.
  3. The println method used through System.out displays its argument and adds a line ending, separating subsequent output onto the following line.
Q8. Explain how condition ? valueIfTrue : valueIfFalse works. Here condition is a Boolean expression and the other two names stand for alternative expressions satisfying Java's type rules. Give three points. [3 marks]
  1. Java evaluates condition first to obtain a Boolean value. This is the first of the conditional operator's three operands.
  2. If condition is true, Java evaluates valueIfTrue and uses its value. The valueIfFalse alternative is not evaluated in this case.
  3. If condition is false, Java evaluates valueIfFalse instead and uses that value. The valueIfTrue alternative is then skipped; both alternatives are not executed.

Key takeaways

  • Classify an operator by both its purpose and its number of operands; these are different descriptions.
  • Check operand types before division: integer division discards the fractional part, while double division can preserve fractions.
  • Assignment stores a value, equality compares values, and shorthand assignment combines a calculation with an update.
  • Prefix and postfix increment both change a variable, but they yield different values to a surrounding expression.
  • Counters record events, while accumulators combine contributions; choose an initial value appropriate to the intended running result.
  • Use precedence, associativity and parentheses to establish grouping, then trace updates and conditional evaluation carefully.
  • Logical AND and OR short-circuit when the left condition already determines the result of the whole expression.
  • The new operator creates objects, dot access selects members, and print or println controls simple output layout.

Test yourself

What distinguishes a unary operator from a binary operator?

A unary operator takes one operand; a binary operator takes two operands.

What does 13 % 5 produce, and why?

It produces 3, the remainder after dividing 13 into whole multiples of 5.

What is displayed by num2 / num1 when both are double variables, num1 is 8 and num2 is 4?

The expression produces 0.5 because the operands use floating-point division.

Does postfix increment leave the variable unchanged?

No. It increases the variable by one but yields its previous value.

When does conditional AND skip its right operand?

It skips the right operand when the left operand is false.

Which alternative does a false ternary condition select?

It selects the expression after the colon and skips the expression before it.

What is the value of 20 - 30 + 40?

It is 30: equal-precedence subtraction and addition group from left to right.

How does println differ from print?

println adds a line ending after output; print does not add one automatically.