1. Using Objects and Methods

Assignment Statements And Input

Assignment Statements and Input

students, imagine writing a Java program that keeps track of your game score, stores a user’s name, or updates the balance in a bank app 💻. Every one of those tasks depends on two basic ideas: assignment statements and input. These are simple tools, but they are everywhere in AP Computer Science A. In this lesson, you will learn how Java stores values in variables, how assignment changes those values, and how programs accept input from the user through objects and methods. By the end, you should be able to explain the key vocabulary, trace code accurately, and connect these ideas to the larger topic of using objects and methods.

What Assignment Statements Do

An assignment statement gives a value to a variable. In Java, the equals sign $=$ does not mean “is equal to” like it does in algebra. Instead, it means “store the value on the right side into the variable on the left side.” This is one of the most important ideas in Java fundamentals.

For example:

$$\texttt{int score = 10;}$$

This statement creates a variable named

score$ of type $

int$ and stores the value $10 in it. If later the program runs:

$$\texttt{score = 15;}$$

the value of

score$ changes from $10$ to $15. That is why assignment statements are called “assignments”: they assign a new value to a variable.

A variable is like a labeled container 📦. The label is the name, and the container holds the current value. In AP Computer Science A, you must be able to read assignment statements carefully and predict the value of variables after several lines of code run.

A very common mistake is thinking the order does not matter. In Java, order matters a lot. Consider:

$$\texttt{int a = 3;}$$

$$\texttt{int b = a + 2;}$$

$$\texttt{a = 10;}$$

After the first line,

a = 3. The second line uses the current value of

a$, so $

b = 5$. The third line changes only $

a$ to $10$. The value of $

b$ stays $5 because it already stored its own value. Java does not automatically update

b$ when $

a changes.

This idea helps explain why assignment statements are important in object-oriented programs. Many objects store state, and assignment is one way programs update that state.

Variables, Expressions, and Reassignment

Assignment statements often use expressions on the right side. An expression is anything that evaluates to a value, such as a number, a variable, or a calculation.

Examples include:

$$\texttt{x = 4;}$$

$$\texttt{x = x + 1;}$$

$$\texttt{name = "Ava";}$$

The statement $\texttt{x = x + 1;}$ is especially important. It does not mean $x$ equals $x + 1$ in a mathematical sense. Instead, Java does this in steps:

  1. Find the current value of $x$.
  2. Add $1$.
  3. Store the new result back into $x$.

If $x$ was $7$, then after the assignment it becomes $8$. This is called reassignment because the variable is assigned a new value.

AP CSA questions often test whether students understand that variables can change over time. For example, if a program starts with:

$$\texttt{int total = 5;}$$

$$\texttt{total = total + 3;}$$

$$\texttt{total = total - 2;}$$

then the final value of $ exttt{total}$ is $6$.

Assignment statements also follow type rules. An $ exttt{int}$ variable can hold only integer values, while a $ exttt{double}$ variable can hold decimal values. A $ exttt{String}$ variable holds text.

Examples:

$$\texttt{int age = 16;}$$

$$\texttt{double price = 2.99;}$$

$$\texttt{String city = "Miami";}$$

If the type and value do not match, the code will not compile. For instance, $\texttt{int number = 3.5;}$ is invalid because $3.5$ is not an integer.

Input: Letting the User Give Data

Input means data that comes into a program from the outside. In Java, input is often read from the keyboard using an object from a library. In AP Computer Science A, the most common class for input is $ exttt{Scanner}$.

To use a $ exttt{Scanner}$, a program usually does two things:

  1. Create a scanner object.
  2. Call a method on that object to read input.

Example:

$$\texttt{Scanner input = new Scanner(System.in);}$$

$$\texttt{String name = input.nextLine();}$$

The first line creates a $ exttt{Scanner}$ object named $ exttt{input}$. The second line calls the method $\texttt{nextLine()}$, which reads an entire line of text from the keyboard and stores it in $ exttt{name}$.

Because input uses objects and methods, it connects directly to the course topic “Using Objects and Methods.” The scanner is an object, and $\texttt{nextLine()}$, $\texttt{nextInt()}$, and $\texttt{nextDouble()}$ are methods. A method is a set of instructions attached to an object or class that performs a task.

