Java – Data Types4 min read

What are Data Types in Java?

Data types specify the varying sizes and values in the variables that can be stored. That is every variable is assigned by data-types according to the need. And based on their respective data-types, the operating system allocates memory to that data-types.

There are two data types in Java:

  • Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
  • Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.

1. Primitive data types:

These are the most basic data types available in the Java language.
There are eight primitive data-types available by Java:

Data type SizeDescription
byte 1 byteStores whole numbers from -128 to 127
short 2 bytesStores whole numbers from -128 to 127
char 2 bytesStores whole numbers from -32,768 to 32,767
int 4 bytesStores a single character/letter or ASCII values
long 8 bytesStores whole numbers from -2,147,483,648 to 2,147,483,647
float 4 bytesStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
double 8 bytesStores fractional numbers. Sufficient for storing 6 to 7 decimal digits
boolean 1 bitStores true or false values

These are put into four group:

  • Integers: This group includes byte, short, int, and long, which are for whole-valued signed numbers.
  • Floating-point numbers: This group includes float and double, which represent numbers with fractional precision.
  • Characters: This group includes char, which represents symbols in a character set, like letters and numbers.
  • Boolean: This group includes boolean, which is a special type for representing true/false values.


  • byte:
    It is an 8-bit signed two’s complement integer. The byte data type is used to save memory in large arrays.
    Its minimum value is -128 and the maximum value is 127. Its default value is 0.
    byte x = 15;
    byte y = -12;
  • Short:
    The short data type is a 16-bit signed two’s complement integer. A short is 2 times smaller than an integer. Its minimum value is -32,768 and the maximum value is 32,767. Its default value is 0. And is also used to save memory.
    short x = 1500;
    short y = -1200;
  • int:
    The int data type is a 32-bit signed two’s complement integer. It is a default data-type for integral values.Its minimum value is – 2,147,483,648 and maximum value is 2,147,483,647. Its default value is 0.
    int x = 25;
    int y = -35;
  • long:
    The long data type is a 64-bit two’s complement integer. Its value-range lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807 (2^63 -1) (inclusive). Its default value is 0. long is used when int is not large enough to store the value.
    x = 200000L;
    long y = -300000L;

Floating Point Types:

  • float:
    The float data type is a single-precision 32-bit IEEE 754 floating-point. It is used when the user needs to save memory in large arrays of floating-point numbers. float is never used for precise values such as for currency. Its default value is 0.0d.
    float x = 234.5f;
  • double:
    The double data type is a double-precision 64-bit IEEE 754 floating-point. The double is generally used for decimal values just like float. The double also should never be used for precise values, such as currency. Its default value is 0.0d.
    double x = 13.99d;


The char data type is a single 16-bit Unicode character. The size of a character is 2 bytes. It is used to store single characters and must be enclosed within single quotes as shown in the example. Example:


A boolean keyword is used to declare with the boolean data-type and represents one bit of information that is either true or false.

2. Non-Primitive Data Types:

The non-primitive data types include Strings, Classes, Interfaces, and Arrays. These are also known as reference types because they refer to objects.
Non-primitive types are created by the user/programmer and are not predefined by Java except one that is String and can have null values whereas primitive data types are predefined in Java and always has a value.

To learn about Non-Primitive Data Types click on the following:
Strings, Classes, Interfaces, Arrays.


Java Program to check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More