In this Java tutorial, you will learn about Java data types, the eight primitive data types, their sizes, ranges, default values, literals, type conversion rules, and practical examples for declaring and initializing variables.

Java Data Types Overview

In Java, a data type tells the compiler what kind of value a variable can store. It also controls what operations are valid for that value. Java data types are divided into two broad categories: primitive data types and reference data types.

Primitive data types store simple values such as numbers, characters, and boolean values. Reference data types store references to objects, arrays, strings, and class instances. User-defined data types are typically implemented as classes, which we will explore in detail in our Java Object Oriented Programming tutorials.

1 What is a Data Type in Java?

A data type in Java specifies the kind of value a variable can hold, such as an integer, decimal number, character, boolean value, or object reference. It helps the compiler reserve suitable memory, check type safety, and prevent invalid operations before the program runs.

For example, an int variable can store whole numbers, a double variable can store decimal values, a boolean variable can store only true or false, and a String variable can refer to a sequence of characters.

2 Primitive and Reference Data Types in Java

Java has two main groups of data types:

  • Primitive data types: Built-in data types that store simple values directly. Java has exactly eight primitive data types.
  • Reference data types: Types that store references to objects, arrays, classes, interfaces, enums, and strings.

The important difference is that primitive variables hold actual values, while reference variables hold a reference to an object in memory. For example, int age = 25; stores the integer value directly, but String name = "Arun"; stores a reference to a String object.

3 List of 8 Primitive Data Types in Java

Java provides eight primitive data types:

  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. boolean
  8. char

Although a sequence of characters, called a string, is used very often in Java programs, String is not a primitive data type. Java provides strings through the java.lang.String class.

4 Java Primitive Data Types with Size, Range, and Default Values

The table below summarizes each primitive data type’s purpose, size, value range, and default value for fields. These default values apply to instance variables and static variables, not to local variables inside methods.

Data TypeUseSizeRange / ValuesDefault Value
byteSmall whole numbers8 bits-128 to 1270
shortWhole numbers smaller than int16 bits-32,768 to 32,7670
intDefault whole-number type32 bits-2,147,483,648 to 2,147,483,6470
longLarge whole numbers64 bits-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8070L
floatSingle-precision decimal values32-bit IEEE 754Approx. 6 to 7 decimal digits of precision0.0f
doubleDefault decimal type64-bit IEEE 754Approx. 15 decimal digits of precision0.0d
booleanLogical true/false valuesJVM-specific representationtrue or falsefalse
charSingle UTF-16 code unit16 bits'\u0000' to '\uffff''\u0000'

Note #1: Java integer primitive types are signed. The integral types byte, short, int, and long use two’s complement representation.

Note #2: In Java SE 8 and later, wrapper classes such as Integer and Long provide methods that let you perform some unsigned operations. This does not create new primitive unsigned types.

Note #3: Instance variables and class variables receive default values if they are not explicitly initialized. However, local variables are not automatically initialized, so you must assign a value before using them.

Note #4: The term signed means that a numeric type can represent negative, zero, and positive values. Java does not provide separate unsigned primitive integer types like some other languages do.

5 Choosing the Right Java Data Type

For most beginner Java programs, choose int for whole numbers, double for decimal numbers, boolean for conditions, char for a single character, and String for text. Use the smaller or larger numeric types only when there is a specific reason.

RequirementRecommended TypeExample
Age, count, index, marksintint score = 95;
Large count or timestamp-like valuelonglong population = 1400000000L;
Measurement or decimal calculationdoubledouble price = 99.75;
Memory-sensitive decimal arrayfloatfloat temperature = 36.6f;
True/false conditionbooleanboolean isPassed = true;
Single charactercharchar grade = 'A';
Text valueStringString city = "Delhi";

Use byte and short mainly when you are working with binary data, file formats, network data, or very large arrays where memory usage matters. In ordinary arithmetic expressions, Java often promotes byte and short values to int.

6 Data Type of a Java Variable

Java is a statically typed language. This means every variable must be declared with a specific data type before it is used. The compiler uses this information to detect type errors early.

For example, to declare a variable n as an int, you would write:

</>
Copy
int n;

Once declared, the variable n is treated as an integer variable. You can assign integer values to it, but you cannot assign incompatible values such as a string without a conversion.

</>
Copy
public class VariableTypeExample {
    public static void main(String[] args) {
        int n = 25;
        System.out.println(n);

        // n = "Java"; // Error: String cannot be assigned to int
    }
}
25

7 Initializing Variables with Java Data Types

It is a good practice to initialize variables so that they have a well-defined starting state. The table below provides examples of initializing variables for different Java data types:

