Java Tutorial » Chapter 5 — Basic Datatypes

Chapter 5 — Java Basic Datatypes

Learn about the fundamental building blocks of data in Java: Primitive and Reference types.

1. Introduction

What is a Datatype?

A datatype tells the Java compiler what kind of data a variable can hold. It determines the size of memory allocated for the variable and the operations that can be performed on it.

Java is a strongly-typed language, which means you must declare a variable with its datatype before you can use it.

Java's datatypes are broadly divided into two categories:

  • Primitive Datatypes: The most basic, built-in types.
  • Reference Datatypes: Types that refer to objects.
2. Primitive Datatypes

The 8 Building Blocks of Java

There are eight primitive datatypes in Java. They are not objects and hold values directly. Let's explore them.

Integer Types (for whole numbers)

Type Size Range Use Case
byte 1 byte -128 to 127 Saving memory in large arrays.
short 2 bytes -32,768 to 32,767 Also for saving memory.
int 4 bytes -2,147,483,648 to 2,147,483,647 Most common for whole numbers.
long 8 bytes Very large range When int is not big enough.

Floating-Point Types (for numbers with decimals)

Type Size Precision Use Case
float 4 bytes ~6-7 decimal digits To save memory when precision isn't critical.
double 8 bytes ~15-16 decimal digits Most common for decimal values.

Other Primitive Types

Type Size Values Use Case
boolean 1 bit (not precisely defined) true or false For simple flags or conditions.
char 2 bytes A single character (Unicode) To store any single letter or symbol.

Code Example

public class PrimitiveExample {
    public static void main(String[] args) {
        // Integer types
        byte byteVar = 100;
        short shortVar = 20000;
        int intVar = 123456;
        long longVar = 9000000000L; // Note the 'L' for long

        // Floating-point types
        float floatVar = 3.14f;      // Note the 'f' for float
        double doubleVar = 3.14159;

        // Other types
        boolean boolVar = true;
        char charVar = 'A';

        // Print them out
        System.out.println("Byte: " + byteVar);
        System.out.println("Double: " + doubleVar);
        System.out.println("Boolean: " + boolVar);
        System.out.println("Char: " + charVar);
    }
}
3. Reference Datatypes

Pointing to Objects

Unlike primitive types which hold values directly, a reference datatype variable holds the memory address (or a "reference") of an object.

Think of it like this: a primitive variable is a small box that holds the value itself. A reference variable is a piece of paper that has the address of where the big box (the object) is located.

Key Characteristics:

  • They are created using the new keyword.
  • They can be null, which means they are not pointing to any object.
  • The size of a reference variable is fixed, but the object it points to can be of any size.

Common Reference Types:

  • String: Used for sequences of characters (text).
  • Arrays: A container object that holds a fixed number of values of one type.
  • Class Objects: Any object you create from a class, like a Car or Student object.

Code Example

public class ReferenceExample {
    public static void main(String[] args) {
        // String is a reference type
        String greeting = "Hello, World!";

        // Array is a reference type
        int[] numbers = new int[5];

        // An object of a custom class is a reference type
        // Let's assume the Car class from Chapter 4 exists
        Car myCar = new Car("Red", "Toyota");

        // A reference variable that points to nothing
        Car anotherCar = null;

        System.out.println(greeting);
        System.out.println(myCar.model); // Accessing object members
    }
}
4. Java Literals

Fixed Values in Code

A literal is a source code representation of a fixed value. They are written directly into the code. For example, `100`, `3.14`, `'a'`, and `"Hello"` are all literals.

Types of Literals

  • Integer Literals: Whole numbers. By default, they are of type int. You can specify long by adding L or l.
    int decimalVal = 100;     // Decimal
    int octalVal = 0144;      // Octal (starts with 0)
    int hexVal = 0x64;         // Hexadecimal (starts with 0x)
    long longVal = 10000000000L; // Long literal (ends with L)
  • Floating-Point Literals: Numbers with a decimal point. By default, they are of type double. You can specify float by adding F or f.
    double doubleVal = 3.14;    // Double literal
    float floatVal = 3.14f;     // Float literal (ends with f)
    double scientificVal = 5.2e10; // Scientific notation
  • Character Literals: A single character enclosed in single quotes.
    char letter = 'A';
    char unicodeChar = '\u0041'; // Also 'A', using Unicode
    char newline = '\n';           // Escape sequence for newline
    char tab = '\t';              // Escape sequence for tab
  • String Literals: A sequence of characters enclosed in double quotes.
    String message = "This is a string literal.";
    String path = "C:\\Users\\YourName"; // Use backslash to escape a backslash
  • Boolean Literals: Can only be true or false.
    boolean isFun = true;
    boolean isHard = false;
  • The null Literal: Represents a default value for any reference variable. It means the variable is not referring to any object.
    Car myCar = null; // myCar does not point to any Car object yet