We may not have the course you’re looking for. If you enquire or give us a call on +61 1-800-150644 and speak to our training experts, we may still be able to help with your training requirements.
Training Outcomes Within Your Budget!
We ensure quality, budget-alignment, and timely delivery by our expert instructors.
Java Variables act as a data container that stores the data values during the execution of a Java program. Every variable is assigned a data type denoting the amount of data and the type of data it can store. Essentially, variable in Java can be described as the name used for memory allocation – the basic unit of storage in a program.
According to the TIOBE Index for the programming community, Java stands as 3rd most used programming language after Python and C. Java Variables provide storage facilities for data values. In Java, there are three different types of variables. Explore this blog to learn more about it.
Table of Contents
1) Learn about Variable Declaration
2) Learn about the Initialisation of Variables
3) Know about the Types of Variables in Java
4) Understanding Type Conversion and Type Casting
5) Conclusion
Learn about Variable Declaration
A variable declaration in Java happens once you mention the data type and give a unique name to the variable. By data type, we mean the type of data stored in this variable. Further, this variable also carries a name. An example of Syntax, as shown below, can be used to help better understand:
Syntax:
variable_Name
Example:
int myNum
where int is the data type in Java, and myNum is the variable's name. In this way, a name can be allotted to a memory location. Values can be assigned to this variable in two ways:
1) Variable Initialisation
2) Assigning values by taking input
![Java Training](/_files/images/Java_Training%287%29.png)
Learn about the Initialisation of Variables
In Java, a variable initialisation happens once the same variable is declared beforehand. It also has an additional component other than data type and variable name, and that is a value, which is an initial value stored in the defined variable. This can be understood through the same syntax used above with an example.
Syntax:
variable_Name = value;
Example:
int myNum = 15;
where int is the Data Type in Java, myNum is the variable's name, and the value 15 is assigned to the variable myNum.
Know about the Types of Variables in Java
In Java, there exist different types of variables, whose characteristics are discussed in detail below:
Local Variables
Local variables in Java are declared within a block, method, or constructor. You create these variables when you enter a block or call a function. After moving out from the block, or when there are call returns from the process, these variables get destroyed. The scope of such variables remains only within the block in which these variables are declared, i.e., you can access these variables within that block only.
Instance Variables
Instance variables in Java are those that are non-static and defined in a class outside of any block, method, or constructor. Since, Instance Variables are defined in a class, when there is an object creation, these variables get created, and when the object is destroyed, these variables are also eliminated. Unlike Local Variables, access specifiers are used for Instance Variables. If an access specifier is not declared, a default access specifier will be used.
It is not mandatory to initialise an instance variable as the default value is zero. These variables can only be accessed by creating objects.
Static Variables
In Java, Static Variables, called class Variables, are similarly defined as Instance Variables. The only difference is that static variables are defined with the static keyword in the class but outside a block, method, or constructor. Irrespective of how many objects we create, we will have only one copy of a static variable for every class. At the start of the program execution, static variables get created, and when the execution ends, it is destroyed automatically.
Learn how to develop a web application using Java programming by signing up for our Web Development Using Java Training course now!
Understanding Type Conversion and Type Casting
In Java, a variable of one data type can have a value from another. We will illustrate this with the help of two cases:
Case 1: It is a case where a smaller capacity variable can be assigned a value coming from another variable with a bigger capacity.
Example:
double x;
int y = 10;
x=y;
In this code, the process happens automatically and is non-explicit. It is known as Type Conversion in Java.
Case 2: It is a case where a large capacity variable can be assigned a value coming from another variable whose capacity is smaller.
Example:
double y;
int x = 10;
y= (int) x;
In this code, a typecast operator has to be mentioned explicitly. In Java, this process is termed Type Casting. In cases where a typecast operator is not specified, the compiler will throw an error. Since the compiler enforces the rule, the programmer becomes aware that the happening conversion might code some data loss.
Learn to develop, understand and implement JavaScript with HTML by signing up for our JavaScript For Beginners course now!
Conclusion
After reading this blog, you will now be able to declare and initialise a Java Variable and get an awareness of the types of variables. Every variable in Java is assigned a data type denoting the amount of data and the type of data it can store. Lastly, by understanding the process of Type Casting and Type Conversion, you can allocate value assigned in one type of variable to a completely new variable that has a different data type.
Want to get an understanding and develop your knowledge on the working of Java EE APIs, then signup for our Introduction To Java EE Training course now!