zuai-logo
zuai-logo
  1. AP Computer Science A
FlashcardFlashcardStudy GuideStudy Guide
Question BankQuestion BankGlossaryGlossary

Variables and Primitive Data Types

Sophie Anderson

Sophie Anderson

5 min read

Next Topic - Expressions and Assignment Statements

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.
Common Mistake

Integer Overflow: Going beyond these limits wraps around to the other end.

* Examples: `1`, `3`, `5`, `-1`, `0` * **double**: Stores decimal numbers with high precision (about 15 decimal places). * Examples: `0.0`, `-13.55342323`, `363.34334` * **boolean**: Stores a truth value. It's either `true` or `false`. * Examples: `true`, `false`
Quick Fact

Other primitive types (byte, short, long, float, char) exist but are not heavily tested on the AP exam.

Memory Aid

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

Key Concept

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. Reserved Words
    • Strongly Typed: Java is strongly typed, meaning once a variable has a data type, it can't change.
  • Legal vs. Illegal Names:
    • Good: iceCreamFlavor, carMake, name
    • Legal (but not preferred): $ball, j, var4
    • Illegal: nf 23f, &jjjjd, 2fdjjkdk
  • Initialization: Giving a variable its first value.

java int daysInWeek = 7; private String name = "Bob"; ```

Explore more resources

FlashcardFlashcard

Flashcard

Continute to Flashcard

Question BankQuestion Bank

Question Bank

Continute to Question Bank

Mock ExamMock Exam

Mock Exam

Continute to Mock Exam

Feedback stars icon

How are we doing?

Give us your feedback and let us know how we can improve

Previous Topic - Why Programming? Why Java?Next Topic - Expressions and Assignment Statements

Question 1 of 10

Which data type would you use to store the number of students in a class? 🤔

double

boolean

int

String