Variables & Basic Data Types

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

๐ŸŽฎ Gaming Data Everywhere

A single game screen hides numbers, decimals, and true/false flags.

๐ŸŽฎ 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 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

โž• More Data Types

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'

๐Ÿท๏ธ Type First, Name Second

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

๐Ÿงฌ Anatomy of a Variable

First create the variable, then give it a value.

VariableDemo.java

Declaration

Choose the variable's type and name.

Example: int score;

First Assignment

Store the variable's first value.

Example: score = 100;

โšก Declare and Initialize Together

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.

VariableDemo.java

๐Ÿท๏ธ Naming Rules

What makes a valid variable name?

Rules

  • Use only: atoz,AtoZ,0to9,_
  • Cannot start with a digit
  • Case-sensitive: age โ‰  Age
  • 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 variable names compile in Java?

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

A name can be valid without following the recommended Java style.

๐Ÿงพ 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;

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.

Evaluation

The right side is evaluated first.

Storage

The result is stored in the left-side variable.

Direction

Data flows from right to left.

๐Ÿงฎ Variable Expressions

Using variables for calculation

Variables Are Placeholders

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

Calculator.java

๐Ÿ” Expression Quiz (1/10)

What is printed, or what error occurs?

ExpressionQuiz.java

๐Ÿ” Expression Quiz (2/10)

What is printed, or what error occurs?

ExpressionQuiz.java

๐Ÿ” Expression Quiz (3/10)

What is printed, or what error occurs?

ExpressionQuiz.java

๐Ÿ” Expression Quiz (4/10)

What is printed, or what error occurs?

ExpressionQuiz.java

๐Ÿ” Expression Quiz (5/10)

What is printed, or what error occurs?

ExpressionQuiz.java

๐Ÿ” Expression Quiz (6/10)

What is printed, or what error occurs?

ExpressionQuiz.java

๐Ÿ” Expression Quiz (7/10)

What is printed, or what error occurs?

ExpressionQuiz.java

๐Ÿ” Expression Quiz (8/10)

What is printed, or what error occurs?

ExpressionQuiz.java

๐Ÿ” Expression Quiz (9/10)

What is printed, or what error occurs?

ExpressionQuiz.java

๐Ÿ” Expression Quiz (10/10)

What are the final values of A, B, and C?

ExpressionQuiz.java

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

An expression with int values only produces an int result.

Any decimal part is discarded.

Program.java
Console Output
2

Mixed Operations

If either value is a double, Java promotes the calculation to double.

The result keeps its decimal part.

Program.java
Console Output
2.5

โ†”๏ธ Assignment Compatibility

Which types can store which results?

int โ†’ double

A double variable can store an int value. Java promotes it automatically.

Program.java
Console Output
3.0

double โ†’ int

An int variable cannot directly store a double value.

Compilation error: cannot convert from double to int

Program.java

๐Ÿ” Arithmetic Quiz (1/10)

What value is printed?

ArithmeticQuiz.java

๐Ÿ” Arithmetic Quiz (2/10)

What value is printed?

ArithmeticQuiz.java

๐Ÿ” Arithmetic Quiz (3/10)

What value is printed?

ArithmeticQuiz.java

๐Ÿ” Arithmetic Quiz (4/10)

What value is printed?

ArithmeticQuiz.java

๐Ÿ” Arithmetic Quiz (5/10)

What value is printed?

ArithmeticQuiz.java

๐Ÿ” Arithmetic Quiz (6/10)

What value is printed?

ArithmeticQuiz.java

๐Ÿ” Arithmetic Quiz (7/10)

What value is printed?

ArithmeticQuiz.java

๐Ÿ” Arithmetic Quiz (8/10)

What value is printed?

ArithmeticQuiz.java

๐Ÿ” Arithmetic Quiz (9/10)

What value is printed?

ArithmeticQuiz.java

๐Ÿ” Arithmetic Quiz (10/10)

What value is printed?

ArithmeticQuiz.java

๐Ÿ“ A Math Connection

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.

๐Ÿ”’ Constants

Locking the drawer with 'final'

Constants.java

Immutability

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

Naming Convention

Use ALL_UPPERCASE names for constants.

Examples: MAX_VALUE, PI

๐Ÿ” Constant Trap Quiz

Which line causes a compilation error?

Constants.java

โŒจ๏ธ Values Can Come From You

Instead of fixing a value in the code, a program can wait for user input.

โŒจ๏ธ The Scanner

Getting values from the keyboard

InputDemo.java

Import

Requires import java.util.Scanner;

Read Methods

Use nextInt() for integers and nextDouble() for decimals.

๐Ÿ“ Homework

Complete it on time and submit it before the deadline.

๐Ÿงญ Next Class

Check what comes next and arrive ready.

๐Ÿ”– Summary

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.

๐Ÿงช 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

Only visit websites related to the current task.

No Drinks

Keep drinks away from computers.

01 / 46