In this Java tutorial, you will learn about primitive datatypes, how many are there, the default value and size of a value of a specific datatype, etc., with examples.


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

User-defined datatypes are classes and we shall discuss about these in Java Object Oriented Programming concept tutorials.

What is datatype

A datatype 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 datatypes in Java

There are eight primitive datatypes in Java. They are

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char

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

Primitive Datatypes – Size & Default Value

The following table summarises the size of a value, default value, and information about the possibility of 2’s complement for each datatype.

DatatypeSizeDefault 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: Variables that are declared but not initialized, gets a default value by the compiler. But there is an exception for local variables. The compiler does not initialize local variables, which could turn things uncertain and therefore it is a good practice to initialize variables. When we try to access these un-initialized local variables, compiler may throw an error during compilation that the variable is used without initialization.

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

Datatype of Variable

As we know, Java is strictly a statically-typed language. Therefore, we should declare the datatype for a variable before we can use it. Literally, we have to type in the datatype before writing out your variable name for the first time in the program. This ensures that the name used for the variable with a specific datatype is not used for another datatype in subsequent program statements. If the same variable name is used to declare a variable of another datatype, the compiler throws an error during compiling.

An example to declare a variable n with an int datatype is

int n;

When we mention the variable n in our program for the first time, it has to be declared, which in this case is ‘int’. And from this moment on, in its lifetime, it is stored in memory like an integer, it behaves like an integer and everything like an integer. The variable once declared with a specific datatype, cannot be redeclared.

Initialize Variables

It is always a good practice to initialize a variable. It makes sure we 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 datatypes with the help of examples in the following table.

DatatypeExample 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 Datatypes in Java, their size in memory, default values, and how to declare these datatypes in a Java Program.