Data Types In Java

Data type in java specifies the size and type of values that can be stored in an identifier. They are helpful in representing how much memory is required to hold the data. It also represents what type of data to be allowed.

Java data types are classified into two types

  • Primitive data types
  • User-defined data types

Primitive Data types

The primitive data types in java include boolean, char, byte, short, int, long, float, and double. In Java programming language, primitive data types are the building blocks of data manipulation. These are the mostly 8 basic data types available in Java which are as follows

  • Int
  • Byte
  • Short
  • Long
  • Float
  • Double
  • Char
  • Boolean

Data Types In Java Image
Data Types In Java

Int Datatype

The int data type in java is a primitive data type and is 32-bit signed two’s complement integer. The int value-range lies in between - 2,147,483,648 to 2,147,483,647. Its minimum value is - 2,147,483,648 and the maximum value is 2,147,483,647. Int default value is 0. The int data type in the java programming language is generally used as a default data type for integral values unless there is no problem regarding memory.

Example

int a = 200;
int b = -200;

Byte Datatype

The byte data type is another example of a primitive data type. It is an 8-bit signed two’s complement integer. Its value-range lies between -128 to 127 (inclusive). Byte’s minimum value is -128 and its maximum value is 127. Same as int data type its default value is also 0. The byte data type is used to save memory in large arrays where memory savings is very important. Byte saves space because a byte is 4 times smaller than an integer. It can also be used in place of the int primitive data type.

Example

byte a = 20;
byte b = -20;

Short Datatype

The short primitive data type is a 16-bit signed two’s complement integer. Short value-range lies in between -32,768 to 32,767. Its minimum value is -32,768 and the maximum value is 32,767. Like above mentioned primitive datatypes short default value is also 0. The short data type can also be used to save memory just like the byte data type. A short data type is 2 times smaller than an integer.

Example

short a = 2;
short b = -2;

Long Datatype

The long primitive data type is a 64-bit two’s complement integer. Long value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Its minimum value is - 9,223,372,036,854,775,808 and the maximum value is 9,223,372,036,854,775,807. Long default value is also 0. The long data type is used when we need a range of values more than that are provided by int data type.

Example

long a = 20000L;
long b = -20000L;

Float Datatype

The float primitive data type is a single-precision 32-bit IEEE 754 floating-point. The float value range is unlimited. It is always recommended to use a float (instead of double) if you need to save memory in large arrays of floating-point numbers. The float primitive data type should never be used for precise values, such as currency. Unlike other data types, the default value of float is 0.0F.

Example

float f1 = 20.3f;

Double Datatype

The double primitive data type is a double-precision 64-bit IEEE 754 floating-point. Like float data type, the double value range is also unlimited. The double data type is generally used for decimal values like float. The double data type also should never be used for precise values as float data type, such as currency. Its default value of double is 0.0d.

Example

double d1 = 12.9;

Char Datatype

The char primitive data type is a single 16-bit Unicode character. Char value-range lies in between ‘\u0000’ (0) to ‘\uffff’ (65,535). The char primitive data type is used to store characters.

Example

char letterB = "B";

Boolean Datatype

The Boolean primitive data type is used to store only two possible values: true and false. The boolean data type is used for simple flags that track true or false conditions. The Boolean data type specifies one bit of information, but its size can’t be defined precisely.

Example

boolean one = true;

Non-Primitive Datatypes

Non-primitive data types are also called reference datatypes because they refer to the objects. The main difference between primitive and non-primitive data types is primitive data types are pre-defined and non-primitive data types are defined by the programmer and are not defined in the java language.

Non-Primitive data types are stated below

  • Interface
  • Enum
  • Class
  • String
  • Abstract

Subscribe For More Content