PrepGo

Expressions and Output - AP Computer Science A Study Guide

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Learn with study guides reviewed by top AP teachers. This guide takes about 14 minutes to read.

Getting Started

Nearly every useful computer program needs to perform calculations and show the results to a user. To accomplish these fundamental tasks, we must learn how Java handles mathematical computations and how it can print information to the screen. This chapter introduces expressions, the core building block for all calculations, and the commands used to produce console output.

What You Should Be Able to Do

  • Evaluate arithmetic expressions that use integers and floating-point numbers.

  • Apply operator precedence rules to determine the order of evaluation in a complex expression.

  • Use casting to deliberately convert a value from one primitive data type to another.

  • Write code to display variable values, string literals, and expression results to the console.

  • Combine strings with other data types for formatted output using the concatenation operator.

Key Concepts & Java Implementation

The Core Idea

In Java, an expression is a combination of values, variables, and operators that the computer evaluates to produce a single, resulting value. The simplest expression is a literal value (like 5 or 3.14), but they are typically more complex, such as 5 + 3. The values or variables in an expression are called operands, and the symbols that perform actions on them (like + or *) are called operators.

The data type of the operands is critical. When an arithmetic expression involves only int values, the result will be an int. If it contains at least one double value, the entire expression is "promoted," and the result will be a double. This distinction is especially important for division, as dividing two integers in Java results in an integer, with any fractional part being truncated (discarded).

Once an expression is evaluated, we often need to display its result. Java provides built-in methods for printing output to a standard console, allowing us to see the outcome of our program's computations.

Syntax & Implementation

Java uses standard mathematical operators. Their behavior depends on the data types of the operands.

Arithmetic Operators Table

OperatorPurposeJava Example (int x = 10, y = 4;)Result
+Additionx + y14
-Subtractionx - y6
*Multiplicationx * y40
/Divisionx / y2
%Modulo (Remainder)x % y2

Annotated Java Examples

  1. Operator Precedence

    Java evaluates expressions using a well-defined order of operations, similar to PEMDAS in mathematics. Multiplication (*), division (/), and modulo (%) have higher precedence than addition (+) and subtraction (-).

    
    // Multiplication is performed before addition.
    
    int result = 5 + 3 * 2; // Evaluates 3 * 2 first, resulting in 6. Then 5 + 6.
    
    System.out.println(result); // Prints 11
    
    
    
    // Parentheses can be used to override the default precedence.
    
    int anotherResult = (5 + 3) * 2; // Evaluates 5 + 3 first, resulting in 8. Then 8 * 2.
    
    System.out.println(anotherResult); // Prints 16
    
  2. Integer vs. Double Division

    The result of the / operator depends on its operands' types.

    
    // When both operands are integers, the result is an integer (truncated).
    
    int intDivision = 7 / 2;
    
    System.out.println(intDivision); // Prints 3, not 3.5
    
    
    
    // If at least one operand is a double, the result is a double.
    
    double doubleDivision = 7.0 / 2;
    
    System.out.println(doubleDivision); // Prints 3.5
    
  3. Casting

    Casting is an operation that explicitly converts a value of one data type into another. This is often used to force floating-point division when you have integer variables.

    
    int numerator = 7;
    
    int denominator = 2;
    
    
    
    // Cast the numerator to a double BEFORE the division occurs.
    
    double preciseResult = (double) numerator / denominator;
    
    System.out.println(preciseResult); // Prints 3.5
    
    
    
    // Casting can also convert a double to an int (truncating the decimal).
    
    double value = 9.8;
    
    int truncatedValue = (int) value;
    
    System.out.println(truncatedValue); // Prints 9
    
  4. Output and String Concatenation

    To print output, we use System.out.print() or System.out.println(). The + operator, when used with a String, performs concatenation, which joins the string with the other operand's string representation.

    
    String label = "Score: ";
    
    int score = 95;
    
    
    
    // System.out.println() prints the text and moves to a new line.
    
    System.out.println("Hello, World!");
    
    
    
    // System.out.print() prints the text and leaves the cursor on the same line.
    
    System.out.print("Player 1");
    
    System.out.print(" is winning.");
    
    
    
    // Use concatenation to combine strings and variables for formatted output.
    
    // The int 'score' is automatically converted to a String.
    
    System.out.println(label + score); // Prints "Score: 95" on a new line
    

