Variables are the bread and butter of the coding. Without variables, apps would have no interactivity and no way to manipulate information. Therefore, learning about variables in Java should be among the very first things you do when picking up the language. In this post you will find everything you need to know.
What are variables in Java?
The best way to understand variables is to think back on math lessons. You may remember solving algebra problems that looked like this:
If 3 + n = 5, then what is n?
Of course, the answer is n = 2.
How variables work in programming. A variable is a label (usually a word) that can be replaced with a piece of data. This then allows us to ferry information around our app by retrieving values from other sources (such as the web or user input) or performing various functions depending on the value of the variable.
For example, we can create a variable for a computer game called “health”. This would represent a number that in turn would describe how much health a player had left. If the player is shot, the health goes down (health = health – 1). If the player has no health, the game ends.
Types of variables in Java
A variable that contains an integer, as in the previous examples, is called an “integer” or “int” for short. However, this is just one type of variable in Java.
Understanding this is important because we must choose (declare) the type of variable that is the first time we create it. This is because Java is “statically written” as opposed to a language like Python which is “dynamically written.” There are pros and cons to each approach.
See also: Python vs Java: What language should you learn and what are the differences?
When you declare your variable, first write the type of variable you want, then the name you want to give it and then the value you want to assign to it at startup:
The other types of variables in Java are:
- byte – stores integers from -128 to 127
- card – stores numbers from -32,768 to 32,767
- int – stores integers from -2,147,473,648 to 2,147,483,647]
- long – stores an even wider range of integers
- float – stores fraction numbers of up to 6-7 decimal places
- double – stores fraction numbers up to about 15 decimal places
- boolean – stores a binary true or false value
- char – stores a single alphanumeric character / ASCII value
These are called “primitive data types” because they are built into the Java function and can no longer be broken down.
The right variable for the job
Why are there so many different options for storing numbers? This is because good programming should be efficient with memory. Bytes are allocated less memory than integers, so if you are absolutely sure that the value will never be higher than 127 or lower than -128, you can safely choose to use them. However, due to Java’s strong writing, you need to know this for sure from the beginning and explain the variable correctly. Using a boolean is the most effective of all, as it only takes up a single piece of information! You can use booleans as “on / off” switches.
Good programming should be efficient with memory.
With that said, most casual programming does not have to be so efficient that they choose to switch over integers. It is often safe to use int for most of your integers.
Strings and lists
If you are familiar with variables in Java, you may be wondering why I left strings from the list. A string is a series of alphanumeric characters and symbols that can be used to store names, phone numbers, or entire text texts.
But “string” is not a keyword in java but is actually a class. You do not really need to know what that means, but our Java beginner’s course will teach you the basics.
For the most part, you are sure to use String in the same way as all other variables. The main difference is that you have to use the word “String” in capital letters. As a class, String also has methods, which means that it can provide useful information about itself, such as its length.
The same goes for other types, such as Arrays. Arrays in Java are variables that contain multiple values. These allow you to store things like high-score lists or phone numbers and can also be organized, counted and manipulated in other ways.
Aslo read: How to print a matrix in Java
Other types of variables in Java
There are other ways to categorize variables in Java and other ways to manipulate data. For example, a constant is a variable whose value never changes. This is especially useful for writing more readable code.
Variables also work differently depending on how they interact with their class (instance variables versus static variables). You do not have to understand these differences for a while, but keep your eyes open for more tutorials looking at these nuances.
Do you want to continue your training in variables in Java directly? Alternatively, see our guide to the best free and paid resources to learn Java. We also offer a comprehensive introduction to the Android app development course, run by Gary Sims, which includes a whole mini course on Java programming!