Javaexercise.com

Java Data Types

Data type specifies type of data stored by a variable. In other words, data type tells to the compiler what type of data a variable can store during program execution.

To deal with different types of data, Java provides 8 types of predifined data types that are also known as primitive data types. Primitive data values are not objects and each primitive type has certain range to store values.

  1. char
  2. boolean
  3. byte
  4. short
  5. int
  6. long
  7. float
  8. double

We can broadly catageries these into integral type (char, byte, short, int, long), floating-point type (float, double) and boolean type.


Java char Type

Char data type is used to store a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

Data Type Size (bits) Minimum Value Maximum Value
char 16 \u0000 (or 0) \uffff (or 65,535 inclusive)

Java char Example

// Java char data type example
public class Demo {
    public static void main(String[] args){
        char a = 'j';
        char b = 65;   // We can also use ASCII value to represent character
        System.out.println("char value of a: "+ a);
        System.out.println("char value of ASCII 65: "+ b);
    }
}

Output:

char value of a: j
char value of ASCII 65: A


Java boolean Type

boolean data type is used to store a logical value either True or False. It used to set simple flag for the conditional programming context.

Data Type Size (bits) True value False value
boolean not defined true false (default)

Java boolean Example

// Java boolean data type example
public class Demo {
    public static void main(String[] args){
        boolean a = true;
        System.out.println(a);
        a = false;
        System.out.println(a);
    }
}

Output:

true
false


Java byte Type

byte data type is used to store numeric value of limited range. It is an 8-bit signed two's complement integer and can be helpful to save memory. Please refer the below table for its minimum, maximum value and size as well.

Data Type Size (bits) Minimum Value Maximum Value
byte 8 -27 (-128) 27-1 (+127)

Java byte Example

// Java byte data type example
public class Demo {
    public static void main(String[] args){
        byte a = -12;
        System.out.println(a);
        a = 12;
        System.out.println(a);
    }
}

Output:

-12
12


Java short Type

short data type is used to store numeric value of limited range but larger than byte size. It is an 16-bit signed two's complement integer and can be helpful to save memory. Its size is twice the size of byte. Please refer the below table for its minimum, maximum value and size as well.

Data Type Size (bits) Minimum Value Maximum Value
short 16 -215 (-32768) 215-1 (+32767)

Java short Example

// Java short data type example
public class Demo {
    public static void main(String[] args){
        short a = -125;
        System.out.println(a);
        a = 125;
        System.out.println(a);
    }
}

Output:

-125
125


Java int Type

int data type is used to store numeric value and can store larger than byte and short. It is a 32-bit signed two's complement integer. Its size is twice the size of short data type. Please refer the below table for its minimum, maximum value and size as well.

Data Type Size (bits) Minimum Value Maximum Value
int 32 -231 231-1

Note: In Java SE 8 and later versions, we can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1.

Java int Example

// Java int data type example
public class Demo {
    public static void main(String[] args){
        int a = -150;
        System.out.println(a);
        a = 150;
        System.out.println(a);
    }
}

Output:

-150
150


Java long Type

long data type is used to store numeric value and can store larger than byte, short and int data types. It is a 64-bit signed two's complement integer. Its size is twice the size of int data type. Please refer the below table for its minimum, maximum value and size as well.

Data Type Size (bits) Minimum Value Maximum Value
long 64 -263 263-1

Note: In Java SE 8 and later versions, we can use long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1.

Java long Example

// Java long data type example
public class Demo {
    public static void main(String[] args){
        long a = -1200;
        System.out.println(a);
        a = 1200;
        System.out.println(a);
    }
}

Output:

-1200
1200


Java float Type

float data type is used to store floating-point value. It is a single-precision 32-bit IEEE 754 floating point. We can use it to store short range of floating-point values to save memory. Please refer the below table for its minimum, maximum value and size as well.

Data Type Size (bits) Minimum +ve Value Maximum +ve Value
float 32 1.401e-45f 3.402e+38f

Java float Example

// Java float data type example
public class Demo {
    public static void main(String[] args){
        float a = -120.50f; // By default, floating point numbers are treated as double in Java. Use suffix 'f' with value to make it float.
        System.out.println(a);
        a = 120.50f;
        System.out.println(a);
    }
}

Output:

-120.50
120.50


Java double Type

double data type is used to store floating-point value. It is a double-precision 64-bit IEEE 754 floating point. We can use it to store large range of floating-point values which are out of the float range. Please refer the below table for its minimum, maximum value and size as well.

Data Type Size (bits) Minimum +ve Value Maximum +ve Value
double 64 4.94e-324 1.79e+308

Java double Example

// Java double data type example
public class Demo {
    public static void main(String[] args){
        double a = -120.50;
        System.out.println(a);
        a = 120.50;
        System.out.println(a);
    }
}

Output:

-120.50
120.50

Java Data Types Default Values and Corresponding Wrapper Classes

All the data types we discussed here are primitive type that means these are not object. But Java provides wrapper classe for each primitive type to convert into object. Here, in the below table we have mentioned default value of each primitive type and corresponding wrapper class.

Data Type Default Value Wrapper Class
byte 0 Byte
short 0 Short
char '\u0000' Character
int 0 Integer
long 0L Long
float 0.0f Float
double 0.0d Double
boolean false Boolean