Data Types in Java

Profile picture for user arilio666

Datatypes specify what is stored inside the variable and what the value should be inside the variable and of different sorts of sizes. In the Java programming language, there are two types of data types.

  1. Primitive datatype: boolean, char, byte, integer, short, long, float and double
  2. Non-primitive datatype: classes, interfaces, and arrays

We will cover the primitive datatypes in this article and what is their purpose.

Primitive Datatypes Of Java

1. Boolean

The boolean datatype when declared tells the two possibilities of true or false. This one is used as a flag for checking conditions whether it is true or false.

Boolean value = true;

2. Byte

An 8-bit signed 2s complement integer byte datatype is another primitive data type that has the range that lies between -128 to 127 and the default value is 0. It has a limit up to -128 and can only exceed up to 127.

The byte data type is used up to save memory in an enormous array and space is consumed 4 times smaller than that of int. Preferably used in place of int too.

byte num1 = 30, num2 = -60;

3. Short

A 16-bit signed 2s complement integer short datatype value ranges between -32768 to 32767 which is the minimum and maximum up to it can reach and also its default value is zero and this datatype is 2 times smaller than an integer.

short a1 = 6000, b1 = -20000;

4. Int

A 32-bit signed 2s complement integer Int datatype value ranges from -2147483648 to 2147483647 and that is the minimum and maximum up to the point it could be used. It is the commonly used data type for integral values if there is no problem in the field of memory.

int num1 = 40000 , num2 = -8000;

5. Long

A 64-bit signed 2s complement integer Int datatype value ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and that is the minimum and maximum up to the point it could be used. Use it when there is a need to for long values which int cannot provide.

long num = 300000; num2 = -300000;

6. Float

A single-precision 32-bit IEEE 754 floating-point Float datatype value range is unlimited. A double replacement datatype can be used as an alternative. Should not be used for getting precise value such as the currency.

float f = 4.22f;

7. Double

A single-precision 64-bit IEEE 754 floating-point Float datatype value range is unlimited. Generally used up for decimal values like float. Should not be used for getting precise value such as the currency.

double doub = 22.544;

8. Char

A single 16-bit Unicode character which has value range from '\u0000'  to '\uffff'.Usually used to store characters.

char sex = "m";
Tags