Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Data Types: Primitive Type And Reference Type

Java has two main categories of data types: primitive types and reference types. Understanding these two categories is essential for working effectively with Java. In this tutorial, we will cover primitive types and reference types in Java, their differences, and usage examples.

  • Primitive Types

Primitive types are the most basic data types in Java. They directly contain their values and are not based on any other data types. Java has eight primitive types:

  • byte: 8-bit integer, ranging from -128 to 127.
  • short: 16-bit integer, ranging from -32,768 to 32,767.
  • int: 32-bit integer, ranging from -2,147,483,648 to 2,147,483,647.
  • long: 64-bit integer, ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • float: 32-bit single-precision floating-point number.
  • double: 64-bit double-precision floating-point number.
  • char: 16-bit Unicode character, ranging from 0 to 65,535.
  • boolean: Represents true or false values.

Example:

public class Main {
    public static void main(String[] args) {
        byte b = 100;
        short s = 10000;
        int i = 1000000000;
        long l = 1000000000000L;
        float f = 3.14f;
        double d = 3.14159265359;
        char c = 'A';
        boolean bool = true;

        System.out.println("byte: " + b);
        System.out.println("short: " + s);
        System.out.println("int: " + i);
        System.out.println("long: " + l);
        System.out.println("float: " + f);
        System.out.println("double: " + d);
        System.out.println("char: " + c);
        System.out.println("boolean: " + bool);
    }
}
  • Reference Types

Reference types are data types based on classes, interfaces, or arrays. They store references (memory addresses) to the actual objects rather than the objects themselves. When you create an object, the Java runtime allocates memory for the object on the heap, and the reference type variable stores the address of that memory. Some common reference types include:

  • Class types: User-defined classes or built-in Java classes, such as String, Integer, ArrayList, etc.
  • Interface types: User-defined or built-in Java interfaces, such as List, Comparable, etc.
  • Array types: Arrays of any data type, including both primitive types and reference types.

Example:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        String str = "Hello, World!";
        ArrayList<Integer> numbers = new ArrayList<>();
        int[] intArray = new int[5];

        numbers.add(1);
        numbers.add(2);
        numbers.add(3);

        intArray[0] = 10;
        intArray[1] = 20;
        intArray[2] = 30;

        System.out.println("String: " + str);
        System.out.println("ArrayList: " + numbers);
        System.out.println("int[]: " + intArray[0] + ", " + intArray[1] + ", " + intArray[2]);
    }
}
  1. Working with primitive data types in Java

    Primitive data types are used to store simple values. Examples include int, float, boolean, etc.

    int intValue = 42;
    float floatValue = 3.14f;
    boolean booleanValue = true;
    
  2. Examples of Java reference data types

    Reference data types include classes, interfaces, arrays, and enums. Example:

    String stringValue = "Hello, Java!";
    Object objectValue = new Object();
    int[] arrayValue = {1, 2, 3};
    
  3. Casting between primitive and reference types in Java

    Casting between primitive and reference types requires explicit casting.

    // Casting from int to Integer (autoboxing)
    int intValue = 42;
    Integer integerValue = (Integer) intValue;
    
    // Casting from Integer to int (unboxing)
    Integer integerValue = 42;
    int intValue = integerValue.intValue();
    
  4. Java wrapper classes for primitive data types

    Wrapper classes provide a way to treat primitive types as objects. Example:

    int primitiveInt = 42;
    Integer wrapperInt = Integer.valueOf(primitiveInt);
    
    // Autoboxing (primitive to wrapper)
    Integer autoboxedInt = primitiveInt;
    
  5. Java autoboxing and unboxing with primitive types and wrappers

    Autoboxing is the automatic conversion of primitive types to their corresponding wrapper classes, and unboxing is the reverse process.

    // Autoboxing
    int primitiveInt = 42;
    Integer wrapperInt = primitiveInt;
    
    // Unboxing
    Integer wrapperInt = 42;
    int primitiveInt = wrapperInt;