Skip to content

Java Datatypes#

Java Datatypes#

  • Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java: Primitive and Reference types.

  • Primitive types include:

    • Numeric types: byte, short, int, long, float, double.
    • Character type: char.
    • Boolean type: boolean.
  • Reference types include:

    • Classes
    • Interfaces
    • Arrays
    • Enumerations

Java Primitive Data Types#

  • As listed above, we have 8 primitives defined in Java are intbyteshortlongfloatdoubleboolean and char. These aren't considered objects and represent raw values. They're stored directly on the stack. Below is the table that contain default values and example for each types.
Type Size (bits) Minimum Maximum Example
byte 8 -2^7 2^7– 1 byte b = 100;
short 16 -2^15 2^15– 1 short s = 30000;
int 32 -2^31 2^31– 1 int i = 100000000;
long 64 -2^63 2^63– 1 long l = 100000000000000;
float 32 -2^-149 (2-2-23)·2^127 float f = 1.456f;
double 64 -2^-1074 (2-2-52)·2^1023 double f = 1.456789012345678;
char 16 0 2^16– 1 char c = ‘c';
boolean 1 boolean b = true;

Byte Data Type#

  • The byte data type is an example of primitive data type. It is an 8-bit signed two's complement integer. Its value-range lies between -128 to 127 (inclusive). Its minimum value is -128 and maximum value is 127. Its default value is 0.

  • The byte data type is used to save memory in large arrays where the memory savings is most required. It saves space because a byte is 4 times smaller than an integer. It can also be used in place of "int" data type.

1
byte b = 100;

Short Data Type#

  • The short data type is a 16-bit signed two's complement integer. Its value-range lies between -32,768 to 32,767 (inclusive). Its minimum value is -32,768 and maximum value is 32,767. Its default value is 0.

  • The short data type can also be used to save memory just like byte data type. A short data type is 2 times smaller than an integer.

1
short s = 30000;

Int Data Type#

  • The int data type is a 32-bit signed two's complement integer. Its value-range lies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is - 2,147,483,648 and maximum value is 2,147,483,647. Its default value is 0.

  • The int data type is generally used as a default data type for integral values unless if there is no problem about memory.

1
int i = 100000000;

Long Data Type#

  • The long data type is a 64-bit two's complement integer. Its value-range lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive). Its minimum value is - 9,223,372,036,854,775,808 and maximum value is 9,223,372,036,854,775,807. Its default value is 0. The long data type is used when you need a range of values more than those provided by int.
1
long l = 100000000000000;

Float Data Type#

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.

1
float f = 1.456f;

Double Data Type#

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.

1
double f = 1.456789012345678;

StringBuffer And StringBuilder#

StringBuffer StringBuilder
StringBuffer (synchronized) and thread-safe. It means there are no two threads can access to a method of StringBuffer class at the same time StringBuilder (non-synchronized) and no thread-safe. It means there are two threads can access to a method of StringBuilder class at the same time
StringBuffer is low StringBuilder is fast

See Also#