Primitive data types are primary and basic data types in java.
In Java there are 8 primitive data types.
List of primitive datatypes in java is as follows
- byte
- short
- int
- long
- float
- double
- char
- boolean
Details of all data types are below.
1 Java Integer Data Type
There are four integer data types (byte, short, int and long) in java that can hold integer values.
All four data types with default value size and minimum and maximum value range that can store are shown below
Type | Default | Size | Min Max Value |
---|---|---|---|
byte | 0 | 8 bits | -27, 27 -1 |
short | 0 | 16 bits | -215, 215 -1 |
int | 0 | 32 bits | -231, 231 -1 |
long | 0 | 64 bits | -263, 263 -1 |
A byte data type in Java
- Byte stores 8 bits signed integer value.
- byte stores data in range of -128 to 127.
- Default value of byte is 0.
- Byte data type can use where we have to save memory during data storage.
B short Data type in java
- short stores 16 bits signed integer value.
- short stores data in range of -32768 to 32767.
- Default value of short is 0.
- Short data type can use where we have to save memory during data storage.
C int Data type in java
- int stores 32 bits signed and unsigned integer value.
- int stores data in range of -2^31 to -2^31-1 for signed integer and 0 to 2^32-1 for unsigned integer.
- Default value of short is 0.
- Int data type can used to represent normal integer values.
D long Data type in java
- long stores 64 bits signed and unsigned long integer value.
- long stores data in range of -2^63 to -2^63-1 for signed integer and 0 to 2^64-1 for unsigned integer.
- Default value of short is 0.
- long data type can used to represent higher integer values.
Lets see a simple program to initialize and print integer data type in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class DemoInteger { public static void main(String args[]) { byte b; short x, y; int i, j, k; long l; b = 120; x = 200; y = 300; i = 20; j = 500; k = i * j; l = 30000; System.out.println("value of byte b = " + b); System.out.println("value of short x and y = " + x + " " + y); System.out.println("value of int i, j and k = " + i + " " + j + " " + k); System.out.println("value of long l = " + l); } } |
Output
1 2 3 4 | value of byte b = 120 value of short x and y = 200 300 value of int i, j and k = 20 500 10000 value of long l = 30000 |
2 Float and Double Data Types
Floating point value (example 11.23, 333.3330 etc. ) can be stored in data type float and double.
Small floating value can be stored in float and large floating stored in double.
java data types float and double contains real values.
Type | Default | Size |
---|---|---|
float | 0.0 | 32 bits |
double | 0.0 | 64 bits |
A float data type in java
- float stores decimal values in 32 bits, represents data in single-precision IEEE754 floating point.
- floats stores fractional values in range of 3.4e-038 to 3.4e+038.
- Default value of float is 0.0.
- float data type can used to represent fractional values.
B double data type in java
- double stores decimal values in 64 bits, represents data in double-precision IEEE754 floating point.
- double stores fractional values in range of 1.7e-308 to 1.7e+308.
- Default value of double is 0.0.
- double data type can used to represent fractional values with more precision.
Write a program to demonstrate use of float data types.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <pre brush="java"> public class DemoFloat { public static void main(String args[]) { float f; double x, y; f = 11.23f; x = 200.2222; y = 100.1111; double z = x - y; System.out.println("value of float data f = " + f); System.out.println("value of double data x= " + x); System.out.println("value of double data y= " + y); System.out.println("value of double data z= " + z); } } |
Output
1 2 3 4 | value of float data f = 11.23 value of double data x= 200.2222 value of double data y= 100.1111 value of double data z= 100.1111 |
3 Java Character Data Type
Character data ( example ‘a’ , ‘b’ , ‘f’, ….. etc. ) is stored in “char” data type.
Type | Default | Size |
---|---|---|
char | \u0000 | 16 bits |
B char data type in java
- char stores single 16 bit unicode character
- double stores fractional values in range of 1.7e-308 to 1.7e+308.
- Default value of double is 0.0.
- double data type can used to represent fractional values with more precision.
Write a program to demonstrate char data types.
1 2 3 4 5 6 7 8 | public class DemoChar { public static void main(String args[]) { char c; c = 'a'; System.out.println("value of char data c = " + c); } } |
Result
1 | value of chart data c = a |
Program to print character using ASCII value
1 2 3 4 5 6 7 8 9 | public class DemoChar1 { public static void main(String args[]) { char c, d; c = 'a'; d = 65; // ASCII value of A is 65 System.out.println("value of char data c and d = " + c + " " + d); } } |
Result
1 | value of chart data c and d =a A |
4 Java Boolean Data Type
boolean data type in java store the true and false value.
By default value of boolean data type is false.
Type | Default | Size |
---|---|---|
boolean | false | 1 bit |
Write a program to demonstrate boolean data types.
1 2 3 4 5 6 7 8 9 10 11 12 | public class DemoBoolean { public static void main(String args[]) { Boolean b = true; if (b) { System.out.println("A"); } else { System.out.println("B"); } System.out.println("value of 10>9 = " + (10 > 9)); } } |
Result
1 2 | A value of 10>9 = true |