Data Types in Java

This tutorial provides you with the details of various Java data types. It is an essential topic for you to know and develop real Java programs.

Learn to Use Data Types in Java

The tutorial has the following sections to help you learn quickly.

Data types in Java

Every programming language provisions a set of data types for the programs to pass data and tell its compiler/interpreter to process it accordingly.

Java too defines the following two major categories for data types:

  • Primitive
  • Non-primitive

Primitive types:

Primitive data types are consists of the following eight variations:

路 Integer (int):

This data type is used to hold integer data value. Size of an int is 4 bytes. And the range of values it can store is – 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive).

int a = 10;

路 byte:

This data type can also hold integer values. Size of a single byte is 1 byte. Therefore, the range of values it can store is -128 to 127 (inclusive).

byte b = 10;

路 short:

It holds integer values, and its size is 2 bytes. Range of values are -32,768 to 32,767 (inclusive).

short c = 120;

路 long:

The long data type is the biggest of all data types as it consumes memory of 8 bytes and holds integer type values. Range of long is -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive).

long d = 10000;

By default, integer literals will have an int data type. Literal is the value stored in the variable.

int a = 20; // 20 is the literal and its type is int
byte b = 20; // here also, 20 is the literal but its type is byte

We rely on the concept of typecasting (both internal and external) to convert the int literal to any other format.

路 float:

A float is a data type used to store real numbers in java. The size of float is 4 bytes, and the range of values it can allow is approximate 卤3.40282347E+38F up to 6-7 significant decimal digits as per the IEEE 754 standard.

float f= 23.4f;

路 double:

A double is another data type that allows real numbers with double precision. The size of double is 8 bytes, and it can store approximate 卤1.79769313486231570E+308 up to 15 significant decimal digits.

double d = 1.2;

By default, all decimal numbers end up as double literals. Therefore, we typecast the value for float by adding an addition “f” along with the value.

float f = 1.2f;

路 boolean:

This data type stores only boolean values. It means that either it can assume a true or false value.

boolean val = true;

路 char:

The char data type facilitates to store characters. Its size is 2 bytes, and the values map according to the Unicode characters. The range of values lie between ‘\u0000’ (or 0) to ‘\uffff’ .

Learn to Write Your First Java Program.

Non-primitive types:

Non-primitive are user-defined data types. Their purpose is not to store a value. Instead, they refer to a memory location. In programming terms, we call it as Heap, which further allocates space for the primitive data types (involve in the non-primitive operations).

For non-primitive data types, Java keeps the reference, additionally called location, not merely a value.

Alternatively, we refer聽them as reference data types. They provide a way to grant space to members of a class or interface. An array is the simplest example of a聽non-primitive data type.

Data type allocation in Java