What are Primitive Data Types in java







Primitive Data Types >

There are 8 primitive data types in Java. All Primitive data types are keyword in java language. Let us now look into detail about the eight primitive data types.

Size
Minimum value
Maximum value
Default value
When to use
Example (Declaration of data type)
byte
8-bit
signed two's complement integer.
-128 (-27)
127 (27 -1)
0
The byte data type can be handy in saving memory in large arrays.
      
byte b = -128 ;
byte b1 = 127;

                      
         
short
16-bit
signed two's complement integer.
-32,768 (-215)
32,767
(215 -1)
0
The short data type can be handy in saving memory in large arrays.
short s = -32768;
short s1 =32767;
short s_ =32_767  ;

Note : Java 7 allows you to use _ between digits to improve readability of code.
int
32-bit
signed two's complement integer.
-2,147,483, 648
(-231)
2,147,483, 647
(231 -1)
0
int is used often for storing integer values (if memory is not a concern)
int i = -2147483648;
int i1 = 2147483647;
int i_ = 2_147_483_647;
         
long
64-bit
signed two's complement integer.
-9,223,372, 036,854, 775,808
(-263)
9,223,372, 036,854,  775,807  
(263 -1)
0L
long is used when a wider range than int is required.
long l = -9223372036854775808L;
long l1 = 9223372036854775807L;
long l_ = 9_223_372_036_854_775_807L;
float
single- precision
32-bit IEEE 754 floating point.


0.0f
The float data type can be handy in saving memory in large arrays.

Float data type is never used for precise values such as currency.
float f = -1.1f;
float f1 = 1.1f;
double
single- precision
64-bit IEEE 754 floating point.


0.0d
double is generally used as the data type for large decimal values.

Double data type should never be used for precise values such as currency.
double d = -2.3d;
double d1 = 2.3d;
boolean
1-bit


false
boolean is used when value can be either true or false
boolean boo=false;
char
single 16-bit Unicode character
'\u0000'  or
0
'\uffff' or 65,535

char is used to store character.
char c='a';

eEdit
Must read for you :