i

JAVA Complete Course

Data Types

Data types in Java can be divided in small groups, such as: primitives, non-primitives, floating types.

In java additionally, every primitive data type has wrapper class, which can be said that is non-primitive as its default value is null instead of default value of primitive. Below is the hierarchy of data types in java:

As we have already mentioned, each type has its default value and default size. Let’s take a look on it

Now let’s define each primitive type separately.

boolean Data Type is the one who can hold only two possible values: true or false. Usually it is used as a flag or in mostly in conditional statements. It only holds one bit of information and default value of primitive boolean is false. The wrapper class for primitive boolean is Boolean and as mentioned earlier, it has default value null.

byte Data Type is an example of primitive data type which is 8-bit signed integer. Minimum value byte primitive can has is -128 and maximum value is 127, so that the value varies between -128 and 127 (inclusive). Default value of byte primitive is 0, and it has wrapper class Byte which has default value null. Byte primitive is 4 times smaller than integer, so it is usually used to save memory.

short Data Type is 16-bit signed integer which can have value between -32,768 to 32,767 (inclusive). Default value of short primitive is 0 and wrapper is Short class. Short primitive data type is 2 times smaller than integer.

int Data Type is  32-bit signed and can have value between 2,147,483,648 (-2^31) and 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is - 2,147,483,648and maximum value is 2,147,483,647. Its default value is 0. It has wrapper class Integer and like any other wrappers, default value of it is null.

long data type is two times bigger in size than int. Its value-range lies between  

-9,223,372,036,854,775,808(-2^63) and 9,223,372,036,854,775,807(2^63 -1)(inclusive). Its minimum value is - 9,223,372,036,854,775,808and maximum value is 9,223,372,036,854,775,807. Its default value is 0. It has wrapper class Long and like any other wrappers, default value of it is null. To declare long primitive data type we need to differentiate it from int, so that we are adding L at the end, for example long value = 12L;

The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is unlimited. The double data type is generally used for decimal values just like float. The double data type also should never be used for precise values, such as currency. Its default value is 0.0d. It has wrapper class Double.

The float data type is a single-precision 32-bit IEEE 754 floating point. Its value range is unlimited. It is recommended to use a float (instead of double) if you need to save memory in large arrays of floating point numbers. The float data type should never be used for precise values, such as currency. Its default value is 0.0F. It has wrapper class Float. To differentiate float primitive from double, we need to add “f” letter at the end of declaration, for example: float value = 11.59f;

The char data type is a single 16-bit Unicode character. Its value-range lies between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive).The char data type is used to store characters. char primitive data type has a wrapper class Character.