Unit 1.2

Variables and Data Types

AP Computer Science A

Understanding the Foundation of Java Programming

What We'll Learn Today:

  • Distinguish between primitive and reference data types
  • Work with int, double, and boolean data types
  • Declare and initialize variables properly
  • Apply appropriate data types to solve problems

What is a Data Type?

Essential Knowledge 1.2.A.1

A data type is a set of values and a corresponding set of operations on those values.

Think of a data type as a category that tells the computer:

  • What kind of data can be stored
  • How much memory to allocate
  • What operations are allowed on that data
// Examples of data and operations int age = 16; // Stores whole numbers age = age + 1; // Can perform arithmetic boolean isStudent = true; // Stores true/false isStudent = !isStudent; // Can perform logical operations

Two Categories of Data Types

Essential Knowledge 1.2.A.1

Data types can be categorized as either primitive or reference.

Primitive Types

Built into Java

Store simple values

Fixed memory size

Examples: int, double, boolean

Reference Types

User-defined or complex

Store object references

Variable memory size

Examples: String, Scanner, arrays

The Three Primitive Types in AP CS A

Essential Knowledge 1.2.B.1

The three primitive data types used in this course are int, double, and boolean.

int

Integer values

Whole numbers: ..., -2, -1, 0, 1, 2, ...

Range: -2,147,483,648 to 2,147,483,647

double

Real numbers

Decimal values: 3.14, -2.5, 0.0

Very large range with decimal precision

boolean

True or False

Only two possible values: true, false

Used for logical operations and conditions

What is a Variable?

Essential Knowledge 1.2.B.2

A variable is a storage location that holds a value, which can change while the program is running. Every variable has a name and an associated data type.

Think of a variable as a labeled box:

  • The label is the variable name
  • The box type is the data type
  • The contents is the value stored
  • You can change the contents but not the box type

Declaring Variables

// Syntax: dataType variableName; int studentCount; // Declares an int variable double gpa; // Declares a double variable boolean isEnrolled; // Declares a boolean variable // You can also initialize (assign a value) when declaring: int age = 16; // Declare and initialize double price = 19.99; // Declare and initialize boolean isReady = true; // Declare and initialize

Important Rules:

  • Variable names must start with a letter, underscore, or dollar sign
  • Use camelCase: studentAge not student_age
  • Choose meaningful names: temperature not t

Common Mistakes to Avoid

// WRONG - Mixing data types int count = 3.5; // Can't assign double to int // WRONG - Using before declaring score = 100; // Variable 'score' not declared int score; // WRONG - Invalid variable names int 2count; // Can't start with number int class; // 'class' is a reserved word // CORRECT int count = 3; // Correct type assignment int score = 100; // Declare before use int count2; // Valid name int studentClass; // Avoid reserved words

Summary

Key Takeaways:

  • Data types define what values can be stored and what operations are possible
  • Primitive types store simple values directly
  • int, double, boolean are the three primitive types in AP CS A
  • Variables are named storage locations with specific data types
  • Declaration creates a variable; initialization gives it a value

Next Steps:

Practice declaring variables and choosing appropriate data types for different scenarios. Remember: the right data type makes your program more efficient and prevents errors!

1 / ?