Primitive Data Types
In this section we will discuss about Primitive Data Types in Java 7.
Primitive Data Type :
Primitive data types is already defined and represented by the reserved keyword. Value of primitive type don't share state with another primitive values. Java 7 supports eight primitive data types -
- byte
- short
- int
- long
- float
- double
- boolean
- char
byte : It is an 8-bit signed two's complement integer that
is 1 byte.
Minimum value : -128
Maximum value : 127(inclusive)
Default value : 0
You can use it in place of int where you need to save memory space.
Example : byte value1=150, value2= -55;
short : It is 16-bit signed two's complement integer that is
2 byte.
Minimum value : -32,768
Maximum value : 32,767(inclusive)
Default value : 0
This is also used to store numeric value where you need to save memory space.
Example : short s1=3000, s2= -2000;
int : It is 32-bit signed two's complement integer that is 4
byte.
Minimum value : -2,147,483,648
Maximum value : 2,147,483,647 (inclusive)
Default value : 0
In general we use this data types for numeric values. It is large enough to use
for common purpose of our program.
Example : int num=20000;
long : It is a 64-bit signed two's complement integer that
is 8 byte.
Minimum value : -9,223,372,036,854,775,808
Maximum value : 9,223,372,036,854,775,807 (inclusive)
Default value : 0L
When we need large range of values then we use it in place of int.
Example: long l=222222L;
float : It is a single-precision 32-bit IEEE 754 floating
point. It takes 4 byte.
Default value : 0.0f
This is used where we want decimal representation and want to save memory space
Example : float value=111.9f;
double : It is double-precision 64-bit IEEE 754 floating
point. By default we use double for decimal values.
Default value is 0.0d.
Example : double value=232.5;
boolean : It is one bit information data type. This contains
only two values that are true and false. we use it for true/false conditions.
By default it is false.
Example : boolean test=true;
char : This is single 16-bit Unicode character.
Minimum value : '\u0000' (or 0)
Maximum value : '\uffff' (or 65,535 inclusive).
This is used to store any character.
Example : char ch='A';