There are some concepts in Java, or any programming language for that matter, that you cannot go without understanding and using. Variables are one of those concepts, but don’t worry because it’s also one of the simplest concepts in Java. By the end of this chapter, you’ll understand all you need to know about variables to start storing all of life’s information for later use.
Why Variables Are Important & Useful
Before we start on Java and its variables, we first need to assert why we program:
We program to capture and manipulate information, data, to create new data for users to consume.
That data can be how much money is in your bank account or the pass code to access it. It could be how many classes you have this week and each course could have more data that says what its title is, who the teacher is, and what its about. That data could even be a picture on Instagram, how many times its been double-tapped, and who owns the picture. Or, a more fun example is data about the release angle of your Angry Bird and where it’ll land on the tower to knock it down. All of that data has to be stored somewhere in order to be manipulated and displayed to you, the user. That’s exactly what variables are used for. We can store a single data point, or value, in a variable for later use. Let’s use the example of the bank account to understand why variables are important and what they are useful for.
Using Variables to Store Bank Account Data
Suppose we are making a new bank account. We need to store some information about the account and its owner for later use. The account will have a unique identification number, a balance, a status of whether or not it’s active, and a pass code (pin number). Further, the account will have information about the owner: the owner’s name, address and birthday. All in all, our bank account will have the following variables:
- pass code
- owner’s name
- owner’s address
- owner’s birth year
- owner’s birth month
- owner’s birthday
Now, from above, you know that we are able to store all of that information in a single variable to use it later. To do this in Java, we can use a String which allows us to store, or represent, character strings. Making a string in Java is simple. A string is an instance of Java’s String class which is provided to us.
Single String Variable to Store Our Account Information
Let’s make a String variable to hold our above bank account information to see how we can create and use variables:
/*This is a multi-line comment.
It won't be compiled by the compiler*//*Declare and instantiate a single variable to store information about our bank account*/
String bankAccount = "identifier: 1234567828;
balance: 1301.78; passcode: 9889; active: true;
owner's name: Lincoln Daniel;
owner's address: 325 JavaPlace, UnitedJavas 19081;
owner's birthday: May 26, 1993";/*print out the data stored in the bankAccount String variable.
All the information in the bankAccount variable will be printed.*/
System.out.println("Our bank account: " + bankAccount);
Now we have our whole bank account stored in that single variable which is a String. Notice that to create a variable, we say what type of variable it is, a String in this case, then give it a name, bankAccount, and finally use the equals operator (=) to set it equal to something. In this case, something is all of our bank account information. Finally, no two variables can have the same name and we’ll learn why later.
In that code block, we have a single String variable called bankAccount that holds all of the information about our bank account. If we later want to check our bank account, we will print it to the system and see that our account’s identifier is 1234567828, balance is 1301.78 dollars, passcode is 9889, its true that it’s active, the owner’s name is Lincoln Daniel, the owner’s address is 325 JavaPlace, UnitedJavas, 19081, and the owner’s birthday is May 26, 1993.
But what if we leave the bank and come back and just want to know what our balance is? That single variable won’t be able to give us our account balance alone. Instead, it’ll give us all the information about our bank account. That is bad for many reasons. Imagine going to the ATM and having all of that information about you and your bank account displayed on the screen for the guy behind you to peek at. That would not be safe.
In the next chapter, we will learn about datatypes in Java and how we can use them to store each individual data point about our bank account in seperate variables so that we can later manipulate and use each one individually.