Variables are the alpha and omega of coding. Without variables, apps would have no interactivity and no way to manipulate information. Therefore, learning variables in Java should be one of the first things you do while learning the language. This post is everything you need to know.
What are variables in Java?
The best way to understand variables is to think back to your math class. You may remember having solved algebra problems that looked something like this:
If 3 + n = 5, then what is n?
The answer is of course n = 2.
This is how variables work in programming. A variable is a label (usually a word) that can replace a data item. This allows us to convey information in our app by pulling values from other sources (such as the web or user input) or by performing different functions depending on the value of that variable.
For example, we could 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, health will decrease (Health = Health – 1). If the player is out of health, the game ends.
Variable types in Java
A variable that contains an integer, as in the previous examples, is called an “integer” or “int” for short. However, this is only one type of variable in Java.
It is important to understand this as we will need to select (declare) the variable type when we first create it. This is because Java is “statically typed” as opposed to a language like Python which is “dynamically typed”. Each approach has advantages and disadvantages.
See also: Python vs Java: which 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 will assign to it at the beginning:
The other types of variables in Java are:
- Byte – stores whole numbers from -128 to 127
- short – stores numbers from -32,768 to 32,767
- int – stores whole numbers from -2,147,483,648 to 2,147,483,647]
- long – stores an even wider range of whole numbers
- float – stores fractions with up to 6-7 decimal places
- Double stores fractions with up to 15 decimal places
- boolean – stores a binary true or false value
- char – stores a single alphanumeric character / ASCII value
These are known as “primitive data types” because they are built directly into how Java works and cannot be further subdivided.
The right variable for the job
Why are there so many different ways to store numbers? This is because good programming should be efficient with memory. Bytes is allocated less memory than integers. So if you are absolutely certain that the value will never be higher than 127 or lower than -128, then you can safely use it. However, due to the strong typing of Java, you need to know this from the start and declare the variable correctly. Using a Boolean value is the most efficient of them all, as only one piece of information is needed! You can use Boolean values such as on / off switches.
Good programming should be efficient with memory.
With this in mind, most casual programming doesn’t have to be efficient enough to choose bytes instead of integers. It’s often safe to use int for the majority of your total numbers.
Strings and lists
If you’re familiar with variables in Java, you might be wondering why I crossed strings off the list. A string is a series of alphanumeric characters and symbols that can be used to store names, phone numbers, or entire text.
However, “String” is not a keyword in Java, but a class. You don’t really need to know what this means, although our Java Beginners Course will give you the basics.
For the most part, you can use String just like any other variable. The main difference is that you have to capitalize the word “String”. As a class, String also has methods so that it can provide useful data about itself, such as: B. the length.
The same applies to other types, e.g. B. Arrays. Arrays in Java are variables that contain multiple values. With these you can, for example, save lists with high scores or telephone numbers and organize, count and manipulate them in other ways.
Also read: How to print an array in Java
Other types of variables in Java
There are other ways to categorize variables in Java and other ways of manipulating data. For example, a constant is a variable whose value never changes. This is primarily useful for writing more readable code.
Variables also behave differently depending on how they interact with their class (instance variables versus static variables). You don’t need to understand these differences for a while, but stay tuned for more tutorials that will explore these nuances.
Would you like to continue your training in variables in Java right away? Alternatively, check out our guide for the best free and paid resources for learning Java. We also provide a full introduction to Gary Sims’ Android App Development Course, which includes a full mini course on Java Programming!