Variables and Primitive Data Types

Sophie Anderson
5 min read
Listen to this study note
Study Guide Overview
This AP Computer Science A study guide covers variables and data types, focusing on primitive types like int, double, and boolean. It explains variable declaration, scope, naming conventions (camel casing), and initialization. It also touches upon reserved words and strongly typed nature of Java. The guide includes practice questions on identifying data types.
#AP Computer Science A: Night Before Cram Session 🚀
Hey there, future coder! Feeling the pressure? Don't worry, we've got this. This guide is designed to be your ultimate last-minute review, focusing on the most crucial concepts and exam strategies. Let's make sure you're confident and ready to ace that AP exam!
#1. Variables and Data Types
This is foundational stuff, and it's everywhere on the exam. Get this down, and you're off to a great start!
#1.1 Primitive Data Types
These are your building blocks. Java has a few, but for the AP exam, you really need to know these three like the back of your hand:
- int: Stores whole numbers. Think of it as counting things.
- Range: -2,147,483,648 to 2,147,483,647
Integer.MAX_VALUE
,Integer.MIN_VALUE
are your boundaries.
Integer Overflow: Going beyond these limits wraps around to the other end.
Other primitive types (byte, short, long, float, char) exist but are not heavily tested on the AP exam.
I.D.B - Int, Double, Boolean - The three amigos of AP CSA primitive types.
#1.1.1 Identifying Data Types Practice
Let's see if you've got this!
Question 1: In which data type would you store a student's GPA?
A. int B. double C. boolean D. String
Answer: B. double
Question 2: If you wanted to store a person's name, which data type would you use?
A. int B. double C. boolean D. String
Answer: D. String
Question 3: What data type would you use to store a person's age?
A. int B. double C. boolean D. String
Answer: A. int
Question 4: If you wanted to store a student's enrollment status (enrolled or not enrolled), which data type would you use?
A. int B. double C. boolean D. String
Answer: C. boolean
Question 5: If you wanted to store a person's phone number, which data type would you use?
A. int B. double C. boolean D. String
Answer: D. String
Phone numbers are stored as Strings because they are not numerical values used in calculations.
#1.2 Variables
Variables are like containers for your data. You give them a name, a type, and a value.
- Declaration:
(scope) dataType variableName = dataValue;
- Scope (access modifiers):
public
: Accessible everywhere.private
: Accessible only within the class.protected
: Accessible within the package and subclasses.- Default (no modifier): Accessible within the package.
- Camel Casing:
myVariableName
,studentGPA
- Reserved Words: Don't use Java keywords like
int
,class
,public
as variable names. - Strongly Typed: Java is strongly typed, meaning once a variable has a data type, it can't change.
- Scope (access modifiers):
- Legal vs. Illegal Names:
- Good:
iceCreamFlavor
,carMake
,name
- Legal (but not preferred):
$ball
,j
,var4
- Illegal:
nf 23f
,&jjjjd
,2fdjjkdk
- Good:
- Initialization: Giving a variable its first value.
java int daysInWeek = 7; private String name = "Bob"; ```
Explore more resources

How are we doing?
Give us your feedback and let us know how we can improve