Table of contents
1.
Introduction
2.
What are the data types in Java?
3.
Primitive Data Type
3.1.
Boolean data type
3.2.
byte data type
3.3.
char data type
3.4.
short data type
3.5.
int data type
3.6.
long data type
3.7.
float data type
3.8.
double data type
3.9.
Code
3.10.
Output
4.
Non-Primitive Data Type
4.1.
Arrays
4.2.
Strings
4.3.
Classes
4.4.
Interfaces
5.
Frequently asked questions
5.1.
What are the 8 primitive data types in Java?
5.2.
What is an object in Java?
5.3.
Why is the char 2 byte in size in java?
6.
Conclusion:
Last Updated: Jul 23, 2025

Data Types in Java

Author Rhythm Jain
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java is a statically and highly typed language since each type of data is predefined as part of the programming language.

A variable's data type is an attribute that tells the compiler how the programmer wants to utilize the variable. It specifies the actions that may perform on the data and the types of values that can be stored.

In this article, we will have a brief insight into the different data types in Java.

What are the data types in Java?

The data types in Java elaborate what sort of data a variable may contain. They are divided into primitive types (such as int, float, boolean, and char), and non-primitive types which include String, arrays, and objects.

Java has two types of data: 

  1. Primitive Data Type
  2. Non-Primitive Data Type or Object Data Type

Let us look at each of them in detail.

Primitive Data Type

The programming language pre-defines a primitive data type. It contains no extra sophisticated methods and specifies the size and type of variable values.

These are the most fundamental data types in the Java programming language.

There are eight kinds of primitive data types supported by Java:

  1. Boolean data type
  2. byte data type
  3. char data type
  4. short data type
  5. int data type
  6. long data type
  7. float data type
  8. double data type

Boolean data type

A boolean data type comprises a single bit of data that can only contain true or false values. True/false circumstances are tracked using this data type.

byte data type

It's an 8-bit two-s complement signed integer. It stores numbers in the range of -128 to 127. A byte data type helps conserve significant quantities of memory.

char data type

A single character is stored in this data type. Single quotes, such as 'A' or 'a', must be used to enclose the character. Alternatively, ASCII values can be used to show certain characters. It is 2 bytes in size.

short data type

The short data type is a 16-bit signed two's complement integer. It keeps track of values ranging from -32,768 to 32767. Use a short to conserve memory in big arrays, similar to byte.

int data type

It is a 4 Bytes or 32-bit signed two's complement integer. Whole numbers between -2147483648 and 2147483647 can be stored in this data format.

long data type

It is an 8 Bytes, or 64-bit signed two's complement integer. Whole numbers between -263 to 263-1can be stored in this data format.

float data type

The float data type is a 32-bit single-precision floating-point data type. It is capable of storing fractional integers between 3.4e-038 and 3.4e+038. The value should end with an “f.”

double data type

The float data type is a 64-bit double-precision floating-point data type. It is capable of storing fractional integers between 1.7e−308 and 1.7e+308. The value should end with a “d.”

Let us give an example and try to use all of the primitive data types in java.

Code

public class Main
{
public static void main(String[] args) {
    //boolean
boolean bt = true;
        System.out.println(bt);
        
        //byte
        byte n;
        n = 127;
        System.out.println(n); // prints 127
        
        //char
        char a = 'A';
        char b = 66;
        System.out.println(a); // prints A
        System.out.println(b); // Displays B


        //short
        short m= 1234;
        System.out.println(m);
        
        //int
        int num1=56782;
        System.out.println(num1);
        
        //long
        long num2 = 15000000000L;
        System.out.println(num2);
        
        //float
        float num3 =3.14f;
        System.out.println(num3);


        //double
        double num4 = 69.784d;
        System.out.println(num4);
}
}
You can also try this code with Online Java Compiler
Run Code

Output

true
127
A
B
1234
56782
15000000000
3.14
69.784

Non-Primitive Data Type

These data types are not actually defined by the programming language but are created by the programmer. These are also called Object Data Types or Reference Data types.

Since they contain a memory address of variable value rather than the value itself.

There are four kinds of non-primitive data types supported by Java:

  1. Arrays
  2. Strings
  3. Classes
  4. Interfaces

Now let us study them in-depth.

Arrays

In Java, arrays are objects that represent homogenous data structures. Arrays are data structures that contain one or more values of a given data type and allow for indexed access to those values. The index of an array element is used to access it.

Strings

A sequence of characters is referred to as a string. On the other hand, a string in Java is an object that represents a sequence of characters. The java .lang package. A string object is created using the String class. Unlike C/C++, Java strings do not have a null character at the end.

Classes

In Java, a class is a user-defined blueprint or prototype containing all your data. To represent the behavior of an object, a class has fields (variables), properties, and methods.

Interfaces

An interface in Java is a user-defined blueprint or prototype that includes all your data ,just like a class, but the methods declared in the interface are all abstract. That is, it contains only definition and signature and no body.

Frequently asked questions

 

What are the 8 primitive data types in Java?


The 8 primitive data types in Java are: byte, short, int, long, float, double, char, and boolean. These are the building blocks for data storage and operations in Java.

What is an object in Java?


Object in java is nothing but an instance of a class. An object has the state and behavior of the class it belongs to but with a unique identity.


Why is the char 2 byte in size in java?


Other languages, such as C/C++, utilize just ASCII characters, and 8 bits is sufficient to represent all ASCII characters. However, Java uses the Unicode system, not the ASCII coding system, and 8 bits is insufficient to express all Unicode characters; hence Java requires 2 bytes for characters.

Conclusion:

Understanding data types in Java is fundamental for efficient memory management and precise programming. By mastering both primitive and non-primitive data types, developers can write robust, type-safe, and scalable applications.

 

Recommended Readings:

 

 

Live masterclass