Java

Basic Datatypes in Java

A data type in a programming language is an attribute that instructs the computer on how to interpret the value given to the data. Datatypes can be classified into various categories on the basis of the value they store. Datatypes in Java are generally categorized into the following types.

  1. Primitive
  2. non-Primitive

1. Primitive Data Types

Data types that a programming language pre-defines are called primitive data types. These data types can store a specific type and size of variable.

Basic primitive data types available in java are:

boolean

This data type stores values which are either true or false and is used to track true or false conditions for simple flags.

Example

boolean flag;

flag=true;
Default Value Size
false 1 byte

byte

When it is required to save memory in arrays, we a byte data type that is capable of doing so because it is smaller than an integer (4 times) and can be used in place of the int data type.

Value Range

-128 to 127

Example

byte age;

age=20;
Default Value Size
0 1 byte

char

A 16-bit data type that is used to store characters is called char data type.

Value Range

‘\u0000’ to ‘\uffff’ or 0 to 65,535.

Example

char a;

a= 'x';
Default Value Size
‘\u0000 2 byte

short

Another 16-bit data type that is used to save memory is the short data type.

Value Range

-32,768 to 32,767.

Example

short value=10000;
Default Value Size
0 2 byte

int

Int data type is a basic 32-bit data type that is used for integral values by default.

Value Range

-2,147,483,648 to -2,147,483,647

Example

int number;

number=1000;
Default Value Size
0 4 byte

long

The long data type provides a bigger range of values as compared to int data type, moreover, it is a 64-bit two’s complement integer.

Value Range

-9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)

Example

long distance;

distance=1221;
Default Value Size
0L 8 byte

float

The float data type is a 32-bit datatype that is used for decimal numbers and is not used for exact numbers.

Example

float pi;

pi=3.14;
Default Value Size
0.0f 4 byte

double

Similar to float data type, the double data type which is a 64-bit data type is used for decimal point numbers and this also is not used for exact numbers.

Value Range

Unlimited

Example

double area;

area=4324352.16;
Default Value Size
0.0d 8 byte

Now that we have a good understanding of the primitive data types let’s discuss non-primitive data types.

2. Non-Primitive Data Types

Data types that are user-defined and are not specified in the programming language are referred to as non-primitive data types.

The non-primitive data types in Java include

String

A string data type in Java is regarded as an object that stores text.

Classes

A class is a data type that is specified by a user that is used for the creation of objects. It specifies the properties or methods of objects that belong to a similar type.

Arrays

Java arrays are referred to as data structures that are implemented as objects. In simple words, these can be regarded as objects that contain a specific number and type of values.

Conclusion

Java data types are categorized into the following types; primitive data types and non-primitive data types. Primitive data types are already specified in the programming language however, non-primitive data types are user-defined. Boolean, byte, char, short, int, long, float, and double fall under the category of primitive data types, whereas, string, classes, and arrays are classified in the category of non-primitive data types. We have discussed these data types in detail in this write-up.

About the author

Naima Aftab

I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.