How to use Java variables
Java variables are containers that can store different Java data types. They include local variables, instance variables and static variables. While the size of Java variables cannot be changed after the fact, you can change their content after creating them.
What are Java variables?
Every programming language has variables that are used to work with the code in a program. Variables are containers that store data of a specific data type (and only data of that type). In Java, variables can contain Java’s primitive data types like whole numbers, floating point numbers, truth values and individual digits. They can also store complex data types like Java strings. Variables in Java have a specific, clearly defined size that can’t be changed after the fact. However, the contents of a variable can be changed later. In this tutorial, we introduce the different variable types and show how to create variables for different data types.
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups
How to declare and initialize var
in Java
The first thing you’ll need to know is how to declare and initialize Java variables. This process is the same for all types of variables. To declare a variable you’ll need two parameters. The first is the data type that you want to store in the variable. The second is the name of the variable.
When you then initialize the variable, there are 3 ingredients. You’ll not only need the data type and name of the variable as above, but also a value for it. Initializing a variable amounts to assigning a value to a previously blank variable.
Here’s what the syntax looks like:
What are the three variable types in Java?
There are three types of variables in Java: local variables, instance variables and static variables.
Local variables
Local variables in Java are declared in the body of a method, constructor or block. The variable can then only be used within that method. Here’s how that looks in practice:
The output will look as follows:
Instance variables
Instance variables are created inside a class but outside a method, constructor, or block. They come about when you create an object with the keyword “new”. Unlike local variables, instance variables have standard values. For numbers, the standard value is 0 or 0.0. For Booleans, it is false. For object references, it is null.
In the following example, you can see how instance variables are used in Java. In the code we’ll create a group of people that want to contribute to a birthday gift and list what each person has contributed.
Here’s the output for this code:
Static variables
While the other two Java variable types cannot be declared statically, static variables can. They are declared outside of a method, constructor or block and thus belong to the class and not to an instance. They are used by all the instances in a class. Static variables are allocated memory when the class is loaded in the memory. Here’s an example in code:
Here’s the output:
How to create variables with different data types
The process of creating Java variables is pretty similar across data types. We’ll show you some examples for the most common data types and explain their differences.
boolean
A Boolean can only contain the truth values true or false. It’s declared as follows:
Here’s the output:
int
int is the most commonly used data type for whole numbers. You can declare a Java var for an int like this:
Here’s the output:
float
float is used for decimal numbers. Here’s how you can declare a float var in Java:
Here’s the output:
char
char contains a single character that’s notated in single quotation marks. Here’s how that looks in code:
Here’s the output:
String
In addition to the primitive data types we looked at above, Java variables can also contain complete strings. Strings should be entered in double quotation marks:
Here’s the output: