📦 Variables and Data Types

AP Computer Science A • Unit 1 • Activity 1.1.2 • Mr. Babb

Learn

What is a variable?

A variable is a container that holds information while your program runs. Creating one is a simple algorithm — a set of steps to accomplish a task. In Java, three things define a variable: its data type, its name, and its value.

public class FirstVariable { public static void main(String[] args) { int age = 12; double allowance = 5.25; boolean isOldEnough = true; System.out.println(age); System.out.println(allowance); System.out.println(isOldEnough); } }

Line 5 declares a variable named age of type int (a storage location) and initializes it with the value 12 — all in one line.

(click Run to see the output)

🧠 Quick check

Which part of double allowance = 5.25; is the data type?

And remember the assignment operator = : the value of the expression on the right is stored in the variable on the left.

Sort it out

Data Type Sorter

0 / 12 sorted

Click a chip, then click the bucket where it belongs. Values go by what they are; variable names go by the type that best fits their meaning.

int

double

boolean

🔑 Remember

int = integers (whole numbers) • double = real numbers (decimals) • boolean = only true or false. College Board note: Java has 8 primitive types, but only these 3 are used on the AP CS A exam.

You be the judge

Variable Name Court

0 / 8 judged

Java has strict rules: a name cannot begin with a number, cannot contain special characters, and cannot be one of Java's 57 keywords. Multi-word names should use camelCase. Rule on each name:

Expect an error?

Compile or Crash

Round 1 of 5

Each snippet either runs fine or makes the compiler complain. You decide! (Each variable can only store its own type of data.)

It's your turn

The Allowance Mission

Gina's household has rules for who receives an allowance. Gina has two siblings, Carl and Jennifer:

  • Allowance is always $8.75 — it never changes.
  • Children under 8 do not receive an allowance.
  • Gina is 16 years old.
  • Carl is 3 years younger than Gina.
  • Jennifer was born 10 years after Carl.

Fill in the program so it stores everyone's information with the right types and values:

final double ALLOWANCE = ___; int age = ___; // Gina boolean getsAllowance = ___; age = ___; // Carl (reuse the variable!) getsAllowance = ___; age = ___; // Jennifer getsAllowance = ___;

🤔 Why final?

The allowance is set by the parents and never changes in the program — so we protect it: final double ALLOWANCE = 8.75;. Once initialized, a final variable cannot be modified (ALL CAPS is the naming convention). Other famous finals: PI = 3.1415926, GRAVITY = 9.81, SPEED_OF_LIGHT = 299792458.

Exit ticket

Show what you know

Answer all 8 questions, then click Grade my exit ticket. One retry allowed.

📝 Computer Science Notebook

Add these to your notebook before you leave:

  • A variable is a storage location that holds a value, which can change while the program runs. Every variable has a name and a data type.
  • The assignment operator (=) stores the value of the expression on the right into the variable on the left.
  • Primitive types in this course: int (integers), double (real numbers), boolean (true/false).
  • Every variable must be assigned a value from a compatible type before it is used; the first assignment is initialization.
  • A final variable cannot be modified — name it in ALL CAPS.
  • Start a "Java keywords" page and add keywords as you meet them: int, double, boolean, final, public, class, static, void