Variable And Data Type In Java
Variable
Variable is a reserved memory location to hold a value. In other words, when we want to store any value and give it a name, that value takes a reserved space in memory and it’s called as a variable.
Variable has any value, which is located in the memory.
Let’s see, how we can declare the variable.
data type variable;
For example
- int x ; (int is a data type and x is a variable name).
- int y;
- int x , y;
- int x = 4 , y = 5;
- byte p = 65;
- double pi = 3.1415;
- char q = ‘s’;
Types of variable are,
- Local Variable
A variable, which is declared inside any methods, constructors and blocks is known as a local variable. Its scope is limited because it is used locally in the method. It is created after the creation of the method, constructor, block, local variable is not visible to class and not accessed by the class.
For example
In this example, first we create a class and a method. Afterwards, declare two local variables, inside the method.
Code
- public class LocalVariableExm {
- void message() { //it is a method.
- int p; // declaration of Local Variable.
- int x = 10; // declaration and initialization of Local Variable.
- }
- }
- Static Variable
Static variable is very important type of variable. We use static keyword with the variable. Hence, it’s called as a static variable. Static Variable is declared inside the class and outside the method, constructor and a block. Static variable belongs to the class not an Object and it is stored in static memory. Its default value is 0 and we can call the static variable with its class name. Hence, there is no need to create an object in static variable.
For example
In this example, first we create a class and three variables outside the method, where two variables are static variables.
Code
- public class StaticVariableExm {
- int p;
- static int q = 10; // Static Variable with initialization
- static int r; // Static Variable declaration
- void message() {}
- }
- Instance Variable
Instance variable is declared inside the class but outside the method, constructor or block. It has widest scope because it is globally visible to the whole class. It is created at the time of an object creation. When instance variable is created that time, it takes space in the heap memory and it is called through object and default value is 0.
For example
In this example, first we create a class and two instance variables outside the method.
Code
- public class InstanceVariableExm {
- int a; // Declaration of instance variable.
- int b = 10; // Declaration and initialization of instance variable.
- void message() {
- }
- }
Data type
With the help of data type, we can store value in the integers, decimals and characters.
There are two types of data types, which are,
- Primitive data type
Java supports eight primitive data type, which are predefined by Java language and named as a keyword.
Data Type Default Size Default Value Byte 1 byte 0 Short 2 byte 0 Int 4 byte 0 Long 8 byte 0L Float 4 byte 0.0f Double 8 byte 0.0d Char 2 byte ‘\u0000’ Boolean 1 bit false
Maximum value and Minimum value
- Byte
Minimum value is -128 (-2^7) and Maximum value is 127 (inclusive)(2^7 -1) - Short
Minimum value is -32,768 (-2^15) and Maximum value is 32,767 (inclusive) (2^15 -1) - Int
Minimum value is -2,147,483,648.(-2^31) and Maximum value is 2,147,483,647(inclusive).(2^31 -1) - Long
Minimum value is -9,223,372,036,854,775,808.(-2^63) and Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1) - Char
Minimum value is '\u0000' (or 0) and Maximum value is '\uffff' (or 65,535 inclusive).
- Non primitive Data Type
Non Primitive data types also known as a Reference data types. Reference variable is used to refer to any object of the declared type and by default, its value is null. Class objects string and various type of array variables are reference data types.
Summary
Thus, we learnt that variable is a reserved memory location to hold a value and data type is mainly used to store value in the integers, decimals, characters and also learnt how to use it in Java.