DevAcademia
C++C#CPythonJava
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz

Loading Java tutorial…

Loading content
Java BasicsTopic 13 of 59
←PreviousPrevNextNext→

Java Data Types - Complete Guide

Introduction to Java Data Types

Data types in Java define the kind of values that can be stored in variables and how much memory they require. Java is a strongly typed language, so each variable must be declared with a data type before use. This enforces type safety and reduces errors.

Java data types are grouped into two main categories: primitive types and non-primitive (reference) types. Understanding both is essential for writing correct and efficient programs.

Primitive vs Non-Primitive Data Types

Java divides its data types into primitives, which are built into the language, and non-primitives, which are created using classes.

FeaturePrimitive TypesNon-Primitive Types
DefinitionPredefined by JavaCreated by programmer (classes, objects)
StorageStored directly in stack memoryReference stored in stack, object in heap
Default ValueDefault values like 0, 0.0, false, '\u0000'Default is null
SizeFixed size depending on typeVariable size depending on object
Examplesbyte, short, int, long, float, double, char, booleanString, Arrays, Classes, Interfaces

The 8 Primitive Data Types

Java provides eight primitive types that are the foundation of data representation.

Example
public class PrimitiveTypes {
    public static void main(String[] args) {
        byte small = 100;           // 8-bit, -128 to 127
        short medium = 10000;       // 16-bit, -32,768 to 32,767
        int number = 1000000;       // 32-bit, -2^31 to 2^31-1
        long big = 10000000000L;    // 64-bit, -2^63 to 2^63-1

        float price = 19.99f;       // 32-bit, ~7 decimal digits
        double pi = 3.1415926535;   // 64-bit, ~15 decimal digits

        char grade = 'A';           // 16-bit Unicode character
        boolean active = true;      // true or false

        System.out.println("Byte: " + small);
        System.out.println("Short: " + medium);
        System.out.println("Int: " + number);
        System.out.println("Long: " + big);
        System.out.println("Float: " + price);
        System.out.println("Double: " + pi);
        System.out.println("Char: " + grade);
        System.out.println("Boolean: " + active);
    }
}
Output
Byte: 100
Short: 10000
Int: 1000000
Long: 10000000000
Float: 19.99
Double: 3.1415926535
Char: A
Boolean: true

Default Values of Primitive Types

Instance variables of primitive types are automatically initialized with default values when not explicitly set. Local variables, however, must be initialized before use.

Example
public class DefaultValues {
    byte defaultByte;
    short defaultShort;
    int defaultInt;
    long defaultLong;
    float defaultFloat;
    double defaultDouble;
    char defaultChar;
    boolean defaultBoolean;

    public static void main(String[] args) {
        DefaultValues dv = new DefaultValues();
        System.out.println("Default byte: " + dv.defaultByte);
        System.out.println("Default short: " + dv.defaultShort);
        System.out.println("Default int: " + dv.defaultInt);
        System.out.println("Default long: " + dv.defaultLong);
        System.out.println("Default float: " + dv.defaultFloat);
        System.out.println("Default double: " + dv.defaultDouble);
        System.out.println("Default char: " + (int)dv.defaultChar);
        System.out.println("Default boolean: " + dv.defaultBoolean);
    }
}
Output
Default byte: 0
Default short: 0
Default int: 0
Default long: 0
Default float: 0.0
Default double: 0.0
Default char: 0
Default boolean: false

Type Conversion and Casting

Java supports type conversion between primitives. Widening (implicit) conversion happens automatically when assigning a smaller type to a larger type. Narrowing (explicit) conversion requires casting and may lose data.

Example
public class TypeConversion {
    public static void main(String[] args) {
        int intValue = 100;
        long longValue = intValue;       // widening
        double doubleValue = intValue;   // widening

        double precise = 9.87;
        int approx = (int) precise;      // narrowing, decimal truncated

        char letter = 'A';
        int code = letter;               // char to int

        System.out.println("Double: " + precise + ", Cast to int: " + approx);
        System.out.println("Char: " + letter + ", Code: " + code);

        float nan = Float.NaN;
        float infinity = Float.POSITIVE_INFINITY;

        System.out.println("NaN: " + nan);
        System.out.println("Infinity: " + infinity);
    }
}
Output
Double: 9.87, Cast to int: 9
Char: A, Code: 65
NaN: NaN
Infinity: Infinity

Best Practices for Data Types

  • ✅ Use the smallest type that fits the value range
  • ✅ Prefer int for integer math unless a wider range is needed
  • ✅ Use double for general floating-point calculations
  • ✅ Cast explicitly when narrowing types
  • ✅ Mark constants with final for clarity
  • ✅ Use wrapper classes when objects are required (e.g., in collections)
  • ✅ Watch out for overflow/underflow with numeric types
  • ✅ Choose meaningful variable names reflecting purpose
Test your knowledge: Java Data Types - Complete Guide
Quiz Configuration
4 of 10 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Java BasicsTopic 13 of 59
←PreviousPrevNextNext→