Hellow there, welcome to the codingaliens..!!. In the previous tutorial, we learned about printing statement in Java programs.Now we will learn how we stores the values in Java programs and what are the types of data.
In this tutorial, you will learn -
- What is variable
- Variable declaration
- Variable initialisation
- The types of variables
- Data types in java
What is a variable ?
A Variable is also called a container which holds the value, during the the storing the data in a Java program.Every variable is assigned a data type which decides the types of data and quantity of value it can hold.
You can use variables in the Java program in two ways are -
First way - in the two steps
Second way - in the single step
First way : for using a variable in the Java program you need to perform two
STEPS :
STEPS :
Step 1 variable declaration.
Step 2 variable initialisation.
> Variable declaration :
To declare variable, you need to specify the data type and give the unique name to the variable in the Java program.
Examples of variable declarations are -
> Variable initialisation :
Examples of valid initialisations are-
Second way : you can combine both the steps variable declaration and initialisation step and convert into single step.
Examples of this -
Types of variables -
In Java, there are three types of variables
- local variables.
- Instance variables.
- Static variables.
1. Local variables
local variables are the variables that are declared inside the body of any method like functions if else classes etc.
2. Instance variables
Are instance variables are defined without the static keyword. They are defined outside the body of the method. The object specific variables are also known as instance variables.
3. Static variables.
Static variables are initialised only once,at the starting of the program execution. This variable should be initialised first, before the initialisation of any instance variables.
Data types are classified the different values to be stored in the variables. In Java there are two types of data types, primitive data types and non primitive data types.
1. Primitive data types
Primitive data types are predefined data types in the JAVA. Primitive values do not share state with other primitive values.
There are 8 primitive types : byte,short,int,long,double and Boolean
Integer data types
byte (1 byte)
short (2 byte)
int (4 bytes)
long (8 bytes)

Floating data types
float (4 bytes)
double (8 bytes)
Textual Data type
char (2 bytes)
Logical Data type
boolean (1 byte) (true / false)
0 Comments