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.
Line 5 declares a variable named age of type int (a storage location) and initializes it with the value 12 — all in one line.
🧠 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.
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.
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:
Each snippet either runs fine or makes the compiler complain. You decide! (Each variable can only store its own type of data.)
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:
🤔 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.
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
finalvariable 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…