In Java, an assignment statement stores a value in a variable or replaces the value already there. See how `int x = 5;` initializes a variable, while `x = 10;` updates it—and why this simple step supports changing program state and carrying out calculations.

Multiple Choice

What does the assignment statement do?

The assignment statement is a fundamental concept in programming that specifically sets or re-sets the value of a variable. When you use an assignment statement in Java, you are effectively telling the program to store a particular value in a variable, which can then be used or modified later in the code. For example, when you write `int x = 5;`, this statement not only creates a variable `x` but also assigns it the value of `5`. If you later write `x = 10;`, the assignment statement updates `x` to now hold the value `10`. This ability to change the value stored in a variable is essential for maintaining state and performing calculations or operations throughout the execution of a program. The other options pertain to different concepts in programming. Defining a constant, creating a new data type, or performing type casting involves more specific operations that do not directly relate to the core purpose of an assignment statement. Therefore, the assignment statement is uniquely tied to the action of assigning values to variables, making it a key pillar of programming in Java.

In Java, an assignment statement tells the program to store a value in a variable. That sounds simple, and it is—but this small action sits at the heart of nearly every useful program. A variable can hold a number, a piece of text, or a reference to an object. Assignment gives that variable a value to work with, and later assignments can change what it holds.

A variable is a named place for a value

Think of a variable as a labeled container. The label lets your code refer to its contents without needing to remember where the data lives in computer memory.


int score = 5;

Here, int says that score stores an integer, and 5 is the value placed there. This single line both declares the variable and gives it an initial value. That initial assignment is called initialization.

A variable can receive a different value later:


score = 10;

Now score holds 10 rather than 5. The variable’s name and type stay the same; its stored value changes. This is what it means to set or reset a variable’s value.

That distinction matters. A variable is not the value itself. It’s a name your program uses to reach a value, rather like a contact name on a phone. You can change the phone number saved under that contact without changing the contact’s name.

What happens when Java sees =

In a basic assignment, Java evaluates the expression on the right side of the equals sign, then stores the result in the variable on the left.


int total;

total = 12 + 8;

Java works out 12 + 8, then assigns the result, 20, to total.

The left side must identify somewhere that can receive a value, such as a variable. The right side can be a literal, another variable, or a more complex expression:


int price = 7;

int quantity = 3;

int cost = price * quantity;

The expression price * quantity produces 21, so cost is assigned that value.

The equals sign in Java is an assignment operator. It does not mean “these two things are equal” in the mathematical sense. It means “store the value on the right in the place named on the left.” That difference is easy to miss at first, but it’s essential when reading code.

Changing a value as a program runs

Assignments let a program keep track of changing information. A score can rise, a counter can increase, and a temperature reading can be replaced when a new measurement arrives.


int score = 0;

score = score + 5;

The second line may look odd if you read it as a math equation. In Java, however, the right side is evaluated first. The program reads the current value of score—zero—adds five, then stores the result back in score. The new value is 5.

This pattern appears in many everyday tasks:


int itemsInCart = 2;

itemsInCart = itemsInCart + 1;

After the second line runs, itemsInCart is 3. The program has updated its state: the information it currently remembers about the cart.

Java also offers a shorter form for common updates:


itemsInCart += 1;

This is called a compound assignment. It adds one to the variable and saves the result back into it. Similar operators include -=, *=, and /=:


int points = 20;

points -= 4;  // points is now 16

For a simple increase or decrease by one, Java has ++ and --:


int count = 0;

count++;

After this line, count is 1. These compact forms are handy, but the longer assignment can be clearer when someone is just getting familiar with the code.

Assignment is not the same as declaration

A declaration introduces a variable and gives it a type:


double temperature;

This line declares temperature, but it doesn’t assign a value. A local variable in a method must be assigned before the program reads it. For example, trying to print temperature before giving it a value will cause a compiler error.

You can declare and initialize a variable in one line:


double temperature = 21.5;

Or split the steps:


double temperature;

temperature = 21.5;

Both forms leave the variable with the same value. The first is often more concise, while the second can be useful when the value isn’t available at the moment of declaration.

This also explains why assignment does not create a new data type. The type is declared separately—int, double, String, and so on—and assignment places a compatible value into a variable of that type.

Types set the rules

Java is a strongly typed language. Each variable has a type, and that type limits which values it can hold. An int variable can store whole numbers, while a String variable stores text.


int age = 18;

String name = "Mina";

Trying to assign text to an integer variable won’t compile:


int age = "eighteen";  // incompatible types

The type rules help catch mistakes early. They also explain why some assignments need an explicit conversion, known as a cast. For instance, Java can convert a double to an int, but that conversion may discard the decimal part:


double measurement = 8.7;

int roundedDown = (int) measurement;

After the cast, roundedDown is 8. The cast controls how a value changes type; assignment itself still stores a value in a variable. These concepts can appear together, but they do different jobs.

A few lookalikes worth noticing

Assignment uses one equals sign:


score = 10;

Comparison for equality uses two:


if (score == 10) {

System.out.println("Score is ten.");

}

The first changes the value in score. The second checks whether score already has the value 10. Mixing them up can lead to confusing code, so it’s worth pausing over the symbols when you read a line.

A constant is different, too. In Java, the final keyword marks a variable that can be assigned only once:


final int DAYS_IN_WEEK = 7;

The variable is initialized with 7, and Java won’t allow a later assignment to change it. The assignment gives the constant its value; final is what restricts future changes.

Objects add one more useful detail

Assignment works with object variables as well, but the value stored in such a variable is a reference to an object—not the object’s full contents.


String greeting = "Hello";

String message = greeting;

Now greeting and message refer to the same string value. Assigning a different value to message does not change greeting:


message = "Hi";

Afterward, greeting still refers to "Hello", while message refers to "Hi". For mutable objects, two variables can refer to the same object, so changing that object through one reference may be visible through the other. That’s a subtle point, but it becomes important when programs work with lists, dates, or custom classes.

Assignment is one of Java’s simplest operations, yet it gives programs their ability to remember and update information. A declaration names a variable and sets its type; an assignment stores a value; later assignments can change that value as the program runs. Once that pattern feels familiar, calculations, counters, user input, and object handling all become easier to follow.