Welcome to the "Memory" of Your Program!

In the previous chapters, we learned how to create variables and perform math. Now, it is time to put those variables to work! Assignment statements are how we give variables their values and update them as our program runs. We will also explore the concept of Input—how our programs can receive information to process. Think of this chapter as learning how to give your program a "memory" and "ears" to hear data.

The Assignment Operator \( ( = ) \)

In computer science, the \( = \) symbol does not mean "is equal to" like it does in math class. Instead, it is the assignment operator. It acts like an arrow, taking the value from the right side and "pushing" it into the variable on the left side.

How to Read an Assignment Statement

When you see a line of code like \( x = 10; \), don't say "x equals 10." Instead, say "x gets the value 10" or "assign 10 to x."

The structure always follows this rule:
Variable = Expression;

1. The computer evaluates everything on the right side first.
2. Once it has a single result, it stores that result in the variable on the left side.

Example:
\( score = 10 + 5; \)
First, Java calculates \( 10 + 5 \). Then, it stores the result \( 15 \) in the variable \( score \).

Important Rules for Assignment

  • The Left Side Must Be a Variable: You can't write \( 10 = x; \). The "container" must always be on the left.
  • Variables Hold One Value: If a variable already had a value, the new assignment overwrites it. The old value is gone forever!

Key Takeaway: Assignment is a right-to-left process. Evaluate the right, then store it in the left.

Updating Variables (Tracing)

Because variables can change, we often use the current value of a variable to calculate its new value. This is very common in games for keeping score or moving characters.

Example:
\( score = 0; \)
\( score = score + 10; \)

Don't worry if \( score = score + 10 \) looks weird! Let’s break it down step-by-step:
1. Java looks at the right side: \( score + 10 \).
2. It looks up the current value of \( score \), which is \( 0 \).
3. It adds: \( 0 + 10 = 10 \).
4. It takes that new \( 10 \) and stores it back into the \( score \) variable.

Quick Review: This process of tracking how variables change line-by-line is called tracing. It is a vital skill for the AP Exam!

Input: Getting Data into the Program

For a program to be useful, it often needs data to work with. In AP Computer Science A, we focus on how to read data using the Scanner class.

Understanding Method Signatures

To use tools like the Scanner, you must be able to read documentation. A method signature tells you three things:
1. The Name: What is the method called?
2. The Parameter List: What information does the method need from you?
3. The Return Type: What kind of data will the method give back to you?

The Scanner Class

While you might see different types of input in other settings, the AP curriculum focuses on using the \( Scanner \) to read data from sources like Files. To use it, we first create a "Scanner object" (which we will learn more about in the "Objects" chapters).

Here are the common methods you will use to "input" data from a Scanner:

  • \( nextInt() \): Reads the next integer and returns an \( int \).
  • \( nextDouble() \): Reads the next decimal number and returns a \( double \).
  • \( nextBoolean() \): Reads the next \( true \) or \( false \) and returns a \( boolean \).
  • \( nextLine() \): Reads an entire line of text as a \( String \).
  • \( next() \): Reads a single "token" (usually one word) as a \( String \).

Note: For the AP Exam, you are not required to write code that accepts input from the keyboard (like typing into a console). Instead, you focus on how these methods work when reading from a file or as part of a provided code segment.

Example Usage:
\( int \text{ age } = \text{ myScanner.nextInt(); } \)
This takes the next number found by the Scanner and assigns it to the variable \( age \).

Common Mistakes to Avoid

1. Using the wrong type: If you try to use \( nextInt() \) but the data is a decimal (like \( 3.14 \)), the program will crash! Always match the method to the variable type.

2. Confusing \( = \) with \( == \): Remember, \( = \) is for assigning a value. In later chapters, we will use \( == \) to compare two values. For now, just remember: "Single equals is for storage!"

3. Forgetting the Right-to-Left Rule: Always solve the math on the right side of the \( = \) before you try to change the variable on the left.

Summary and Key Takeaways

  • The assignment operator \( ( = ) \) stores the value on the right into the variable on the left.
  • The assignment statement always evaluates the right side completely before updating the variable.
  • Tracing is the practice of following a variable's value as it changes through multiple assignments.
  • Input is handled by the Scanner class using methods like \( nextInt() \) and \( nextDouble() \).
  • A method signature is your roadmap: it tells you the name, what the method needs (parameters), and what it returns.

Cross-reference: To learn more about how to change variable types during assignment, see the upcoming chapter "Casting and Range of Variables".