JAVA Variable type & Datatype
Summary of this Tutorial
This tutorial explains what is Variable type and Data type with their examples. It also explains different types of Data type and variable type used to write a program in JAVA programming language.
What is Variable in Java?
Variables types in Java are memory location which is used for storing any value. Therefore for storing any data value in JAVA, a variable must be created. As soon as the variable gets created the memory will be allocated to the data value and the java program will be executed. The size of the memory allocated to a data depends on it datatype for which variable is created. Therefore we must have to mention the datatype before creating the variable.
Types of Variable in Java
Java has three kinds of variables in JAVA:
- Local Variables
- Instance Variables
- Static Variables
Local Variables
- Local variables in Java are declared in constructor, method, or blocks.
- This variable is only visible in the blocks or method in which they are declared they can't be used outside the methods or block in which they are declared.
- Local variables are created when the method, constructor or block is entered and gets once it exits the method, constructor, or block
Instance Variables
- Instance variables in Java are declared outside a method, constructor or block.
- They are declared in a class and are available for an entire class method, constructor and blocks.
- Instance variables are created when an object is created and destroyed when the object is destroyed.
Static Variables
- Static variables in Java are also declared in the class outside the method, constructor or any block like instance variable but unlike instance variables static variable can only be declared with a static keyword.
- Static variables are created when the program starts and destroyed when the programs end
What is Data Type in Java?
Data types in Java are the type of data for which a memory will be allocated in the variable and for which memory is allocated in variables
Types of Datatypes in Java
Data types in Java can be categorised mainly in two forms:
- Primitive Data Types
- Non-Primitive or Reference Data Types
Primitive Data Types in Java
The data types in Java in which are pre-defined in java are called primitive data types. Primitive data types are not user-defined data types because it is not defined by the user.
Primitive data types in Java can be further categorised into eight types:
- Byte datatype
- Short data type
- Int datatype
- Long datatype
- Float datatype
- Double datatype
- Boolean datatype
- Char datatype
Data Type | Function | Range |
byte | Byte datatype in Java is used to save space in large arrays, It is used in placean of integer as it is four-time smaller than integer | +127 to -128 |
short | Short data type in Java is also used to save memory as byte datatype. The short data type is 2 times smaller than an int. | +32,767 to -32,768 |
int | Int datatype is used as a default data type for integer values. | +2,147,483,647 to -2,147,483,648. (-2^31) |
long | Long datatype in Java is used when a wider range than int datatype is needed and Default value is OL. | +9,223,372,036,854,775,807 to -9,223,372,036,854,775,808 |
float | Float datatype is used for saving memory in large arrays of floating point numbers. | 3.402,823,5 E+38 to 1.4 E-45 |
double | Double datatype is used as a default data type for decimal values | 1.797,693,134,862,315,7 E+308 to 4.9 E-32 13.04, -145.5427, 0,0 |
boolean | Boolean datatype is used for simple flags tracks true/false condition | true, false |
char | Char datatype is used for storing any character | All Unicode characters such as 'a', 's', '%', '9' |
Non-Primitive or Reference Data Types
Non-Primitive datatypes in Java are not pre-defined datatypes. It is a datatype which has to be created by a programmer as needed. It is also called as Reference or Object data type.
There are three types of Non-Primitive Datatype
- Array
- Classes
- Interface