In Java, data types are crucial because they specify the size and type of values that variables can hold. Understanding data types is fundamental to writing correct and efficient Java programs. Every variable you declare in Java must have a data type, which tells the compiler how much memory to allocate for the variable and what kind of operations can be performed on it.

Java data types can be broadly categorized into two main groups:

  1. Primitive Data Types

  2. Non-Primitive (Reference) Data Types

Primitive Data Types

Primitive data types are the most basic data types available in Java. They are predefined by the language and hold simple values. Java is a strongly typed language, meaning that all variables must be declared with a specific data type before they can be used.

There are eight primitive data types in Java:

Numeric Data Types

These are used to store numbers.

  • Integer Types (for whole numbers):

    • byte:

      • Size: 1 byte (8 bits)

      • Range: −128 to 127

      • Default Value: 0

      • Usage: Useful for saving memory in large arrays, where the actual values are within its range.

      • Example: byte age = 30;

    • short:

      • Size: 2 bytes (16 bits)

      • Range: −32,768 to 32,767

      • Default Value: 0

      • Usage: Can be used for small integer numbers.

      • Example: short year = 2025;

    • int:

      • Size: 4 bytes (32 bits)

      • Range: −231 to 231−1 (approx. −2 billion to 2 billion)

      • Default Value: 0

      • Usage: This is the most commonly used integer type for general-purpose integer values.

      • Example: int population = 700000000;

    • long:

      • Size: 8 bytes (64 bits)

      • Range: −263 to 263−1 (very large numbers)

      • Default Value: 0L (note the 'L' suffix)

      • Usage: Used when int is not large enough to hold the value, e.g., for timestamps or large calculations.

      • Example: long nationalDebt = 28000000000000L;

  • Floating-Point Types (for numbers with decimal points):

    • float:

      • Size: 4 bytes (32 bits)

      • Default Value: 0.0f (note the 'f' suffix)

      • Usage: Used for single-precision floating-point numbers. It's less precise than double.

      • Example: float temperature = 98.6f;

    • double:

      • Size: 8 bytes (64 bits)

      • Default Value: 0.0d (or just 0.0)

      • Usage: Used for double-precision floating-point numbers. This is the default choice for decimal values and offers higher precision.

      • Example: double pi = 3.1415926535;

Character Data Type

  • char:

    • Size: 2 bytes (16 bits)

    • Range: ′0˘000′ (or 0) to ′\uffff′ (or 65,535)

    • Default Value: ′0˘000′ (null character)

    • Usage: Stores a single Unicode character. Characters are enclosed in single quotes.

    • Example: char grade = 'A';

Boolean Data Type

  • boolean:

    • Size: Varies (not precisely defined by JVM, but typically 1 bit logically)

    • Values: true or false

    • Default Value: false

    • Usage: Used for conditions that are true or false.

    • Example: boolean isOpen = true;

Non-Primitive (Reference) Data Types

Non-primitive data types are not predefined by Java and are created by the programmer (except for String). They don't directly store the value but rather a reference (memory address) to the object. When you declare a variable of a non-primitive type, you are essentially creating a pointer to an object in memory.

Key characteristics:

  • They are created using the new keyword (except String literals).

  • They have a default value of null.

  • They can be used to call methods to perform certain operations.

Common non-primitive data types include:

Strings

  • A String is a sequence of characters. Unlike primitive types, String is a class in Java (java.lang.String).

  • String objects are immutable, meaning their value cannot be changed after creation. Any operation that appears to modify a String actually creates a new String object.

  • Example: String message = "Hello, Java!";

Arrays

  • An array is an object that holds a fixed number of values of a single data type.

  • The size of an array is determined when it is created and cannot be changed.

  • Example: int[] numbers = {1, 2, 3, 4, 5}; String[] names = new String[10];

Classes and Objects

  • These are user-defined data types. When you create a class, you are defining a blueprint for objects.

  • An object is an instance of a class.

  • Example:

    class Dog {
        String breed;
        String name;
    }
    
    // In another part of your code:
    Dog myDog = new Dog(); // myDog is an object of type Dog
    myDog.name = "Buddy";

Interfaces

  • Similar to classes, but an interface can only contain abstract methods and constants.

Type Conversion (Casting)

Sometimes, you need to convert a value from one data type to another. This process is called type casting.

Widening Conversion (Implicit/Automatic)

  • Converting a smaller data type to a larger data type.

  • This is done automatically by Java because there's no loss of data.

  • Example: int myInt = 10; double myDouble = myInt; // myDouble is now 10.0

  • Order: byte -> short -> char -> int -> long -> float -> double

Narrowing Conversion (Explicit/Manual)

  • Converting a larger data type to a smaller data type.

  • This requires explicit casting using parentheses () because there might be a loss of data or precision.

  • If the value is too large for the target type, it can result in an incorrect value.

  • Example: double myDouble = 9.78; int myInt = (int) myDouble; // myInt is now 9

  • Example with potential data loss: int bigInt = 300; byte smallByte = (byte) bigInt; // smallByte will be 44 (due to overflow)

Key Takeaways

  • Primitive (simple data) and non-primitive/reference (references to objects)

  • Type casting is the act of changing the data type of a variable

Quiz