Data Types in Java

In java programming language for any variable before using it, needs to declare the type of the variable.Once the variable is declared we can't change the type of the variable with in its scope. Every variable has its own scope will understand the scope of the variable in coming chapters for now will concentrate on the data types.

Data Types in Java are of two types

  • Primitive data types.
  • Non-primitive data types.

primitive data types in Java

There are 8 primitive data types are present Java, every primitive data type has its own limitations and features. Limitations can be like its size cannot be increased or decreased by the programmer and whose size will, be fixed.

primitive data types doesn't have any methods associated with it unlike the Non-primitive data types. In the primitive data types, if we not assigned any value to the respective variable then default value will be assigned to it based on its primitive data type declaration. The following are the primitive data types, their size and default values.

# Data Type Size Range Default Value
1 byte 1 byte store numbers from -128 to 127 0
2 short 2 bytes stores numbers from -32,768 to 32,767 0
3 int 4 bytes stores numbers from -2,147,483,648 to 2,147,483,647 0
4 long 8 bytes stores numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0L
5 float 4 bytes stores decimal numbers,stores up to 7 decimal digits 0.0f
6 double 8 bytes stores decimal numbers,stores up to 15 decimal digits 0.0d
7 char 2 bytes Stores single character '\u0000'
8 boolean 1 bit stores either true or false false

Non-primitive data types

Excluding the primitive data types the remaining all are the Non-primitive data types(excluding String). Non-primitive data types can be declared by the programmer as per their requirements.

There is no limitation as such in the Non-primitive data types about its size or length or default values.

The following are the major differences from the primitive data types to Non-primitive data types

# Primitive data types Non-primitive data types
1 Size constraint No Size constraint
2 No methods associated Methods will be associated
3 default values are present No default values are present