How computers remember, organize, and manipulate information in memory.
A single game screen hides numbers, decimals, and true/false flags.
Can you spot the difference in these gaming stats?
Can you be Level 15.5? Why must this be a whole number?
Wait, 1x damage is normal, but 2x is too much. How do we store that precise 1.5x boost?
Only "Yes" or "No". Do we need a number for this status?
The fundamental building blocks of memory
int
Stores whole numbers without decimals.
Examples: 1, -5, 100
double
Stores numbers with decimal values.
Examples: 3.14, 6.0, -0.5
boolean
Stores one of two logical values.
Values: true or false
FYI โ not tested on the AP CSA Exam
byte
Very small integer type.
Range: -128 to 127
short
Small integer type.
Range: -32,768 to 32,767
int
Standard integer type.
Main AP CSA type
long
Large integer type.
Example: 100L
float
Less precise decimal type.
Example: 3.14f
double
Standard decimal type.
Main AP CSA type
boolean
Stores true or false.
Main AP CSA type
char
Stores one character.
Example: 'A'
Start with the kind, then give each one a name.
First create the variable, then give it a value.
Declaration
Choose the variable's type and name.
Example: int score;
First Assignment
Store the variable's first value.
Example: score = 100;
Java can combine both steps in one statement.
One-Line Form
Write the type, name, and first value together.
The variable is declared and initialized at the same time.
What makes a valid variable name?
age โ Ageint, class, etc.)Use camelCase: studentAge, isActive
Use meaningful names: score > s
Constants use UPPER_CASE: MAX_SIZE
Classes use PascalCase: MyProgram
Which variable names compile in Java?
A name can be valid without following the recommended Java style.
After the cashier finds the total, the display keeps the answer where everyone can see it.
Master the direction of data flow
โฌ ๏ธ
price = 12 + 5;
Evaluate the right side, then store the result in the variable on the left.
Data flows from right to left.
= assigns a value; it is not a mathematical equal sign.
The right side is evaluated first.
The result is stored in the left-side variable.
Data flows from right to left.
Using variables for calculation
Variables Are Placeholders
When a variable appears in an expression, Java uses its current value.
What is printed, or what error occurs?
What is printed, or what error occurs?
What is printed, or what error occurs?
What is printed, or what error occurs?
What is printed, or what error occurs?
What is printed, or what error occurs?
What is printed, or what error occurs?
What is printed, or what error occurs?
What is printed, or what error occurs?
What are the final values of A, B, and C?
Things work better when they are prepared first and placed where they fit.
Essential variable safety
Every variable must be assigned a value before it enters an expression.
Values must come from compatible types. No square pegs in round holes!
The power of 'double' in expressions
Integer Only
An expression with int values only produces an int result.
Any decimal part is discarded.
Mixed Operations
If either value is a double, Java promotes the calculation to double.
The result keeps its decimal part.
Which types can store which results?
int โ double
A double variable can store an int value. Java promotes it automatically.
double โ int
An int variable cannot directly store a double value.
Compilation error: cannot convert from double to int
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
A familiar equation gives us the same two ideas.
๐
y = kx + b
As x changes, the value of y can change with it.
For one fixed line, k and b stay the same.
Variables: x and y
Their values can change.
Constants: k and b
Their values stay fixed for the equation.
Locking the drawer with 'final'
Once a final variable is initialized, its value can never change.
Use ALL_UPPERCASE names for constants.
Examples: MAX_VALUE, PI
Which line causes a compilation error?
Instead of fixing a value in the code, a program can wait for user input.
Getting values from the keyboard
Requires import java.util.Scanner;
Use nextInt() for integers and nextDouble() for decimals.
Complete it on time and submit it before the deadline.
Check what comes next and arrive ready.
Key takeaways from this lesson.
Variables
Named containers with Type, Name, and Value.
Assignment
Left โ Right
Calculate first, then store.
Constants
Use final to lock values forever.
Mixed Division
int / int truncates; double involvement keeps decimals.
Before we start, let's set some ground rules.
Bring Your USB
Save your work with a USB drive.
No Games
Gaming is not allowed during lab.
Stay on Task
Only visit websites related to the current task.
No Drinks
Keep drinks away from computers.