Java datatypes have a simple purpose and that is to store different types of data in appropriate variables for efficiency in later use. Look at it in the case of a dog and an apple. Your dog is an animal and your apple is a fruit. You wouldn’t try to do the same things with your dog that you would with your apple, would you? Not sure? Try to make your apple bark. Another fortune of datatypes is predictability.
Why Datatypes are Important & Useful
Datatypes Give You the Power of Predictability
If you have a dog, you know for a fact that it can bark, so it would be okay to ask it to do just that. If you have an apple, you know for a fact it is okay to eat it because that’s what apples are for. But what if someone wrapped an apple in a dog suit and gave it to you without you knowing? You would try to make that dog bark, but nothing would happen. You would try to take it for a walk and it wouldn’t move. This is the same idea with datatypes in Java.
You can’t count with a String because strings represent characters that make concepts like words and stories like the one you are reading now. On the other hand, you can count with integers. However, if you try to make a word with an integer, nobody would understand it because integers only represent numbers.
We can use this same logic towards data in Java. If you store your data in variables of appropriate datatypes, you will be able to safely use them as you wish later because you know exactly what type of data they are. Let’s see how we can use datatypes in Java to store our bank account information more efficiently.
A Better Way to Store Our Account Information
Remember that variables are useful for storing information for later use and manipulation. We want it to be easy to get only the information we need, use it as we want, and move on. Given that, we do not want to store all of our information in a single variable. Let’s see how we can make this better.
So we have nine pieces of information about our account that we have to store in variables. It helps if we first understand what types of data we are trying to store:
- identifier is a number
- balance is a number with a decimal point (floating point number)
- pass code is a number
- active is a true or false value
- owner’s name is a character string
- owner’s address is a character string
- owner’s birth year is a number
- owner’s birth month is a character string
- owner’s birthday is a number
Now that we know what types of data we need to store, let’s see how Java can help us. Java has primitive data types for us to use to store our information. We can store numbers as int variables, floating point numbers as double, and true or false values as boolean.
If we wanted to store a single character in a variable, we could use the char primitive type. Along with those primitive types, Java also allows us to store character strings in String variables. There are four more primitive data types provided by Java, but they are not necessary for beginners.
Store Each Piece of Data in its Own Variable
Let’s make nine individual variables to store our bank account’s nine individual data points:
/*Declare and instantiate nine different variables to store our account's nine different data points*//*declare an int variable to store our account identifier*/
int id = 1234567828;/*declare a double variable to store our account balance*/
double balance = 1301.41;/*declare an int variable to store pass code*/
int passCode = 9889;/*declare a boolean variable to store our account's active status*/
boolean isActive = true;/*declare a String variable to store our account owner's name*/
String ownerName = "Lincoln W Daniel";/*declare a String variable to store our account owner's address*/
String ownerAddress = "325 JavaPlace, UnitedJavas 19081";/*declare an int variable to store our account owner's birth year*/
int ownerBirthYear = 1993;/*declare a String variable to store our account owner's birth month*/
String ownerBirthMonth = "May";/*declare an int variable to store our account owner's birth day*/
int ownerBirthDay = 26;/*print out our bank account's balance*/
System.out.println("Our bank account's balance: " + balance);
Now we have our bank account and all of it’s data is stored in variables with their appropriate datatypes. Given our new setup, we can manipulate parts of our account more easily without having to deal with parts we don’t want to. The bank teller can find our account by looking up it’s id, which is 123456782, and ask us to verify the ownerBirthday on the account to make sure we are the owner of the account before telling us our balance. Later we can go to the ATM use our passCode to view our account’s balance. We can even change our balance by adding money to it or taking money out.
We can do all of these individual data manipulations because we have our data separated in appropriately typed variables. It’s pay day, so let’s update our account balance.
/*add 600 dollars and 50 cents to our bank account*/
balance = balance + 600.50;/*print out our new bank account balance*/
System.out.println("Our account's new balance: $" + balance);
//prints "Our account's new balance: $1901.91"
After adding $600.50 to our account’s balance, it went from $1301.41 to $1901.91 and is represented in our balance variable. As we learn more awesome Java concepts, we’ll see our account balance increase considerably. It definitely pays to know how to increase your account balance ☺.
This was originally published on 1Liner.dev.