Core Java · Chapter 20 of 48

The Object class and memory

By the end of this chapter you can decide when two objects count as the same, make hash sets and maps find them, print them for people, and tell which objects the garbage collector may take. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Objects
  • 5 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Core Java are free. This one comes with the all labs plan.

Why you need this

Anjali launched the loyalty card on Monday: every tenth chai is free. By Wednesday Ravi has complaints. A man has bought thirty chai at SBC and the system calls him a new customer every time. The code keeps regulars in a HashSet<Customer>, and every lookup builds a fresh Customer from the phone number the customer reads out. The set never finds him.

With what you know so far, two objects are only "the same" when they are one object: == compares references (chapter 10). Every lookup builds a new object, so it never matches. You could loop over every regular comparing phone numbers by hand, but then HashSet, HashMap and contains are no use to you.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Customer.java: make two customers with the same phone equal

    In Customer.java, make equals(o) return true when o is a Customer with the same phone number; names do not matter. It must work both ways, a customer equals itself, and a customer is never equal to null or to an object of another class, not even a String holding the same digits. The parameter stays an Object.

    For example, Ravi on 9845012345 equals Ravi Kumar on 9845012345 (and the other way round), Meera on 9123456780 equals neither, and Ravi's equals(null) and equals("9845012345") are false.

  2. Task 2 · interview question

    Interview: Customer.java, fix the HashSet that loses a customer

    In Customer.java, make hashCode() return a number built only from the phone, the field equals uses, so two equal customers always return the same hash code, whatever their names. Then a HashSet or a HashMap finds a customer from a new object with the same phone. This needs task 1.

    For example, a HashSet given Ravi on 9845012345, Meera on 9123456780 and Ravi Kumar on 9845012345 holds 2 customers and contains a new customer R. on 9845012345; a HashMap that counts two visits by Ravi, each with a new object, holds 2 under one key.

  3. Task 3

    Customer.java: show a customer with a masked phone number

    In Customer.java, make toString() return the name, then " (phone ending ", then the last four digits of the phone, then ")". Joining a customer to a string uses the same text, and after a rename it shows the new name.

    For example, Ravi on 9845012345 gives Ravi (phone ending 2345); Meera on 9123456780 renamed to Meera S gives Meera S (phone ending 6780).

  4. Task 4

    Customer.java: predict what two references to one customer show

    In Customer.java, make predictReferences() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them (true or false in lower case).

    The check runs the same code, with your equals from task 1, and compares your list with what Java really printed, so finish task 1 first.

  5. Task 5

    Customer.java: say which customers the garbage collector may take

    In Customer.java, make collectable() return the names of the customers that nothing can reach any more at the line marked here in its comment, in any order. Only those may be taken by the garbage collector.

What you will learn

  • Object: the parent of every class, and the equals, hashCode and toString you inherit from it.
  • equals and hashCode, as a pair: the contract hash sets and maps rely on.
  • toString: what an object prints as.
  • Stack, heap and garbage: where variables and objects live, and when an object can be collected.

More in Objects

Build The Object class and memory yourself

Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Core Java are free. This one comes with the all labs plan.