Tracing & Analysis

  • Execution Trace

    Let's trace the evaluation of a complex expression to see precedence rules in action.

    Code:int finalValue = 10 + 8 / 2 * 3 % 5;

    1. 8 / 2 is evaluated first (division has high precedence, evaluated left-to-right): 10 + 4 * 3 % 5

    2. 4 * 3 is evaluated next (multiplication): 10 + 12 % 5

    3. 12 % 5 is evaluated next (modulo has same precedence as * and /): 10 + 2

    4. 10 + 2 is evaluated last (addition has lower precedence): 12

    5. The final value assigned to finalValue is 12.

  • Analysis

    A common logical error occurs when programmers forget about integer division. Consider calculating an average: int sum = 15; int count = 2; double average = sum / count;. The variable average will hold 7.0, not 7.5. This is because the integer division 15 / 2 is performed first, resulting in 7. That integer 7 is then assigned to the double variable, becoming 7.0. To get the correct result, you must cast one of the operands: double average = (double) sum / count;.

Java Syntax Quick-Reference

A summary of the key syntax introduced in this topic.

SyntaxPurpose
+, -, *, /, %Standard arithmetic operators for addition, subtraction, multiplication, division, and modulo.
(type) expressionCasts the result of the expression to the specified type (e.g., (double) 5).
System.out.println(value)Prints the value to the console, followed by a new line character.
System.out.print(value)Prints the value to the console without moving to a new line.
String + valueConcatenates (joins) a String with another value, converting the value to its String form first.

Core Code Examples & Terminology

  • Expression: A combination of literals, variables, and operators that evaluates to a single value.

  • Operator: A symbol (e.g., +, *) that performs an operation on one or more operands.

  • Casting: The explicit conversion of a data type to another, such as (int) or (double).

  • Concatenation: The operation of joining character strings end-to-end using the + operator.

  • Core Snippet 1 (Operator Precedence)

    
    int result = 10 - 2 * 3; // result is 4
    

    This demonstrates that multiplication (*) is performed before subtraction (-).

  • Core Snippet 2 (Integer Division)

    
    int quotient = 15 / 4; // quotient is 3
    

    This shows that dividing two integers results in an integer, with the remainder discarded.

  • Core Snippet 3 (Casting for Precision)

    
    double preciseQuotient = (double) 15 / 4; // preciseQuotient is 3.75
    

    This shows how to cast an integer to a double to force floating-point division.

  • Core Snippet 4 (Output with Concatenation)

    
    int items = 25;
    
    System.out.println("Total items: " + items);
    

    This prints a descriptive label along with the value of a variable to the console.

Core Skill Check

  • Code Tracing: What is the final value of x after this Java code runs: int x = 20 % 6 + 10 / 4 * 2;?

    Answer: 6

  • Debugging: Identify the logical error in this Java code intended to find the average of a and b: int a = 5; int b = 6; double avg = (a + b) / 2;.

    Answer: The division (a + b) / 2 is integer division, resulting in 5. The result should be 5.5. It should be written as double avg = (a + b) / 2.0; or double avg = (double)(a + b) / 2;.

  • Application: Write a single line of Java code that prints the remainder when an integer variable total is divided by 10.

    Answer: System.out.println(total % 10);

Common Misconceptions & Errors

  1. Integer Division by Default: Assuming 5 / 2 will produce 2.5. In Java, if both operands are integers, the result is always a truncated integer (2). You must use a double literal (e.g., 5 / 2.0) or casting to get a floating-point result.

  2. Incorrect Casting Order: Writing (double) (x / y) when x and y are integers. The integer division x / y happens first inside the parentheses, and only then is the truncated result cast to a double. The correct way is to cast one of the operands before the division: (double) x / y.

  3. Confusing print and println: Using System.out.print() in a loop and wondering why all the output appears on a single line. Remember that println adds a newline character after its output, while print does not.

  4. Order of Concatenation and Addition: In an expression like System.out.println("Sum: " + 5 + 10);, the result is "Sum: 510", not "Sum: 15". Because evaluation proceeds left-to-right, the "Sum: " string concatenates with 5 first, producing "Sum: 5". This new string then concatenates with 10. To perform the addition first, use parentheses: System.out.println("Sum: " + (5 + 10));.

Summary

Expressions are the foundation of computation in Java, allowing us to perform calculations using operators like +, -, *, /, and %. The outcome of an expression is heavily influenced by the data types of its operands and the rules of operator precedence. Integer division truncates decimals, a frequent source of bugs that can be resolved using casting or double literals. Finally, the System.out.println() and System.out.print() methods, combined with string concatenation, provide the essential tools for displaying the results of our expressions and communicating information to the user.