Data Types in Java

All variables in the java must have a data type. A variable's type determines the values that the variable store and the operations that can be performed on it.

The Java programming language is strictly-typed, which means that all variables must first be declared before they can be used. Data types can be categorized as two –

  1. Primitive data types
  2. Non - primitive data types

Data Types in Java | Primitive and Non Primitive Data Types

Primitive Data Types in Java

A primitive type is predefined by the language and is named by a reserved keyword. There are eight primitive data types supported by Java:

There are 8 types of primitive data types:

  • boolean data type
  • byte data type
  • char data type
  • short data type
  • int data type
  • long data type
  • float data type
  • double data type

Byte Data Type

The byte data type is an 8 bit signed integer. It ranges from -128 to 127. It can be declared as

byte b=5;

Short Data Type

The short data type is a 16 bit signed integer. It ranges from -32,768 to 32,767. It can be declared as

short s = 2;

Int Data Type

The int data type is used to store the integer values, not the fraction values. It is a 32 bit signed integer data type. It ranges from -2,147,483,648 to 2,147,483,647 that is more enough to store large number in the program. It can be declared as

  int num = 50;

Long Data Type

The long data type is a 64 bi signed integer. It ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. It can be declared as

    long ln = 759;

Float Data Type

The float data type is a 64 bit IEEE 754 floating point. It ranges from 1.40129846432481707e-45 to 3.40282346632528860e+38 (positive or negative). Use a float (instead of double) to save memory in large arrays. It can be declared as

float f=123.45;
float f1 = -1000.44;

Double Data Type

This data type is a double precision 64 bit IEEE 754 floating point. It ranges from 4.94065645841246544e-324d to 1.7976933486231570e+308d (positive or negative). This data type is normally a default choice for floating values. It can be declared as

double d = 1254.77

Char Data Type

The char data type is a single 16 bit signed Unicode character. It ranges from 0 to 65,535. It can be declared as

char ch = ‘k’;

Boolean Data Type

The Boolean data type represents only two values: true and false and occupy 1 ut in the memory. It can be declared as

 boolean result = true;

Here is the list of primitive data types with their details

Type Contains Default Size Range
boolean true or false false 1 bit True/False, Yes/No, 0/1
char Unicode character \u0000 16 bit 0 to 65,536
byte Signed integer 0 8 bits -128 to 127
short Signed integer 0 16 bits -32786 to 32767
int Signed integer 0 32 bits -2147483648 to 2147483647
long Signed integer 0 64 bits -9223372036854775808 to 9223372036854775807
float IEEE 754 floating point 0.0 32 bits 1.4e – 045 to 3.4e + 038
double IEEE 754 floating point 0.0 64 bits 4.9e - 324 to 1.8e + 308

Data type char takes 2 bytes to store a character because java uses the Unicode system rather than the ASCII code system \u0000 is the lowest range of Unicode system.

Java provides special support for character strings using java.lang.String class. Enclosing character string within double quotes (“”) will automatically create String objects.

It can be declared as –

String str = “My Name Is JAVA”.

Non-Primitive Data Types in Java

The non-primitive data types include Classes, Interfaces, and Arrays.