Expression in AP Computer Science A

In AP Computer Science A, an expression is any combination of values, variables, operators, and method calls that Java evaluates to produce a single value of a specific type, like (double)a / b or temp + 0.5, which can then be stored in a variable with an assignment statement.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is expression?

An expression is anything Java can evaluate down to one value. That includes a single literal like 5, a variable like temp, or a longer combination like (double)a / b + (int)(c * 2). No matter how complicated it looks, every expression boils down to exactly one value with exactly one type (an int, a double, a boolean, a String, and so on).

In Topic 1.4, expressions show up on the right side of the assignment operator =. Java evaluates the expression on the right first, then stores the result in the variable on the left (EK 1.4.A.2). The type of that result has to be compatible with the variable's type (EK 1.4.A.1), which is exactly why casting and int-vs-double division matter so much. Think of an expression as a tiny machine. You feed it values and operators, it follows precedence rules, and it spits out one answer.

Why expression matters in AP® Computer Science A

Expressions live in Unit 1: Using Objects and Methods, specifically Topic 1.4: Assignment Statements and Input. Learning objective AP Comp Sci A 1.4.A asks you to develop code for assignment statements with expressions and determine the value stored in the variable as a result. That second part is the big one. A huge slice of AP CSA multiple choice is literally just "what is the value of x after this code runs?" and answering it means evaluating expressions correctly, including integer division truncation, the modulus operator, and casts. Expressions are also the foundation for everything later. Boolean expressions drive conditionals and loops, and method calls inside expressions return values you use everywhere. If you can't evaluate an expression by hand, almost no other AP CSA skill works.

How expression connects across the course

Assignment Statements (Unit 1)

Expressions and assignment statements are a package deal. The expression on the right of = gets evaluated first, and its single result gets stored in the variable on the left. An expression produces a value; an assignment statement is what you do with it.

Casting and Integer Division (Unit 1)

The type of an expression's result depends on the types inside it. Two ints divided give a truncated int, but cast one to double and you get a decimal answer. Casts like (double)a / b and the rounding trick (int)(temp + 0.5) are the most-tested expression details in Unit 1.

Boolean Expressions in Conditionals (Unit 2)

Once expressions can produce boolean values using comparison operators, they become the conditions inside if statements and loops. Same evaluation skill, new payoff. Every branch your program takes is decided by an expression evaluating to true or false.

Method Calls That Return Values (Unit 1)

A method call like scan.nextDouble() or Math.sqrt(x) is itself an expression because it evaluates to a single value. That's why you can drop it straight into a bigger expression or onto the right side of an assignment.

Is expression on the AP® Computer Science A exam?

Expressions are tested constantly but almost never by name. The classic MCQ stem hands you a code segment and asks "what is the value of num after executing this code?" For example, given double temp = 7.8; and int num = (int)(temp + 0.5);, you have to evaluate the expression step by step (7.8 + 0.5 = 8.3, then the cast truncates to 8). Harder versions stack casting, integer division, and modulus into one expression, like (double)a / b + (int)(c * 2) - (int)(a % b + 0.7), and the wrong answer choices are exactly what you get if you truncate at the wrong moment or forget operator precedence. On FRQs, you won't be asked to define an expression, but every line of code you write uses them. The 2019 FRQs on string processing and the LightBoard class both required writing expressions to compute indexes, counts, and boolean conditions. Practice evaluating expressions on paper, since you won't have a compiler to catch your mistakes.

Expression vs Statement

An expression evaluates to a value; a statement is a complete instruction that does something. temp + 0.5 is an expression because it produces one value (8.3). int num = (int)(temp + 0.5); is a statement because it performs an action, storing a value in num. Easy check: expressions could appear on the right side of =, while statements end with a semicolon and stand alone.

Key things to remember about expression

  • An expression is any combination of values, variables, operators, and method calls that evaluates to exactly one value of one type.

  • In an assignment statement, Java evaluates the expression on the right side of = first, then stores that result in the variable on the left (EK 1.4.A.2).

  • Every variable must be assigned a compatible value before it can be used in an expression, and the first assignment is called initialization (EK 1.4.A.1).

  • The types inside an expression control the result, so int / int truncates to an int while a cast like (double)a / b keeps the decimal.

  • The pattern (int)(x + 0.5) rounds a positive double to the nearest int, and it shows up repeatedly in AP CSA multiple choice.

  • When evaluating expressions on the exam, work inside-out and apply casts at the exact moment they appear, because most wrong answer choices come from truncating too early or too late.

Frequently asked questions about expression

What is an expression in AP Computer Science A?

An expression is a combination of values, variables, operators, and method calls that Java evaluates to produce a single value of a specific type. Even something as simple as the literal 5 or the variable temp counts as an expression.

What's the difference between an expression and a statement in Java?

An expression produces a value, like temp + 0.5, while a statement is a complete instruction that performs an action, like int num = (int)(temp + 0.5);. Assignment statements contain expressions on the right side of the = operator.

Does an expression always have to be on the right side of an assignment?

No. Expressions also appear as arguments inside method calls, as conditions in if statements and loops, and inside return statements. Anywhere Java needs a value, an expression supplies it.

Why does (int)(7.8 + 0.5) equal 8 and not 7?

Java evaluates the expression inside the parentheses first, so 7.8 + 0.5 gives 8.3, and then the (int) cast truncates the decimal to get 8. This rounding trick, (int)(x + 0.5), is a favorite on AP CSA multiple choice.

Is a method call like scan.nextDouble() an expression?

Yes, if the method returns a value. A call like scan.nextDouble() evaluates to a single double, so you can assign it to a variable or use it inside a larger expression, exactly like any other expression.