4.1.1.2, 4.1.1.3, 4.1.1.6 - Variables, constants and arithmetic (Java)

4.1.1.2, 4.1.1.3, 4.1.1.6 - Variables, constants and arithmetic (Java)

Track declaration and assignment in sequence, use named constants, and choose the right Java operation for division, powers, rounding and truncation.

Java route status: incomplete: runnable support pending. Code answers use written Java practice.

Declaration, assignment and sequence

A variable holds a current value, and an assignment replaces that value when its statement is reached.

Java
int stock = 20;
int sold = 3;
stock = stock - sold;
sold = sold + 1;
System.out.println(stock);

The declaration associates an identifier with a type. Initialisation supplies its first value. Assignment uses =: evaluate the expression on the right using current values, then store the result on the left. Here stock becomes 17. Increasing sold to 4 later does not recalculate an earlier assignment, so the output stays 17. stock -= sold is a shorter form of the stock update; sold++ adds one.

Sequence means statements execute in order. Selection chooses a branch; iteration repeats a body. A subroutine groups statements to be called when needed. Java's main is a method, and System.out.println is a method call. Selection, iteration and your own procedures/functions receive their full treatment in the following lessons: declaring data and making assignments support all three combining principles.

Use meaningful identifiers such as ticketsSold instead of x: the purpose and units become easier to recognise, so a maintenance change is less likely to confuse quantities. A name does not enforce a requirement by itself; the code must still update the right variable. Java uses braces to group statements and semicolons to end ordinary statements.

Named constants

A constant gives a fixed value a meaningful name so the program expresses a rule clearly.

Java
final double PRICE_PER_TICKET = 7.5;
int tickets = 4;
double total = tickets * PRICE_PER_TICKET;
System.out.println(total);

A variable such as tickets may be reassigned; a local final variable may be assigned only once. Trying to reassign this price is a compile-time error. Uppercase words separated by underscores are a common convention for named constants, not the mechanism that makes them constant. final is the mechanism.

The output is 30.0. The name explains the meaning of 7.5. If the price changes for the next version, editing one declaration changes all uses consistently. Repeated unexplained numbers can be missed or changed inconsistently. A constant can still be used in arithmetic and conditions. For object references, final prevents reassignment of the reference; it does not automatically make the referenced object's fields immutable.

Division, remainders and powers

The operand types affect the result of division, so choose them before calculating.

Java uses +, - and * for addition, subtraction and multiplication. Multiplication and division bind before addition and subtraction; parentheses make the intended grouping explicit. With integer operands, / performs integer division and discards the fractional part toward zero. % gives the remainder. For positive counts, 17 / 5 is 3 and 17 % 5 is 2: three complete groups, with two items left.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account