You are currently viewing Java: Everything You Need to Know About the Object Class

Java: Everything You Need to Know About the Object Class

Java is a powerful object-oriented programming language that provides a robust foundation for developing applications. At the core of Java’s class hierarchy lies the Object class. Every class in Java implicitly inherits from the Object class, making it the root of the class hierarchy. Understanding the Object class is essential for mastering Java programming. In this blog post, we will explore the Object class and provide code examples for its important methods.

The toString() Method

The toString() method returns a string representation of an object. By default, it returns the fully qualified class name along with the object’s hash code. However, it is recommended to override this method in your own classes to provide a more meaningful representation. Here’s an example:

class Person {

    private final String name;
    private final int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person {name: " + name + ", age: " + age + "}";
    }

}

public class Main {

    public static void main(String[] args) {

        Person person = new Person("John Doe", 25);
        
        // Output: Person {name: John Doe, age: 25}
        System.out.println(person.toString());
        
    }

}

The equals() Method

The equals() method is used to compare the equality of two objects. The default implementation provided by the Object class checks for reference equality. It is often necessary to override this method in your classes to define your own equality criteria. Here’s an example:

class Point {

    private final int x;
    private final int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean equals(Object obj) {

        if (this == obj)
            return true;

        if (obj == null || getClass() != obj.getClass())
            return false;

        Point other = (Point) obj;

        return x == other.x && y == other.y;

    }

}

public class Main {

    public static void main(String[] args) {

        Point pt1 = new Point(2, 3);
        Point pt2 = new Point(2, 3);

        // Output: true (because the xy values are the same)
        System.out.println(pt1.equals(pt2));

    }

}

The hashCode() Method

The hashCode() method returns a hash code value for an object. The default implementation in the Object class computes the hash code based on the object’s memory address. It is advisable to override this method when overriding equals() to maintain the contract between these two methods. Here’s an example:

import java.util.Objects;

class Book {

    private final String title;
    private final String author;

    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    @Override
    public int hashCode() {
        return Objects.hash(title, author);
    }

}

public class Main {

    public static void main(String[] args) {

        Book book = new Book("Core Java, Volume I Fundamentals", "Cay S. Horstmann");

        // Output: 986324813 (May vary on your computer)
        System.out.println(book.hashCode());

    }

}

The getClass() Method

The getClass() method returns the runtime class of an object. It is often used to perform runtime type checks or obtain class-related information. Here’s an example:

class Car { }

public class Main {

    public static void main(String[] args) {

        Car car = new Car();

        // Get the car object class name
        String carObjectClassName = car.getClass().getName();

        // Output: Car
        System.out.println(carObjectClassName);
        
    }

}

Conclusion

The Object class forms the foundation of the Java class hierarchy and provides several important methods that every Java programmer should be familiar with. In this blog post, we covered the toString(), equals(), hashCode(), and getClass() methods with code examples. By understanding and utilizing these methods effectively, you can enhance the functionality and behavior of your Java classes. Remember to consult the Java documentation for more information on the Object class and its methods.

I hope you found this information informative. If you would like to receive more content, please consider subscribing to our newsletter!

Leave a Reply