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

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

  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.

 
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

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

  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.

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

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

  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.

 
public class DemoChar {

    public static void main(String args[]) {
        char c;
        c = 'a';
        System.out.println("value of char data c = " + c);
    }
}

Result

value of chart data c =  a

Program to print character using ASCII value

 
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

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.

TypeDefaultSize
booleanfalse1 bit

Write a program to demonstrate boolean data types.

 
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

A
value of 10>9  = true