🌱BeginnerVariables & Data Types

Primitive Data Types

Learn Java's eight primitive types - int, double, boolean, char, byte, short, long, and float - and when to use each.

Overview

Java has eight primitive data types that are built into the language. They store simple values directly in memory (unlike objects, which store references). Choosing the right primitive type matters for memory efficiency and avoiding data loss. The four most commonly used primitives are: int (whole numbers), double (decimal numbers), boolean (true/false), and char (a single character). The other four (byte, short, long, float) exist for specific use cases like memory-constrained systems or financial calculations.

Key Points

  • •int - 32-bit whole number, range −2.1 billion to +2.1 billion; default choice for integers
  • •double - 64-bit floating-point decimal; default choice for numbers with decimal points
  • •boolean - true or false; used for conditions and flags
  • •char - a single Unicode character in single quotes: 'A', '9', '@'
  • •long - 64-bit integer for very large numbers; append L to literals: 9_000_000_000L
  • •float - 32-bit floating-point (less precise than double); append f to literals: 3.14f
  • •byte (−128 to 127) and short (−32768 to 32767) save memory when ranges are small

Examples

The four most common primitive types

public class Main {
    public static void main(String[] args) {
        int age = 28;
        double salary = 55000.50;
        boolean isEmployed = true;
        char grade = 'A';

        System.out.println("Age: " + age);
        System.out.println("Salary: " + salary);
        System.out.println("Employed: " + isEmployed);
        System.out.println("Grade: " + grade);
    }
}

char uses single quotes; String (text) uses double quotes.

All eight primitive types with their ranges

byte  small = 127;           // -128 to 127
short medium = 32767;         // -32,768 to 32,767
int   number = 2_147_483_647; // ~2.1 billion
long  big = 9_223_372_036L;   // ~9.2 quintillion (needs L)

float  pi32 = 3.14159f;  // ~7 decimal digits precision (needs f)
double pi64 = 3.141592653589793; // ~15 decimal digits precision

boolean flag = false;
char letter = 'Z';

Underscores in numeric literals (2_147_483_647) are ignored by Java but improve readability.

Default values for primitive fields (in a class, not a local variable)

public class Defaults {
    static int    num;     // default: 0
    static double dec;     // default: 0.0
    static boolean flag;   // default: false
    static char   ch;      // default: '\u0000' (null char)

    public static void main(String[] args) {
        System.out.println(num);   // 0
        System.out.println(dec);   // 0.0
        System.out.println(flag);  // false
        System.out.println((int) ch); // 0
    }
}

Instance and class (static) fields are automatically given default values. Local variables are not.

Common Mistakes

  • Forgetting the L suffix on long literals: `long x = 10000000000;` causes a compile error - use `10000000000L`
  • Forgetting the f suffix on float literals: `float f = 3.14;` causes an error - the literal is a double by default
  • Using float instead of double for precision-sensitive calculations - float has only ~7 significant digits

Note

Use int for whole numbers and double for decimals in almost every situation. Reach for long only when you need numbers larger than ~2.1 billion, and avoid float unless you are specifically optimising memory.

Exercises

Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.

Loading editor…

Completed: 0 / 4 exercises

Knowledge Check

Question 1 of 4

Which primitive type should you use to store the value 3.14159?