In Java, a variable is the name of memory location that are used like a container to store values during Java program execution. Every variable must be assigned a data type to define the kind of data it can hold. In Java, variables are categorized into three types: local, instance, and static variables. In this chapter, we will learn about the Java variables and types with the help of examples.
Before understanding Java variables and their types, you must be familiar with what a variable is and how it is named.
A variable is the name of a reserved area allocated in memory. In other words, it is a name of the memory location. It is a combination of "vary + able" which means its value can be changed.

There are three types of variables in Java:

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.
Let us take an example to demonstrate the Local Variable in Java.
Output:
Variable: 10
A variable declared inside the class but outside the body of the method, is called an instance variable. It is not declared as static.
It is called an instance variable because its value is instance-specific and is not shared among instances.
Let us take an example to demonstrate the Instance Variable in Java.
Output:
Student Name is: Deepak Age: 19
A variable that is declared as static is called a static variable. It cannot be local. You can create a single copy of the static variable and share it among all the instances of the class. Memory allocation for static variables happens only once when the class is loaded in the memory.
Let us take an example to demonstrate the Static Variable in Java.
Output:
S1's age is: 23 S2's age is: 23
Let’s see some other examples for better understanding about the Java variables:
In the following program, we use variables to store values and perform an addition operation. The variables a and b hold values, c stores their sum, and the result is printed.
Output:
20
This example demonstrates implicit type casting in Java. The integer variable a stores the value 10, which is automatically converted to a float and assigned to f. Both values are then printed, showing that Java safely converts an int to a float without data loss.
Output:
10 10.0
This example demonstrates explicit type casting from float to int in Java. Directly assigning f to a causes a compile-time error because a float cannot be automatically converted to an int. By using (int)f, the float value 10.5 is cast to 10, truncating the decimal part, and both values are printed.
Output:
10.5 10
This example demonstrates overflow during explicit type casting in Java. The integer value 130 is explicitly cast to a byte, but since the byte range is from -128 to 127, the value overflows and results in -126.
Output:
130 -126
This example shows type promotion in Java. Although a and b are bytes, the expression a + b is automatically promoted to int, causing a compile-time error when assigned to a byte. By explicitly casting the result to byte, the value is stored in c and printed successfully.
Output:
20
We request you to subscribe our newsletter for upcoming updates.