Welcome to the World of Variables!
In computer science, we need a way to store information so we can use it later. Whether it is your high score in a game, the price of a taco, or whether a light switch is on or off, we use variables. Think of a variable as a labeled storage box: you give the box a name, and you put a specific piece of information inside it.
In this chapter, we will learn how to create these boxes and the three specific types of data that the AP Computer Science A exam expects you to know. Don't worry if you've never coded before—we will take this step by step!
1. The Three Essential Data Types
Java is a "strongly typed" language. This means you must tell the computer exactly what kind of data you want to store in your variable. For the AP exam, you only need to master three primitive data types:
A. \(int\) (Integers)
The \(int\) type is used for whole numbers. This includes positive numbers, negative numbers, and zero. There are no decimal points allowed here!
Real-world examples: Your age, the number of students in a class, or the year.
Quick Note: While you don't need to memorize the exact numbers, remember that \(int\) variables have a limit. They range from \(Integer.MIN\_VALUE\) to \(Integer.MAX\_VALUE\). You will learn more about these specific limits in Chapter 1.5.
B. \(double\) (Decimal Numbers)
The \(double\) type is used for numbers that require decimal points. The name "double" comes from "double-precision," meaning it can hold very precise values.
Real-world examples: The price of a soda (\(\$1.50\)), your GPA (\(3.85\)), or the value of \(\pi\) (\(3.14159\)).
C. \(boolean\) (True or False)
A \(boolean\) is the simplest data type. It can only hold one of two values: \(true\) or \(false\). Computers love booleans because they represent the "on/off" logic of computer chips.
Real-world examples: Is the game over? (true/false), Is the user logged in? (true/false).
Key Takeaway: If it's a whole number, use \(int\). If it has a decimal, use \(double\). If it's a yes/no choice, use \(boolean\). You do not need to learn other types like float, char, or long for this exam!
2. Creating Variables: Declaration and Initialization
Creating a variable is usually a two-step process, though we often do both on one line.
Step 1: Declaration
Declaration is like telling the computer, "Hey, I'm going to need a box for an integer, and I'm going to call it \(score\)." You state the type and the name.
\(int\) \(score;\)
Step 2: Initialization
Initialization is when you put a value into that box for the first time using the assignment operator (\(=\)).
\(score = 0;\)
Doing it all at once
Most of the time, we declare and initialize on the same line to keep our code clean:
\(int\) \(score = 0;\)
\(double\) \(price = 19.99;\)
\(boolean\) \(isGameOver = false;\)
Did you know? The semicolon (\(;\)) is like a period at the end of a sentence in Java. It tells the computer that your instruction is finished.
3. Naming Your Variables
In Java, we can't just name variables anything we want. There are rules we must follow and conventions that good programmers follow.
The Mandatory Rules:
1. Names can contain letters, numbers, and underscores.
2. Names cannot start with a number.
3. Names are case-sensitive (\(score\) and \(Score\) are two different boxes!).
4. You cannot use Java "keywords" (you can't name a variable \(int\) or \(boolean\)).
The Java Style Convention: Lower Camel Case
To make code easier to read, Java programmers use lower camel case. You start with a lowercase letter, and every new word after that starts with a capital letter.
Examples:
\(playerScore\)
\(daysUntilGraduation\)
\(isLightOn\)
Key Takeaway: Always pick names that describe what is inside the box. Use \(currentHealth\) instead of just \(h\). It makes your life much easier when you read your code later!
4. Common Mistakes to Avoid
Don't worry if you run into these; even professional developers make these mistakes!
- Type Mismatch: Trying to put a decimal into an \(int\) box. For example, \(int\) \(x = 5.5;\) will cause an error because an \(int\) cannot hold decimals.
- Forgetful Initialization: Trying to use a variable before you have given it a value. You can't print a score if the computer doesn't know what the score is yet!
- Starting with a Capital: In Java, variable names usually start with a lowercase letter. Class names (which we will learn about later) start with uppercase letters. Keeping them distinct helps you stay organized.
Quick Review Box
1. Primitives: Only \(int\), \(double\), and \(boolean\) are on the exam.
2. Declaration: Specifying the type and name (\(int\) \(myAge;\)).
3. Initialization: Giving the variable its first value (\(myAge = 16;\)).
4. Assignment: The \(=\) sign moves the value on the right into the variable on the left.
5. Naming: Use \(lowerCamelCase\) for variable names to be a "pro" Java coder.
Next Topic Preview: Now that you have your boxes and data, how do you do math with them? In Chapter 1.3, we'll look at Expressions and Output!