Introduction
In programming, we need ways to store and manipulate information. Variables are the foundation of any program—they hold the data your program works with. Topic 1.2 introduces the concept of variables and the three primitive data types used in AP Computer Science A: int, double, and boolean.
Understanding data types is essential because they determine what kind of values a variable can hold and what operations can be performed on those values. Choosing the correct data type ensures your program works efficiently and produces accurate results.
Data Types in Java
What is a Data Type?
A data type is a set of values and a corresponding set of operations on those values. Every variable in Java must have a declared type that determines what kind of data it can store.
Primitive vs. Reference Types
Data types in Java can be categorized as either primitive or reference types.
Primitive types store simple values directly. They are the basic building blocks of data in Java. AP Computer Science A focuses on three primitive types: int, double, and boolean.
Reference types are used to define objects that are not primitive types. Reference types include classes like String, ArrayList, and any custom classes you create. We'll explore reference types in later topics.
Key Difference:
- Primitive variables store the actual value
- Reference variables store a memory address (reference) to an object
The Three Primitive Data Types
int (Integer)
An int value is an integer—a whole number with no decimal point. Integers can be positive, negative, or zero.
Valid int values:
42
-17
0
1000000
-999Invalid int values:
3.14 // Has decimal point (use double)
"25" // String, not number
2,500 // Cannot use commasCommon uses for int:
- Counting items (number of students, array indices)
- Storing ages or years
- Representing discrete quantities
- Loop counters
Example declarations:
int numberOfStudents;
int temperature;
int score;double (Floating-Point Number)
A double value is a real number that can include a decimal point. The name "double" refers to "double precision floating-point," which means it can store very large or very small numbers with decimal precision.
Valid double values:
3.14159
-0.5
2.0
1000.25
0.0Note: Even 2.0 is a double because it has a decimal point, whereas 2 is an int.
Common uses for double:
- Measurements (height, weight, distance)
- Money calculations (price, tax)
- Scientific calculations
- Any value requiring decimal precision
Example declarations:
double gpa;
double price;
double pi;Important: Numbers without decimal points are treated as int, even if stored in a double variable. Numbers with decimal points are treated as double.
double x = 5; // 5 is int, converted to 5.0
double y = 5.0; // 5.0 is double
int z = 5.0; // ERROR! Cannot store double in intboolean (True or False)
A boolean value is either true or false. Boolean values are used for decision-making and controlling program flow.
Only two valid boolean values:
true
falseInvalid boolean values:
True // Capitalized (Java is case-sensitive)
FALSE // Capitalized
"true" // String, not boolean
1 // Number, not boolean
0 // Number, not booleanCommon uses for boolean:
- Storing yes/no answers
- Tracking on/off states
- Results of comparisons (is x greater than y?)
- Controlling loops and conditional statements
Example declarations:
boolean isRaining;
boolean gameOver;
boolean isPassing;Variables
What is a Variable?
A variable is a storage location that holds a value, which can change while the program is running. Every variable has:
- A name (identifier)
- A data type
- A value
Think of a variable as a labeled box that holds a specific type of item.
Declaring Variables
Before you can use a variable, you must declare it by specifying its type and name:
int age;
double price;
boolean isValid;Variable naming rules:
- Must start with a letter, underscore (_), or dollar sign ($)
- Can contain letters, digits, underscores, or dollar signs
- Cannot be a Java keyword (int, class, public, etc.)
- Cannot contain spaces
- Case-sensitive (age and Age are different variables)
Naming conventions (not required, but standard):
- Use camelCase for variable names (firstName, studentCount)
- First letter lowercase
- Descriptive names (score is better than s)
Valid variable names:
int numberOfDays;
double price2;
boolean _isActive;
int $amount;Invalid variable names:
int 2ndPlace; // Cannot start with digit
double my price; // Cannot contain spaces
boolean class; // Cannot use Java keyword
int first-name; // Cannot use hyphenStoring Values
Variables of primitive types hold primitive values from that type. Before using a variable in calculations or expressions, it must be assigned a value.
int count; // Declared but not initialized
count = 10; // Now initialized with value 10
double temperature;
temperature = 98.6;
boolean isOpen;
isOpen = true;You can also declare and initialize a variable in one statement:
int count = 10;
double temperature = 98.6;
boolean isOpen = true;Primitive vs. Reference Variable Storage
This distinction becomes important as you progress in the course:
Primitive variables store the actual value:
int x = 5; // x contains the value 5Reference variables store a memory address pointing to an object:
String name = "Alice"; // name contains a reference to "Alice"If you assign null to a reference variable, it indicates there is no object:
String text = null; // text refers to no objectData Type Comparison
Common Mistakes
Mistake 1: Using the wrong data type
// WRONG - age won't have decimals
double age = 16;
// CORRECT
int age = 16;
// WRONG - GPA needs decimals
int gpa = 3;
// CORRECT
double gpa = 3.75;Mistake 2: Not declaring a variable before using it
// WRONG
score = 100; // What type is score?
// CORRECT
int score;
score = 100;Mistake 3: Using invalid variable names
// WRONG
int 1stPlace; // Starts with digit
double my-score; // Contains hyphen
boolean return; // Java keyword
// CORRECT
int firstPlace;
double myScore;
boolean shouldReturn;Mistake 4: Confusing boolean values
// WRONG
boolean isValid = "true"; // String, not boolean
boolean flag = 1; // Number, not boolean
// CORRECT
boolean isValid = true;
boolean flag = false;