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
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
Important Rules:
- Variable names must start with a letter, underscore, or dollar sign
- Use camelCase:
studentAgenotstudent_age - Choose meaningful names:
temperaturenott
Common Mistakes to Avoid
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!