Java Data Types

As discussed earlier, data type specifies the type of data which we are storing in a variable.

In Java, we can divide data types in two groups:

  1. Primitive data types
  2. Reference data types

Primitive Data Types

Data TypeSizeDescription
byte8 bitsRepresents a signed 8-bit integer value. Range: -128 to 127.
short16 bitsRepresents a signed 16-bit integer value. Range: -32,768 to 32,767.
int32 bitsRepresents a signed 32-bit integer value. Range: -2^31 to 2^31-1.
long64 bitsRepresents a signed 64-bit integer value. Range: -2^63 to 2^63-1.
float32 bitsRepresents a single-precision floating-point number. It's used for decimal values. Example: 3.14159f.
double64 bitsRepresents a double-precision floating-point number. It's used for decimal values. Example: 3.14159.
char16 bitsRepresents a single Unicode character. Example: 'A', '1'.
boolean1 bitRepresents a true or false value.

Reference Data Types

Data TypeDescription
classRepresents user-defined types. Classes define objects and their behavior in Java.
interfaceRepresents a blueprint for classes. Interfaces define a set of methods that classes must implement.
enumRepresents a special type used to define a set of constants. Enums are often used for defining a limited set of values.
arrayRepresents a collection of elements of the same data type. Arrays can be of primitive or reference types.

We will not focus on the details of reference data types here.

String

Although it's not a primitive data type, String is widely used to represent text in Java. It's a class that provides methods for manipulating strings. We will discuss more about strings in strings section.

Variables with different Data Types

Scientific Numbers in Java

A floating point number can be used to store a scientific number with "e" corresponding to the power of 10.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

Which data type is used to store decimal numbers?

10 XP~2 min
Exercise 2 of 2Medium

What is the typical size of an int data type?

15 XP~3 min