Java Data types

This tutorial is about various Data types in Java and how to use them

If you want to master Java, or even grasp it’s core concepts well, you need to understand the foundation. You need to be aware of the limitations, sizes and quirks of each data type. Incorrect handling of Data types may go unnoticed for a while, but will cause problems later on.

Data types in Java are split into two categories, Primitive and Non-Primitive. Primitive data types include Boolean, Byte, int, char, long, double, float and short. Non-primitive data types include String, Arrays and Classes.

Due to the complexity of classes, strings and Arrays as data types, they are discussed separately in the Strings, Arrays and classes sections respectively.

Primitive Data Types:

A list of all primitive data types with their descriptions.

TypeMeaning
BooleanHolds True and False values
bytestores integers of size 8 bit
charStores a character
doubleDouble precision floating point
floatSingle precision floating point
intStores a integer
longStores long integer
shortStores a short integer

Boolean

The Boolean data type, has only two possible values. true, or false. Used often in Logic gates or logical operations.

boolean a;

a = true;
System.out.println(a);
a = false;
System.out.println(a);

char

The char data type is a 16 bit type having a range of 0 to 65,535. Java uses Unicode, hence the large range. Since ASCII is a subset of Unicode, Java char data type and ASCII are compatible. (ASCII range is from 0 to 127). The purpose of using Unicode is to ensure portability across all systems.

float

There are two kinds of data types to represent decimals values in Java. The only difference between them is the range of values they support. The first is float, which is a 32 bit data type, and the second is double, which is a 64 bit data type. double is more often used type both by programmers and Java’s functions itself.

Integers