In java,every variable has a type, every expression has a type and all types are strictly defined.All the assignments should be checked by the compiler for the type compatibility. Hence java language considers as strongly typed language.


Note:Except boolean and char all the remaining datatypes are signed datatypes i.e we can represent both +ve and –ve numbers.

Integral data types

  1. byte

    Size : 8-bits
    Range: -27 to 27-1(-128 to 127)
    -ve numbers can be represented in 2’s compliment form.

    byte b = 10;
    byte b = 127;
    byte b = 130;//Compile time error: possible loss of precision
                  //found int
    byte b = true;//Compile time error:Incompatible types.
                 //found: boolean
                 //required: byte

    byte datatype is best suitable if we are handling data either from file or form network.

  2. short

    size = 2 bytes
    range = -215 to 215-1 (-32768 to 32767)

    short s = 10;
    short s = 32767;
    short s = 65535;//C.E: possible loss of precision.
    short s = true; //C.E: Incompatible types

    short is best suitable datatype for 16-bit process. But currently these are completely outdated and hence the corresponding data types also no one is using.

  3. int

    The most commonly used datatype is int.
    size = 4 bytes
    range = -231 to 231 – 1(-2147483648 to 2147483747)
    The size of int is always fixed irrespective of platform and the chance of failing java program is very less if you are changing the platform, hence the java is considered as Robust.

  4. long
    if int is not enough to hold big values then we should go for long datatype
    size = 8 bytes
    range = -263 to 263 – 1
    The amount of distance travelled by light in 1000 days can be represented by long datatype only and int is not enough.

Floating-point data types

for representing real numbers(numbers with decimal points)


boolean data type

size = not applicable(virtual machine dependent).
range = not applicable but allowed values are true/false.

package com.java2learn.datatypes;
public class BooleanDemo {

	public static void main(String[] args) {
		int x = 0;

Compile time error: Incompatible types found :int
required: boolean.

char data type

java provides support for Unicode i.e it supports all world wide alphabets. Hence the size of char in java is 2 bytes.and range is 0 to 65535.

Comparison table for java primitive datatypes