Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

AP Computer Science A: Deep Dive and FRQ Mastery — Alpha School

Lesson 1: Primitive Types, Variables and Expressions

Every Java program is built from values, and the simplest values belong to the primitive types: int for whole numbers, double for numbers with a decimal point, boolean for true or false, and char for a single character. These types are the vocabulary of the language, and choosing the right one is the first decision in any program. An int can hold a count of students or a year; a double can hold a price or a measurement; a boolean can hold whether a door is open; a char can hold a single letter. Each type has a fixed range and a fixed set of operations, and Java will not let you mix them carelessly.

A variable is a named box that holds a value of one type, and it must be declared before it is used, with its type and a name, for example int score = 0;. The declaration reserves the box, and the assignment puts a value into it. Java is statically typed, which means the type of every variable is fixed when the program is written, and the compiler checks that every value you assign matches the declared type. This strictness catches many errors before the program ever runs, which is one reason Java is used for teaching: the compiler is a patient tutor that points out mistakes early.

Expressions combine values and operators to produce new values, and Java follows the familiar order of operations: parentheses first, then multiplication, division and remainder, then addition and subtraction. Integer division is the first surprise for new programmers: 7 / 2 is 3, not 3.5, because dividing two ints gives an int, discarding the remainder. The remainder operator, %, gives what is left over: 7 % 2 is 1. To get a decimal result, at least one operand must be a double, so 7.0 / 2 is 3.5. Mixing int and double in an expression promotes the result to double, and assigning a double to an int variable is an error unless you cast it explicitly.

Variables can be updated with compound assignment operators such as +=, -=, *=, and /=, and with the increment and decrement operators ++ and --. Writing count++ adds one to count, and total += price adds price to total. These are conveniences, but they are also a source of exam questions, because the value of an expression involving ++ depends on whether it appears before or after the variable. In this course you will mostly use them as statements on their own, where their meaning is unambiguous, and you should be cautious about using them inside larger expressions.

Good names are part of good code, and Java has conventions that markers and other programmers expect you to follow. Variable and method names use camelCase, starting with a lowercase letter, as in totalScore or calculateAverage, while class names use UpperCamelCase, as in BankAccount. Names should say what the value is for: a variable called x tells the reader nothing, while a variable called numberOfStudents tells them everything. Comments are for explaining why code does something surprising, not for restating what it does — a comment that says 'add one to count' next to count++ adds nothing, while a comment explaining why a particular value was chosen can save the next reader a long investigation.

A worked example

Worked example: consider the program fragment below, which computes an average test score. The three scores are ints, so their sum is an int, and dividing by 3 would give integer division, losing the decimal part — so the code divides by 3.0 instead, which promotes the result to a double.

int score1 = 85;
int score2 = 90;
int score3 = 78;
double average = (score1 + score2 + score3) / 3.0;
System.out.println(average);  // prints 84.33333333333333

The parentheses force the addition to happen first, giving 253, and dividing 253 by 3.0 gives the double 84.33... If the code had divided by 3 instead, the result would have been the int 84, assigned to a double as 84.0 — a silent loss of precision that no compiler error would catch. In free-response questions, trace each expression step by step, writing down the type and the value at each stage, and watch especially for integer division and for the order in which assignments happen.

Key Terms

Primitive type
A basic data type built into Java: int, double, boolean, or char.
Variable
A named storage location holding a value of a fixed type.
Declaration
A statement that creates a variable and gives it a type and a name.
Integer division
Division of two ints, which discards the remainder and gives an int result.
Remainder operator (%)
An operator giving the remainder after integer division.
Casting
Explicitly converting a value from one type to another, for example (int) 3.7.

Discussion Questions

  1. Why does Java insist that every variable has a declared type?
  2. What is the difference between 7 / 2 and 7.0 / 2, and why does it matter?
  3. When would you choose a double instead of an int?

Practice Questions

  1. What is the value and type of 10 / 4 in Java?
  2. What is the value of 10 % 4?
  3. int x = 5; x += 3; What is x now?
  4. double d = 7 / 2; What value is stored in d, and why?
  5. Write a declaration for a boolean variable named passed, initially false.
Show answers
  1. 2, of type int, because integer division discards the remainder.
  2. 2, the remainder after dividing 10 by 4.
  3. 8.
  4. 3.0, because 7 / 2 is integer division giving 3, and the int 3 is then converted to 3.0 when assigned to the double. The decimal part is already lost before the assignment.
  5. boolean passed = false;
Keep reading to complete this lesson