CoreJavaTutorials
Contribute Us | Ask Question |
z

Data type in java



The data type of a variable refers to the type of data the variable can have or store.The operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.

The data type means to the type of data that can be stored into a variable.Java is known as a strongly typed language because when programmer declare a variable, it must specify the type of a variable. After that the compiler ensures that you or programmer don not try to assign the data of the wrong type to the variable.

See the following example It generates a compiler time error:

int a;
a= 2.14125;

Because a is declared as a variable of type int (int is use to hold integer value), and we assign float number to it.

The Correct declaration for above value should be like this :

float a;
a= 2.14125;

data type in java

Types of Data type in java

In Java , data type is divided into two types that is :
1 ) Primitive types and
2 ) Non-Primitive types.

Primitive Datatypes are the type of data that defined by the language itself or that is predefined in programming language.
On the other side, non- primitive or reference types are types defined by classes in the Java application programming libraries, interface or by classes and API that user create rather than by the language itself.

List of data type

Data Type Size of Data type(In Bytes) Default Value
boolean 1 false
char 2 '\u0000'
byte 1 0
short 2 0
int 4 0
long 8 0L
float 4 0.0f
double 8 0.0d