Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

Java Data Types

Primitive Data Types

Java has 8 built-in primitive data types. They are not objects and hold their values directly in memory.

TypeSizeDefaultRange
byte1 byte0-128 to 127
short2 bytes0-32,768 to 32,767
int4 bytes0-2,147,483,648 to 2,147,483,647
long8 bytes0L-9.2×10¹⁸ to 9.2×10¹⁸
float4 bytes0.0f~±3.4×10³â¸ (7 decimal digits)
double8 bytes0.0d~±1.7×10³â°â¸ (15 decimal digits)
char2 bytes'\u0000'0 to 65,535 (Unicode)
boolean1 bitfalsetrue or false
Primitive Types Example
public class PrimitiveTypes {
    public static void main(String[] args) {
        byte   b  = 100;
        short  s  = 30000;
        int    i  = 2_000_000;   // underscores improve readability
        long   l  = 9_000_000_000L;
        float  f  = 3.14f;
        double d  = 3.141592653589793;
        char   c  = 'A';
        boolean flag = true;

        System.out.println("byte:    " + b);
        System.out.println("short:   " + s);
        System.out.println("int:     " + i);
        System.out.println("long:    " + l);
        System.out.println("float:   " + f);
        System.out.println("double:  " + d);
        System.out.println("char:    " + c);
        System.out.println("boolean: " + flag);
    }
}

Wrapper Classes

Every primitive type has a corresponding wrapper class in java.lang. Wrapper classes allow primitives to be used as objects (e.g., in collections).

PrimitiveWrapper ClassUseful Methods
byteByteByte.parseByte(), Byte.MAX_VALUE
shortShortShort.parseShort()
intIntegerInteger.parseInt(), Integer.MAX_VALUE, Integer.toBinaryString()
longLongLong.parseLong()
floatFloatFloat.parseFloat()
doubleDoubleDouble.parseDouble()
charCharacterCharacter.isLetter(), Character.toUpperCase()
booleanBooleanBoolean.parseBoolean()

Autoboxing and Unboxing

Autoboxing is the automatic conversion of a primitive to its wrapper class. Unboxing is the reverse — wrapper to primitive. Java does this automatically.

Autoboxing & Unboxing
import java.util.ArrayList;

public class Autoboxing {
    public static void main(String[] args) {
        // Autoboxing: int -> Integer
        int primitive = 42;
        Integer wrapped = primitive;  // compiler inserts Integer.valueOf(42)

        // Unboxing: Integer -> int
        int back = wrapped;           // compiler inserts wrapped.intValue()

        // Autoboxing in collections (collections only accept objects)
        ArrayList<Integer> list = new ArrayList<>();
        list.add(10);   // autoboxed to Integer.valueOf(10)
        int val = list.get(0);  // unboxed automatically

        System.out.println("Wrapped: " + wrapped);
        System.out.println("Back:    " + back);
        System.out.println("From list: " + val);
    }
}

Type Casting

Widening casting (implicit) converts a smaller type to a larger type automatically. Narrowing casting (explicit) requires a cast operator and may lose data.

Type Casting
public class TypeCasting {
    public static void main(String[] args) {
        // Widening: byte -> short -> int -> long -> float -> double
        int i = 100;
        long l = i;       // implicit widening
        double d = l;     // implicit widening
        System.out.println("Widening: " + d);  // 100.0

        // Narrowing: double -> int (explicit cast required)
        double pi = 3.99;
        int truncated = (int) pi;  // fractional part is dropped, NOT rounded
        System.out.println("Narrowing: " + truncated);  // 3

        // char <-> int
        char ch = 'A';
        int ascii = ch;           // widening: char to int
        char back = (char) 66;    // narrowing: int to char
        System.out.println("char to int: " + ascii);  // 65
        System.out.println("int to char: " + back);   // B
    }
}

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.