Please enable JavaScript to use CodeHS

Variables in Java

Learn how to store data in Java.

By Evelyn Hunter

What is a variable?

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.

int numLives;
Plain text

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:

int numLives = 3;
Plain text

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:

numLives = 2;
Plain text

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:

  • What happens when you assign different values to the variable numLives?
  • How you can print a mixture of variables and text, or strings?

Types of Variables

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:

  • integer (int): whole numbers, such as -5, 0, 15. Example: int numLives = 3
  • double: number with a decimal, such as 43.67, -0.11, 45.2543. Example: double cost = 40.25
  • String: text - note that strings are always surrounded by quotes. Example: String name = "Baby Yoda"
    • You may notice that 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.
  • boolean: either true or false. Example: boolean isJedi = true;

Take a look at the example to see the different variable types in action.

  • Can you identify the type for each variable?
  • Notice how the Boolean variable type does not include quotes around the value true. If you were to add quotes, what type of variable would it be?
  • Can you change the type of age by changing the type of data?

Naming Variables

When naming variables in Java, follow these rules:

  • no spaces
  • must start with a letter, $, or _
  • variables are case sensitive
  • written in lower camel case (the first word starts with a lower case letter, the rest of the words start with a capital letter)

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.


Practice!

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:

My favorite television show is Game of Thrones.
The best season is season 3, because of the incredible plot twists.
Plain text