Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Type Casting

sebastianchristopher edited this page Sep 13, 2019 · 2 revisions

Type casting

Whenever our applications save objects to databases, we'll need to override the equals() method, and cast its general Object argument into a more-specific type for comparison.

public class ToDo {

    private int id;
    private String content;
    private int labelId;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ToDo toDo = (ToDo) o;
        return id == toDo.id &&
                labelId == toDo.labelId &&
                Objects.equals(content, toDo.content);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, content, labelId);
    }

That's a lot of code to have to remember each time - fortunately Injellij has a way of doing this for you:

overrideEquals1 overrideEquals2 overrideEquals3 overrideEquals4 overrideEquals5 overrideEquals6

Clone this wiki locally