Data TypeExample InitializationImportant Detail
bytebyte b = 65;Value must fit in the byte range.
shortshort s = 456;Value must fit in the short range.
intint i = 10;Default whole-number literal type.
longlong l = 25589L;Use L for long literals, especially for large values.
floatfloat f = 5.6f;Use f or F because decimal literals are double by default.
doubledouble d = 5.63128e3;Default decimal literal type.
booleanboolean isTodayHoliday = false;Only true or false are valid.
charchar ch = 'A';Use single quotes for a character.
StringString str = "Good day!";Use double quotes for a string.

The following program demonstrates how to initialize variables of various data types:

Example.java

</>
Copy
public class Example {
    public static void main(String[] args) {
        byte b = 65;
        short s = 456;
        int i = 10;
        long l = 25589L;
        float f = 5.6f;
        double d = 5.63128e3;
        boolean bool = false;
        char ch = 'A';
        String str = "Good day!";
    }
}

8 Java Numeric Data Types and Literal Suffixes

Java treats whole-number literals as int by default and decimal literals as double by default. This is why long and float examples often use suffixes.

LiteralMeaningExample
L or lLong literallong distance = 9876543210L;
F or fFloat literalfloat rate = 7.5f;
D or dDouble literaldouble value = 12.5d;

Prefer uppercase L for long literals because lowercase l can look similar to the digit 1.

</>
Copy
public class LiteralExample {
    public static void main(String[] args) {
        long largeNumber = 9876543210L;
        float percentage = 86.5f;
        double average = 91.25;

        System.out.println(largeNumber);
        System.out.println(percentage);
        System.out.println(average);
    }
}
9876543210
86.5
91.25

9 Type Conversion Between Java Data Types

Java allows some conversions automatically, but requires explicit casting for conversions that may lose information.

Widening Conversion in Java

Widening conversion happens when a smaller numeric type is converted to a larger numeric type. Java can do this automatically because the conversion is generally safe.

</>
Copy
public class WideningExample {
    public static void main(String[] args) {
        int marks = 95;
        double marksAsDecimal = marks;

        System.out.println(marksAsDecimal);
    }
}
95.0

Narrowing Conversion in Java

Narrowing conversion happens when a larger numeric type is converted to a smaller numeric type. Java requires an explicit cast because data may be lost.

</>
Copy
public class NarrowingExample {
    public static void main(String[] args) {
        double price = 99.75;
        int roundedDownPrice = (int) price;

        System.out.println(roundedDownPrice);
    }
}
99

In the example above, the decimal part is removed when double is cast to int. This is not rounding; it is truncation toward zero.

10 Default Values of Fields and Local Variables in Java

A common beginner mistake is assuming that all Java variables automatically get default values. Default values are assigned only to fields, such as instance variables and static variables. Local variables inside methods must be initialized before use.

</>
Copy
public class DefaultValueExample {
    static int count;
    static boolean active;

    public static void main(String[] args) {
        System.out.println(count);
        System.out.println(active);
    }
}
0
false

The following local variable example does not compile because x is declared inside the method but not initialized before use.

</>
Copy
public class LocalVariableExample {
    public static void main(String[] args) {
        int x;
        // System.out.println(x); // Error: variable x might not have been initialized
    }
}

11 Common Mistakes with Java Data Types

  • Using float without f: Write float amount = 10.5f;, not float amount = 10.5;.
  • Forgetting L for large long values: Write long n = 9876543210L; when the value is outside the int range.
  • Using double quotes for char: Write char grade = 'A';. Double quotes create a String, not a char.
  • Expecting local variables to have default values: Always initialize local variables before reading them.
  • Assuming String is primitive: String is a reference type, even though Java provides special syntax for string literals.

12 Java Data Types Quick Review Checklist

  • Check that the tutorial clearly states there are exactly eight primitive data types in Java.
  • Verify that String is explained as a reference type, not as a primitive data type.
  • Confirm that numeric sizes and ranges are accurate for byte, short, int, and long.
  • Ensure that default values are described only for fields, not for uninitialized local variables.
  • Check that examples use correct Java literal suffixes such as L for long and f for float.

Java Data Types FAQs

What are the 8 data types in Java?

The 8 primitive data types in Java are byte, short, int, long, float, double, boolean, and char.

Is String a primitive data type in Java?

No. String is not a primitive data type in Java. It is a class in the java.lang package. A String variable stores a reference to a string object.

What is the default data type for whole numbers in Java?

The default data type for whole-number literals in Java is int. If a whole-number value is too large for int, use the L suffix and store it in a long.

What is the default data type for decimal numbers in Java?

The default data type for decimal literals in Java is double. To store a decimal literal in a float variable, add the f or F suffix, such as float value = 12.5f;.

Do local variables get default values in Java?

No. Local variables in Java do not get default values automatically. You must initialize a local variable before reading it. Fields, such as instance variables and static variables, receive default values.

Conclusion

In this Java Tutorial, we explored Java data types, including the eight primitive data types, their sizes, ranges, default values, declaration syntax, initialization examples, literal suffixes, and type conversion rules. Understanding these basics makes it easier to write Java programs that are type-safe, readable, and easier to debug.