Skip to main content

Menu

System onlineAI Tutorials / 2026

AI Tutorials
Knowledge search

Recent · Popular · Tutorials

  • dart3 tutorials
  • java1 tutorial
  • Javascript3 tutorials
  • Node.js Introduction2 tutorials
  • dartdart · General · In depth
  • JavascriptJavascript · Advanced · In depth
  • JavascriptJavascript · Advanced · Mid
  • JavascriptJavascript · Advanced · Short

Find a tutorial

Tell the assistant what you want to learn. It suggests published chapters.

For example: “I want to learn Rust ownership from the start.” The conversation is kept only in this tab.

0 / 2,000 characters

Enter to send, Shift+Enter for a new line. Do not include personal information.

Chapter 3

The Object class, references, and garbage collection

You already know how to write classes and methods. This chapter takes a quick tour of the tools Java gives you once those basics are in place.
Level: GeneralLength: Short2 min read

Java's Bigger Picture: Objects, Data, and Handling the Unexpected

You already know how to write classes and methods. This chapter takes a quick tour of the tools Java gives you once those basics are in place.

Everything is an Object

In Java, every class is part of one big family. The root of the family tree is a class called Object. It is the built-in class that all other classes inherit from, directly or indirectly. That's why you can store any object in a variable of type Object, and why every object comes with shared methods like toString(), which turns it into readable text.

An important detail: a variable does not hold the object itself. It holds a reference — like a price tag pointing to a shelf item. Two variables can point to the same object. When nothing points to an object anymore, Java quietly clears it away. This automatic cleanup is called garbage collection.

Enums, Records, and Exceptions

  • An enum is a type with a fixed list of values, like Day.MONDAY. Use it when a value can only be one of a few choices.
  • A record is a short way to define a class that just holds data, like record Point(int x, int y) {}.
  • An exception is Java's way of reporting a problem at runtime, such as a missing file. You catch exceptions to handle them gracefully instead of crashing.

Collections, Generics, and Files

  • The collections framework is a set of ready-made containers: List (ordered items), Set (no duplicates), and Map (key-to-value pairs).
  • Generics let you say what type goes inside: List<String> is a list that only holds strings. The compiler checks this for you.
  • Files are read and written with classes from the java.nio.file package.

Lambdas, Streams, and Concurrency

A lambda is a short, nameless function, like x -> x * 2. A stream is a pipeline that sends data through steps — filter it, transform it, collect it. Concurrency means doing several things at once using threads, usually managed for you by an executor service, which is a helper that runs tasks on a pool of worker threads.

caption: How these Java features build on objects

alt: A flow chart showing objects feeding into enums, records, and exceptions; generics feeding collections; collections and lambdas feeding streams; exceptions feeding files and concurrency.

Exercise

Why does Java garbage-collect objects automatically?

Show answer

Because when no references point to an object anymore, it can never be used again, so Java frees its memory for you without you having to delete it manually.

Spotted a mistake, or code that does not work?

Report an issue

Chapter: The Object class, references, and garbage collection

0 / 2,000 characters

Plain text, for example: the second code sample does not compile. Do not include personal information.

You can't do that