In this series, I hope to share with you a better way of learning and teaching Java. My approach is simple: relate the concepts, which may seem complex, to real human experiences in order to make them more easily understandable and enjoyable.
I went from newb to interning at IBM, NASA, & Medium.com in a year, so I wrote Java for Humans to teach anybody with the slightest bit of interest in computer science, software engineering, and programming/coding how to begin by learning Java, the most widely used and deployed programming language today. However, Java for Humans won’t be like other coding books. I intend to relate coding to human experiences with an aim of making it easier for beginners to learn and have a connection with Java.
Each part of this series will take on a unique Java concept either in full or in bitesize. If a concept needs expansion, I may refer you to finish reading in the appropriate chapter of the Java for Humans book here.
I will add new concepts to this series as time permits, but if you wish to read ahead, you can read the whole book here.
Originally published here as part of the Java for Humans beginner’s book.
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.
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.
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:
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 Stringwhich 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.
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.