Java Primitive Types and Expression Evaluation

A focused guide to Java primitive types, expression evaluation, numeric conversion, floating-point behavior, integer overflow, and common methods in the Math class for AP Computer Science A.

Primitive Values and Variables

Java programs store values in variables and combine values in expressions. A is a basic Java type whose values are handled directly by the language. The three central AP Computer Science A types are , , and .

  • stores whole-number values.

  • stores decimal or floating-point values.

  • stores either true or false.

A variable has a declared type, a name, and optionally an initial value. For example, students = 28; declares an variable, while schoolOpen = true; declares a variable. The value stored in a variable may change during execution, but its declared type does not change.

A is a fixed value written directly in Java source code, such as 12, 4.99, or false. An integer can be assigned to a because Java can perform a : value = 6; stores the value 6.0. The reverse direction is not automatically safe because a may contain a fractional part.

Takeaway: Identify the declared type of every variable and the type of each or subexpression assigned to it.

Operators and Evaluation Order

Java's arithmetic operators are +, -, *, /, and %. The unary operators + and - can indicate a number's sign. The result of an expression depends on both the operators and the types of its operands.

determines evaluation order. Java evaluates parentheses first, then multiplication, division, and remainder, and finally addition and subtraction. Operators at the same precedence level are evaluated from left to right. Thus, 2 + 3 * 4 produces 14, whereas (2 + 3) * 4 produces 20. The expression 20 / 5 * 2 is evaluated from left to right and produces 8.

The + operator also performs concatenation when either operand is a String. Evaluation still proceeds from left to right. Therefore, "Total: " + 2 + 3 produces "Total: 23", while "Total: " + (2 + 3) produces "Total: 5".

Use parentheses whenever they clarify the intended grouping, particularly when arithmetic is combined with string concatenation.

Takeaway: Trace an expression in precedence order, and remember that a String changes the behavior of + from addition to concatenation.

Integer Arithmetic and Remainders

When both operands are values, Java performs for /. The fractional part is discarded by rounding toward zero: 7 / 3 produces 2, 10 / 4 produces 2, and -7 / 3 produces -2. Assigning the result to a afterward does not restore the discarded fraction, so value = 7 / 3; stores 2.0.

To preserve the fraction, at least one operand must be a . For example, 7.0 / 3 produces an approximate decimal result. A cast before division also works: () 7 / 3. However, () (7 / 3) is too late because has already produced 2.

The % gives the remainder after division. For example, 17 % 5 produces 2. A common divisibility test is number % 2 == 0, which is true when number is even. With integer operands, the remainder keeps the sign of the dividend, so -5 % 3 produces -2.

An or remainder operation with a zero divisor causes an ArithmeticException at run time. Integer calculations can also exceed the fixed range of . This does not automatically create a larger value; adding one to 2_147_483_647 produces the wrapped value -2_147_483_648.

Takeaway: Before evaluating / or %, check the operand types and confirm that a divisor is not zero.

Floating-Point Behavior

A uses floating-point representation. supports fractional values and a large range of magnitudes, but some decimal values cannot be represented exactly in binary. As a result, an expression such as 0.1 + 0.2 may produce a value commonly displayed as 0.30000000000000004 rather than an exact 0.3.

When comparing calculated values, use a tolerance when appropriate. Compute the absolute difference with Math.abs(actual - expected) and check whether it is less than a small threshold such as 0.000001. This tests whether the values are sufficiently close instead of requiring exact binary equality.

Floating-point division by zero behaves differently from . It can produce special values such as positive infinity, negative infinity, or NaN, rather than throwing the exception.

Takeaway: Use for exact whole-number calculations within range, and treat calculated values as potentially approximate.

and Numeric Conversion

explicitly converts an expression by using the form (targetType) expression. a to an discards the decimal portion and moves toward zero: () 7.9 produces 7, and () -7.9 produces -7. A cast does not round to the nearest integer.

Use Math.round when rounding is intended. Math.round(6.7) produces 7 as a long, whereas () 6.7 produces 6. The two operations have different purposes.

A conversion from to is a . A conversion from to is a because information may be lost, so Java requires an explicit cast. Numeric promotion also occurs inside expressions: when an arithmetic operation includes a , an operand is generally promoted to before that operation.

The location of a cast matters. With x = 7; and y = 3;, x / y produces 2, but () x / y produces an approximate value of 2.33332.3333. By contrast, () (x / y) produces 2.0 because the happens first.

Takeaway: Cast an operand before division to preserve a fractional result, and use Math.round rather than when the goal is rounding.

The

The supplies static methods for common calculations. Call these methods with the class name, such as Math.sqrt(25.0), which produces 5.0. Common methods include:

  • Math.abs(x) for absolute value.

  • Math.pow(a, b) for raising one value to a power; its result is a .

  • Math.sqrt(x) for a square root.

  • Math.max(a, b) and Math.min(a, b) for the larger or smaller value.

  • Math.round(x) for rounding a floating-point value to an integral result.

  • for a pseudorandom from 0.00.0 inclusive to 1.01.0 exclusive.

For a square, multiplication such as side * side is often clearer than Math.pow(side, 2). A distance calculation can use Math.sqrt and Math.pow with horizontal and vertical differences as the two squared components.

To generate an integer from 0 through 9, use () ( * 10). More generally, an integer from lower through upper, inclusive, can be generated with () ( * (upper - lower + 1)) + lower. For a six-sided die, () ( * 6) + 1 gives a value from 1 through 6.

Takeaway: Check the return type and interval of each Math method, especially when converting a random into an integer range.

Tracing Types and Values

When tracing Java code, determine both the type and the value of each important subexpression. Consider a = 9;, b = 4;, and result = a / b + 0.5;.

  1. a / b uses two operands, so produces 2.

  2. The expression 2 + 0.5 includes a , so the addition produces 2.5.

  3. The final variable stores 2.5.

The result is not 2.75, because the fractional part was discarded before 0.5 was added. To obtain the decimal quotient, use result = () a / b + 0.5;. The cast changes the division itself, so the quotient is approximately 2.252.25, and the final result is 2.752.75.

A reliable tracing process is:

  1. Record each variable's declared type and current value.

  2. Apply parentheses and .

  3. Determine whether each arithmetic operation is integer or .

  4. Apply casts and numeric promotion at the point where they occur.

  5. Continue until the final assignment or output is reached.

Takeaway: Do not infer the final type or value from the assignment alone; trace every operation in order.