To understand the concept of data types in a better way, let us take a real life example. Suppose you have to make reservations, by train, for planning a holiday trip during your vacations. Firstly, you will have to fill a form to make the reservations. You need to provide your details to the railway department, such as your name, age, gender, date of journey, place of visit etc. This information will help the department to book your reservations. Now, all of the data you provide is of a particular type. Say, your name is a string of characters; your age is numeric and so on. Similarly in Java, we need to inform the compiler about the type of data as variables or constants that we will be storing in a particular variable(s).  Java data types  does this for us.

Java Data types are broadly classified into two categories:

  1. Primitive data types
    1. Integer data type
      • byte
      • short
      • int
      • long
    2. Real data type
      • float
      • double
    3. Character data type
    4. Boolean data type
  2. Reference / Object data types

Primitive Java Data Types

Primitive data types are pre-defined in Java and are commonly referred to as simple types. These data types are efficient to use as they are built-in parts of the Java language. There are eight data types supported by Java which are grouped into four groups mentioned earlier. Now, let’s have a look at each data type.

byte

  • Smallest integer type.
  • This is an 8-bit (1 byte) signed two’s complement integer.
  • Its range is from -128 to 127.
  • Default value is 0.
  • Declared using the byte
  • For example: byte b, c;

short

  • short data type is a 16-bit (2 bytes) signed two’s complement integer.
  • Its range is from -32,768 to 32,767.
  • Default value is 0.
  • Declared using the short
  • For example: short s;

int

  • This is the most commonly used data type.
  • int is a 32-bit (4 bytes) signed two’s complement integer.
  • Its range is from -2 billion (i.e. -2^31) to 2 billion (i.e. 2^31 – 1).
  • Default value is 0.
  • Declared using the int
  • For example: int age;

long

  • long is a 64-bit (8 bytes) signed two’s complement integer.
  • Its range is from -10E+18 (i.e. -2^63) to +10E+18 (i.e. 2^63 – 1).
  • Default value is 0L.
  • Declared using the long
  • For example: long distance;

float

  • float is a single precision (7 decimal places) 32-bit floating point number.
  • Its range is from 1.4e-045 to 3.4e+038.
  • Default value is 0.0f.
  • Declared using the float
  • For example: float pi;

double

  • double is a double precision (15 decimal places) 64-bit floating point number.
  • Its range is from 4.9e-324 to 1.8e+308.
  • Default value is 0.0d.
  • Declared using the double
  • For example: double area;

boolean

  • Boolean represents one bit of information for logical values.
  • It can have only one of the two values, true or false.
  • Default value is false.
  • Declared using the boolean
  • For example: boolean b;

char

  • char is a single 16-bit Unicode character.
  • It is used to store any characters.
  • Its range is from 0 (or ‘\u0000’) to 65,536 (or ‘\uffff’). There are no negative characters.
  • Declared using the char
  • For example: char letterb=’B’;

Reference Java Data Types

These are also known as composite data types. They are created through primitive data types. Some examples are: classes and arrays.