Java – Data Types

There are two kinds of data types is Java. They primitive data types and user-defined datatypes.

In this tutorial, we shall learn about primitive data types in detail with the help of examples.

What is Data Type?

Data type represents the type of value stored in a variable. It defines the size of storage in memory for a variable and range of values a variable can hold.

List of Primitive Data Types in Java

There are eight primitive data types in Java. They are

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

Though a string of chars is not a primitive data type, there is a lot of support given to char strings via java.lang.String class.

Primitive Data Types – Size & Default Value

The following table summarizes the size, default value of all data types in Java. Also provided in the last column is the possibility of 2’s compliment on a data type in Java.

Data TypeSizeDefault Value2’s complement (signed)
byte 8 bits0 yes
short16 bits0 yes
int32 bits0 yes
long64 bits0L yes

32-bit IEEE 754 floating point

0.0f no
double double-precision

64-bit IEEE 754 floating point

0.0d no
boolean 1 bitfalse no

16-bit Unicode character

‘\u0000’ no

Note #1: int could be used as an unsigned with the help of Integer class, in Java SE8.

Note #2: Though the variables aren’t declared gets a default value by the compiler, there is a scope for variables, called local, and could turn out to be dangerous not to initialize them. This is because neither the compiler initializes local variables. And when we access these uninitialized local variables, compiler may throw an error during compilation.

Note #3: Signed/Unsigned reveals if that datatype could hold a negative value or not, respectively.

Data Type of Variable

As you know, java is strictly a statically-typed language. Therefore, you should declare the variable to a data type before you can use it. Literally, you have to type in the data type before writing out your variable in the program. This ensures that the name used for the variable of a data type is not used for another data type in subsequent instructions. If the same variable name is used to declare a variable of another data type, the compiler throws an error during compiling. An example to declare a variable that it belongs to a data type is shown below:

int n;

When you mention your variable ‘n’ in your program for the first time, it has to be declared, which in this case to ‘int’. And from this moment on, in its lifetime, it stores like an int, it behaves like an int and everything like an int. The variable once declared of a type, in its lifetime, doesn’t take any other data type value(unless it could be typecast).

Initialize Variables

It is always a good practice to initialize a variable. It makes sure you know the initial state of that variable and avoid compile errors(Note #3 above). Being said that, now let us see how to initialize variables of different data types with the help of examples in the following table.

Data TypeExample for initialization
bytebyte b = 65;
shortshort si = 456;
intint i = 10;
longlong li = 25589;
floatfloat f = 5.6f;
doubledouble dd = 5.63128e3;
booleanboolean isTodayHoliday = false;
charchar ch = ‘A’;
StringString str = “Good day!”;

In the following program, we will initialize variables of different datatypes that we mentioned in the above table.

public class Example{
    public static void main(String[] args){
    	byte b = 65;
        short s = 456;
        int i = 10;
        long l = 25589;
        float f = 5.6f;
        double d = 5.63128e3;
        boolean bool = false;
        char ch = 'A';
        String str = "Good day!";


In this Java Tutorial, we have learnt about the Data Types in Java, their size in memory, default values, and how to declare these data types in a Java Program.

In our next Java Tutorial, we shall learn Java Variable Types, which are responsible for holding data belonging to appropriate data types and providing a named reference to those memory locations.