Unit 1 - Reference Types

Intro Primitive Types Reference Types Stack and Heap Code Example Quiz

Refrence Types

Reference types in Java include classes, arrays, and interfaces. Unlike primitive types, reference types store addresses (references) to objects rather than the objects themselves.

Classes

  • Create complex data structures by grouping variables and methods.

Example:

class Person {
    String name;
    int age;
}
Person person = new Person(); // `person` reference in stack, `Person` object in heap

Arrays

  • Collections of variables of the same type.

Example:

int[] numbers = new int[5]; // `numbers` reference in stack, array in heap

Popcorn Hack

public class Main {
    public static void main(String[] args) {
        // Create a reference type variable of type String
        String myString = "Hello, World!";
        
        // Create a reference type variable of type Array
        int[] myArray = new int[] {1, 2, 3, 4, 5};

        // Print the values
        System.out.println(myString);
        System.out.println(Arrays.toString(myArray));
    }
}

Main.main(null);
Hello, World!
[1, 2, 3, 4, 5]

Comments

You are seeing this because your Disqus shortname is not properly set. To configure Disqus, you should edit your _config.yml to include either a disqus.shortname variable.

If you do not wish to use Disqus, override the comments.html partial for this theme.