Comparing Objects

Learn how to compare objects in Java.

Introduction

Comparing objects is a little different than comparing primitive type values, like numbers. Objects can be very complex and can have many attribute values or instance variables inside them.

Suppose we have a Person object. It includes the variables: name, address, phone, and age. To compare two Person objects, we can’t simply use the == or != operator. We have to design an equal method that compares the objects because comparing the objects means comparing their instance variables and values.

Get hands-on with 1200+ tech skills courses.