Learn how to store data in Java.
In programming, variables are used to store information. By storing values in variables, programmers are able to reference and/or manipulate information throughout a program. Let’s say we create a game where players have a specific number of lives. We could use a variable to store the number of lives.
We’ll start to create a variable by declaring it. In Java, there are two key components to declaring a variable: Determining the type of data that is going to be stored in the variable, and naming the variable. We can determine the type of the variable by first writing the data type we want stored in the variable. For example, if we were creating a game where a player has a certain number of lives, we’d want to create a variable that stored the total number of lives that player has. Since that number is an integer, we declare the variable as a type int
to clarify what type of data is allowed to be stored in our variable.
Right now, we have an empty storage container called numLives.
Now we need to initialize the variable by assigning an initial, or starting, value to it:
Here, we’ve initialized the variable numLives
and given it a starting value of 3. Let’s say the player loses a life and now has 2 lives. We can change the value of a variable by assigning a different value to it:
Notice how we don’t use the keyword int
when assigning a new value to an existing variable. This is because we’re not creating a new variable. Instead, we want to change the value of the original variable.
The program below puts all of this together. Explore the example by running it and answering these guiding questions:
numLives
?In the examples so far, we’ve only used numerical data. However, variables can store other types of information as well. The variable type is based on the type of data the variable holds. Let’s take a look at the different types of variables:
int numLives = 3
double cost = 40.25
String name = "Baby Yoda"
String
is uppercase while the other data types are lowercase. This is because String
is an object data type, whereas the others are primitive data types. All object data types are written in uppercase when declaring a variable and all primitive types are lowercase.true
or false
. Example: boolean isJedi = true;
Take a look at the example to see the different variable types in action.
true
. If you were to add quotes, what type of variable would it be?age
by changing the type of data?When naming variables in Java, follow these rules:
Additionally, while not necessarily a rule, you’ll want to name your variable in a way that describes what information it’s holding. This makes your program easier to read and understand.
In the following program, see if you can declare and initialize a couple of variables, and then print their values to the console. Create a variable that stores the name of your favorite television show, and create another variable that stores which season of that show is your favorite. Then, print that information to the console, including an explanation for why you chose what you chose. Your program might print something like this: