Archangel Macsika Sikademy Image
Archangel Macsika

Ultimate Tutorial to Learn Java Variables

Tutorial 9 of 9 | 6 minutes read
Ultimate Tutorial to Learn Java Variables - Sikademy

Introduction to Variables in Java

Variables are temporal storages that hold a value with a specific datatype. The datatype of a variable determines the kinds of value the variable can store, the size of the variable and range of values in memory, and the set of operations that can be performed on the variable.

Creating Variables In Java

Examples of variables in Java


int age = 108;
float allowance = 0.99f;
char grade = 'A';
boolean likesFood = true;
String greeting = "What's Good?";

In java, the process of creating a variable is known as declaring a variable.

To declare a variable, you must specify the type and assign it a value:

Syntax of a Java Variable


type variableName = value;

In the code sample above, "type" is one of the datatypes in Java such as int or String, and "variableName" as the term suggest is the name of the variable. The equal sign is used to assign values to the variable, and the value is always at the right of the equality sign.

Example in Java

Another way to create a variable is by declaring the type and name first before assigning a value. Let's illustrate this by rewriting the example above:


data type variableName;
variableName = value;

Notice the data type is written once and the variable name is repeated. The first line type variableName; is called variable declaration while the second line is called the variable assignment and should only be written after a variable declaration.

Example in Java

Declare Many Variables in Java

The above example shows how to create single variables; However, to declare more than one variable of the same type, use a comma-separated list:


data type variable1 = value1, variable2 = value2, variable3 = value3;

Example in Java

Let's put it in another format,


data type variable1, variable2, variable3;
variable1 = value1, variable2 = value2, variable3 = value3;

Example in Java

Display Single variables

The println() method is often used to display variables.

Save, compile, and execute the program.

Macsika 8

String Concatenation in Java - Combined Display of Variables

The process of combining variables together is called "string concatenation." This is usually done during the display of variables.

To combine text and a variable, use the + character:

Save, compile, and execute the program.

Welcome, Macsika

Combine a variable with another variable, use the + character:

-> If the data type of both variables are text i.e. using string or char, they will be concatenated

Save, compile, and execute the program.

WelcomeMacsika
Welcome Macsika
Welcome, Macsika

-> If the data type of one is a number and the other a text, the variable with the number will be automatically converted to text and then concatenated.

Save, compile, and execute the program.

Macsika 8

-> If both are numbers, the mathematical addition function will be performed on both variables and the result displayed as a single value (addition of the values of the variables). But to print the numbers without addition, concatenate a string ("").

Save, compile, and execute the program.

6
123
6
1 2 3

-> Finally, you can concatenate as many text and variables as you please.

Save, compile, and execute the program.

What's good Archangel Macsika?
You are 108 Years Old.
Your account balance is 0.99$

Types of variables in Java Programming Language

Don't worry if you do not understand these terms, this is just a beginner tutorial series. For now, we will simply define these variables, but we will go in-depth in intermediate and advanced tutorial series.

Just know that there are three kinds of variables in java:

1. Local Variable

A variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other methods in the class aren't even aware that the variable exists.

A local variable cannot be defined with "static" keyword.

2. Instance Variable

A variable declared inside the class but outside the body of the method, is called instance variable. It is not declared as static.

It is called instance variable because its value is instance specific and is not shared among instances.

3. Static/Class Variable

A variable which is declared as static is called static variable. It cannot be local. You can create a single copy of static variable and share among all the instances of the class. Memory allocation for static variable happens only once when the class is loaded in the memory.

Wrap Off

If you run into errors or unable to complete this tutorial, feel free to contact us anytime, and we will instantly resolve it. You can also request clarification, download this tutorial as pdf or report bugs using the buttons below.

Tutorial Exercise

Assign values to the variables below and print the results together.


public class TutorialClass {
  public static void main(String[] args) {
    int age = 
    String lastname = 
    String firstname = 
    String fullname = 
    String greeting = 
    double accBalance = 
  }
}

Enjoy this Tutorial? Please don't forget to share.