Primitive Data Types in Java

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

  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. char
  8. 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

TypeDefaultSizeMin Max Value
byte08 bits-27, 27 -1
short016 bits-215, 215 -1
int032 bits-231, 231 -1
long064 bits-263, 263 -1

A byte data type in Java

  1. Byte stores 8 bits signed integer value.
  2. byte stores data in range of -128 to 127.
  3. Default value of byte is 0.
  4. Byte data type can use where we have to save memory during data storage.

B short Data type in java

  1. short stores 16 bits signed integer value.
  2. short stores data in range of -32768 to 32767.
  3. Default value of short is 0.
  4. Short data type can use where we have to save memory during data storage.

C int Data type in java

  1. int stores 32 bits signed and unsigned integer value.
  2. int stores data in range of -2^31 to -2^31-1 for signed integer and 0 to 2^32-1 for unsigned integer.
  3. Default value of short is 0.
  4. Int data type can used to represent normal integer values.

D long Data type in java

  1. long stores 64 bits signed and unsigned long integer value.
  2. long stores data in range of -2^63 to -2^63-1 for signed integer and 0 to 2^64-1 for unsigned integer.
  3. Default value of short is 0.
  4. long data type can used to represent higher integer values.

Lets see a simple program to initialize and print integer data type in java.

Output

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.

TypeDefaultSize
float0.032 bits
double0.064 bits

A float data type in java

  1. float stores decimal values in 32 bits, represents data in single-precision IEEE754 floating point.
  2. floats stores fractional values in range of 3.4e-038 to 3.4e+038.
  3. Default value of float is 0.0.
  4. float data type can used to represent fractional values.

B double data type in java

  1. double stores decimal values in 64 bits, represents data in double-precision IEEE754 floating point.
  2. double stores fractional values in range of 1.7e-308 to 1.7e+308.
  3. Default value of double is 0.0.
  4. double data type can used to represent fractional values with more precision.

Write a program to demonstrate use of float data types.

Output

3 Java Character Data Type

Character data ( example ‘a’ , ‘b’ , ‘f’, ….. etc. ) is stored in “char” data type.

TypeDefaultSize
char\u000016 bits

B char data type in java

  1. char stores single 16 bit unicode character
  2. double stores fractional values in range of 1.7e-308 to 1.7e+308.
  3. Default value of double is 0.0.
  4. double data type can used to represent fractional values with more precision.

Write a program to demonstrate char data types.

Result

Program to print character using ASCII value

Result

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.

TypeDefaultSize
booleanfalse1 bit

Write a program to demonstrate boolean data types.

Result