Variable assignment in Java means giving a value to a variable. A variable is like a container that holds data that can be used and manipulated throughout your program. When you assign a value to a variable, you are telling the program to store a specific piece of data in that container.
In Java, you declare a variable by specifying its type and name, then assign a value to it using the =
operator. The variable name is on the left side of the =
, and the value you want to assign is on the right side.
Here's a simple example:
int age = 16; // declaring an integer variable named age and assigning it the value 16
In this example:
int
is the data type, indicating that the variable will hold an integer.
age
is the variable name.
16
is the value assigned to the variable.
Let's look at some more examples:
double price = 19.99; // declaring a double variable named price and assigning it the value 19.99
char grade = 'A'; // declaring a char variable named grade and assigning it the value 'A'
boolean isJavaFun = true; // declaring a boolean variable named isJavaFun and assigning it the value true
Imagine you are running a small store, and you need to keep track of your inventory. You can use variables to store the quantity of each item.
int apples = 50; // you have 50 apples in stock
int oranges = 30; // you have 30 oranges in stock
If you sell 10 apples, you can update the value of the apples
variable:
apples = apples - 10; // now you have 40 apples in stock
Let's create a simple program that demonstrates variable assignment and updates the values of variables.
public class InventoryTracker {
public static void main(String[] args) {
// Declaring and assigning initial values to variables
int apples = 50;
int oranges = 30;
// Printing initial stock
System.out.println("Initial stock:");
System.out.println("Apples: " + apples);
System.out.println("Oranges: " + oranges);
// Selling some apples and oranges
apples = apples - 10; // selling 10 apples
oranges = oranges - 5; // selling 5 oranges
// Printing updated stock
System.out.println("Updated stock:");
System.out.println("Apples: " + apples);
System.out.println("Oranges: " + oranges);
// Receiving new stock
apples = apples + 20; // receiving 20 more apples
oranges = oranges + 15; // receiving 15 more oranges
// Printing final stock
System.out.println("Final stock:");
System.out.println("Apples: " + apples);
System.out.println("Oranges: " + oranges);
}
}
We declare two integer variables, apples
and oranges
, and assign them initial values.
We print the initial stock of apples and oranges.
We update the values of the variables to reflect sales (subtracting sold items).
We print the updated stock.
We update the values again to reflect receiving new stock (adding received items).
We print the final stock.
Now that you understand how variable assignment works, try solving this problem:
Problem: Create a Java program that tracks the number of books in a library. Start with an initial stock of 100 fiction books and 200 non-fiction books. Update the stock after lending 15 fiction books and 25 non-fiction books. Then, update the stock again after receiving 10 new fiction books and 20 new non-fiction books. Print the stock at each stage.
Good luck, and happy coding!