Variables &
Basic Data Types

How computers remember, organize, and manipulate information in memory.

Data Discovery

Can you spot the difference in these gaming stats?

Level
15

Can you be Level 15.5? Why must this be a whole number?

Crit Multi
1.5x

Wait, 1x damage is normal, but 2x is too much. How do we store that precise 1.5x boost?

Is Alive
true

Only "Yes" or "No". Do we need a number for this status?

Primitive Data Types

The fundamental building blocks of memory

int

Stores whole numbers. No decimals!
Example: 1, -5, 100

double

Stores large decimal numbers.
Example: 3.14, 6.0, -0.5

boolean

Stores logic values (true/false).
Example: true, false

More Data Types

FYI — not tested on the AP CSA Exam

byte

Very small integer.
-128 to 127

short

Small integer.
-32,768 to 32,767

int

Standard integer.
Main AP CSA type

long

Large integer.
Suffix L: 100L

float

Less precise decimal.
Suffix f: 3.14f

double

Standard decimal.
Main AP CSA type

boolean

True / false.
Main AP CSA type

char

Single character.
Single quotes: 'A'

Type First, Name Second

Start with the kind, then give each one a name.

Anatomy of a Variable

Variable = A Named Storage Location

Declaration

int x;
Setting the Size (Type) & Label (Name).

Naming Tip: Use camelCase (e.g., studentAge)

Initialization

x = 10;
Placing the very first Content (Value) inside.

VariableDemo.java
java

Naming Rules

What makes a valid variable name?

Rules

  • Use only: atoz,AtoZ,0to9,_
  • Cannot start with a digit
  • Case-sensitive: ageAge
  • Cannot use reserved keywords (int, class, etc.)

Conventions

  • Use camelCase: studentAge, isActive
  • Use meaningful names — score > s
  • Constants use UPPER_CASE: MAX_SIZE
  • Classes use PascalCase: MyProgram

Naming Quiz

Which of these are valid?

myVar
2ndPlace
class
_temp
firstName
my-var
3x
MAX_SIZE
my var
count2
milesPerHour
double
int
student_name
1stPlace
result

The Number Lands on Screen

After the cashier finds the total, the display keeps the answer where everyone can see it.

Assignment Logic

Master the direction of data flow

price = 12 + 5;
Right to Left
= is not the same as a math equal sign.

Evaluation

The right side is evaluated first.

Storage

The result is stored into the left side variable.

Direction

Data always flows from Right ➔ Left.

Variable Expressions

Using variables for calculation

Variables are Placeholders

When a variable appears in an expression, Java uses its current value.

Calculator.java
java

Expression Quiz

Variable substitution in expressions

Quiz.java
java
Question 1 of 10

Ready Before Use

Things work better when they are prepared first and placed where they fit.

The Golden Rules

Essential variable safety

Rule #1: Assigned Before Use

Every variable must be assigned a value before it enters an expression.

int x;

// ERROR: x not initialized!
int y = x + 5;

Rule #2: Type Safety

Values must come from compatible types. No square pegs in round holes!

// ERROR: Type mismatch!
int count = 3.99;

Automatic Promotion

The power of 'double' in expressions

Integer Only

int only ➔ int

Program.java
java
Console Output
2

Integer math is strict. Any decimals are deleted!

Mixed Operations

double involvement ➔ double

Program.java
java
Console Output
2.5

If there is at least one double, the result is double.

Assignment Compatibility

Which types can store which results?

int → double

Assigning an int result to a double variable works fine — the value is promoted.

Program.java
java
Console Output
3.0

double → int

Assigning a double result to an int variable causes a compilation error.

Program.java
java

"Type mismatch: cannot convert from double to int"

Arithmetic Quiz

Integer division with mixed types

Quiz.java
java
Question 1 of 10

Constants

Locking the drawer with 'final'

Constants.java
java

Immutability

Once a final variable is initialized, its value can never be changed.

Naming Convention

Use ALL_UPPERCASE letters for constants.
(e.g., MAX_VALUE, PI)

Constant Trap Quiz

Understanding the final keyword

Constants.java
java

The Scanner

Getting values from the keyboard

InputDemo.java
java

Import

Requires:
import java.util.Scanner;

Read Methods

nextInt() for integers,
nextDouble() for decimals.

Course Summary

Key Takeaways for Variables & Types

Variables

Named containers with Type, Name, and Value.

Assignment

Flows from Right ➔ Left (Calculate first, then store).

Constants

Use final to lock values forever.

Mixed Division

int / int truncates; double involvement keeps decimals.

Lab Rules

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

No browsing unrelated websites.

No Drinks

Keep drinks away from computers.
01 / 22