Here is a short example of input in action:

$$\texttt{Scanner input = new Scanner(System.in);}$$

$$\texttt{System.out.print("Enter your age: ");}$$

$$\texttt{int age = input.nextInt();}$$

$$\texttt{System.out.println("You are " + age + " years old.");}$$

If the user types $17$, the variable $\texttt{age}$ stores $17$, and the program prints a message using that value.

How Assignment and Input Work Together

Assignment statements and input work as a team 🤝. Input brings information into the program, and assignment stores that information in variables so the program can use it later.

Suppose a teacher wants a program to collect a student’s quiz score:

$$\texttt{Scanner input = new Scanner(System.in);}$$

$$\texttt{System.out.print("Enter quiz score: ");}$$

$$\texttt{int score = input.nextInt();}$$

$$\texttt{int bonus = score + 5;}$$

$$\texttt{System.out.println("Your adjusted score is " + bonus);}$$

The input method $\texttt{nextInt()}$ reads the score. The assignment statement stores it in $ exttt{score}$. Then another assignment computes $\texttt{bonus}$ from that value.

This pattern appears in many real-world programs. A shopping app might ask for a quantity, store it in a variable, and then calculate the total cost. A weather app might read a city name and store it before looking up the forecast. A game might read a player’s move and store it before updating the game board.

When tracing code, always pay attention to three things:

  • What value does the input method return?
  • Which variable receives that value?
  • How does later code use that variable?

If you know those answers, you can predict the program’s behavior much more accurately.

Common AP Computer Science A Reasoning Skills

The AP exam often asks you to trace code, identify values, or predict output. Assignment and input questions may look simple, but they test careful reading.

Here are some important reasoning habits:

  • Read code top to bottom.
  • Remember that assignment stores a value right now.
  • Do not treat $=$ like algebra.
  • Track changes to each variable separately.
  • Pay attention to type compatibility.
  • Know which input method matches the data type.

For example, consider:

$$\texttt{Scanner input = new Scanner(System.in);}$$

$$\texttt{int x = input.nextInt();}$$

$$\texttt{x = x + 2;}$$

$$\texttt{System.out.println(x);}$$

If the user enters $4$, then $\texttt{x}$ becomes $4$, then $6$, and the output is $6$.

Another important detail is that input methods return values. $\texttt{nextInt()}$ returns an $\texttt{int}$, $\texttt{nextDouble()}$ returns a $\texttt{double}$, and $\texttt{nextLine()}$ returns a $\texttt{String}$. If the program expects one type but uses another, the code may fail or behave unexpectedly.

Also, remember that assignment can happen with object references too. For example, if a variable stores an object reference, assignment copies the reference, not necessarily the whole object. That idea becomes more important later in the course, but the basic rule is still the same: assignment changes what a variable refers to.

Conclusion

Assignment statements and input are foundational skills in Java and AP Computer Science A. Assignment lets a program store and update values, while input lets a program receive information from the user. Together, they make programs interactive, flexible, and useful. students, if you can trace assignment statements carefully, understand how input methods return values, and connect both ideas to objects and methods, you are building a strong foundation for more advanced Java topics. These skills are not just test content—they are the building blocks of real programs.

Study Notes

  • An assignment statement uses $=$ to store the right-hand value into the left-hand variable.
  • In Java, $=$ does not mean mathematical equality.
  • A variable holds a value that can change during program execution.
  • Reassignment means giving a variable a new value.
  • Expressions on the right side are evaluated before the value is stored.
  • Type matters: $\texttt{int}$, $\texttt{double}$, and $\texttt{String}$ variables have different rules.
  • Input means data entering a program from outside, often through the keyboard.
  • AP CSA commonly uses $\texttt{Scanner}$ for input.
  • $\texttt{nextInt()}$ reads an integer, $\texttt{nextDouble()}$ reads a decimal, and $\texttt{nextLine()}$ reads a line of text.
  • Input methods return values, and assignment statements store those values in variables.
  • Assignment statements and input together let programs collect data and use it later.
  • Careful tracing of variables is essential for AP Computer Science A success.

Practice Quiz

5 questions to test your understanding