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

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

Assignment Logic

Master the direction of data flow

score = 100 + 5;
Right to Left

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

Test your placeholder skills

What is the final value of 'result'?

Program.java
java

Independence Quiz

What happens to the copy?

What is the value of y after all lines execute?

Program.java
java

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.

Arithmetic Quiz

Integer vs. Double Division

What is the result of the following arithmetic?

Program.java
java

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

Final is final

Which line will cause a Compilation Error?

Program.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.

After-Class
Assignment

Sequential Logic

int x = 10;
int y = 3;
x = x / y;
y = x * 5;
x = y + 2;

Scanner Challenge

  • 1. Create Scanner object
  • 2. Print a prompt message
  • 3. Store input in a double