콘텐츠로 이동

Unit 1.2: Variables and Data Types

Scope: CS Awesome 2, Section 1.2

Learning Goals

By the end of this lesson, you should be able to:

  1. Explain what a variable stores.
  2. Distinguish primitive and reference types.
  3. Use the AP course types int, double, boolean, and String.
  4. Declare and initialize variables.
  5. Choose valid, descriptive variable names.

What Is a Variable?

A variable is a named memory location that stores a value. Its value may change while a program runs. Every variable has:

  • a name;
  • a data type;
  • a current value after initialization.
int seats = 24;

Here, seats is the name, int is the type, and 24 is the initial value.

Data Types

A data type defines a set of possible values and the operations that make sense for those values.

Primitive Types

A primitive variable stores its value directly. The three primitive types emphasized in this course are:

Type Represents Example literals
int whole numbers 0, 42, -7
double numbers with a fractional part 3.5, -0.25, 8.0
boolean logical truth values true, false
int floor = 12;
double temperature = 21.5;
boolean doorOpen = false;

The literals true and false are lowercase Java keywords and are not placed in quotes.

Reference Types

A reference variable refers to an object rather than holding a primitive value directly. String is a reference type used for sequences of characters.

String destination = "Busan";

String begins with an uppercase S. The value is a string literal enclosed in double quotes.

Declaring and Initializing

A declaration introduces a variable and its type.

int score;

Initialization gives a variable its first value.

score = 10;

Declaration and initialization can be combined:

int score = 10;
double average = 8.75;
boolean complete = true;
String label = "Section A";

A local variable must have a value before an expression tries to use it.

Matching Values to Types

int count = 6;             // valid
double distance = 6.0;     // valid
boolean active = false;    // valid
String code = "A6";       // valid

These declarations are invalid:

int count = 6.5;           // double value cannot be stored directly in int
boolean active = "false"; // String is not boolean
String code = 6;           // int is not String

The value's type must be compatible with the variable's declared type.

Naming Variables

A Java identifier:

  • may contain letters, digits, _, and $;
  • cannot begin with a digit;
  • cannot contain spaces;
  • cannot be a Java keyword;
  • is case-sensitive.

Use camel case for variable names and choose names that explain the data.

Good name Weak or invalid name Problem
ticketCount x Too vague for this purpose.
room2Capacity 2roomCapacity Cannot start with a digit.
isAvailable is available Spaces are not allowed.
className class class is a keyword.

Printing Variable Values

public class TripInfo
{
    public static void main(String[] args)
    {
        String city = "Jeju";
        int nights = 3;
        double budget = 450.0;
        boolean booked = true;

        System.out.println(city);
        System.out.println(nights);
        System.out.println(budget);
        System.out.println(booked);
    }
}

The quotes are used when creating the string literal, not when printing the variable.

Practice Missions

Mission 1: Type Decisions

Choose the most appropriate type for each value and explain why:

  1. the number of books on a shelf;
  2. the average mass of a package;
  3. whether a security alarm is armed;
  4. a train identifier such as KTX-217.

Mission 2: Observatory Record

Create variables for an observatory's name, number of visitors, cloud coverage percentage, and whether the telescope is operating. Initialize each variable and print each value on its own line.

Mission 3: Declaration Repair

Correct every declaration without changing the intended meaning.

int 3rdFloor = 3;
double humidity = "64.5";
boolean sensorReady = "true";
string stationName = "North Ridge";

Mission 4: Explain the Difference

Explain the difference among these four items: variable name, data type, literal, and stored value. Use double speed = 4.5; as your example.

Key Summary

Concept Core idea
Variable A named storage location with a type.
Primitive type Stores a primitive value directly.
Reference type Stores a reference to an object.
int Whole-number primitive type.
double Decimal-number primitive type.
boolean true or false.
String Reference type for text.
Initialization The first value assigned to a variable.

Source scope: CS Awesome 2, Unit 1.2