Java Lab Documentation

Object

Category:java.lang

The Object class in Java is the root of the class hierarchy. All classes inherit from the Object class, so it is automatically the superclass of all classes.

Method Details

equals

public boolean equals(Object obj)

The equals() method in the Object class compares the calling object to another object. The method returns true if the two objects are equal and false otherwise.

Note: This method is often overridden.

Parameters

NameTypeRequired?Description
objObject

the object to compare to the calling object

Examples

Dog firstDog = new Dog("Fido");
Dog secondDog = new Dog("Max");

System.out.println(firstDog.equals(secondDog)); 

Output:

false

toString

public String toString()

The toString() method returns a String representation of an object.

Note: This method is often overridden.

Examples

Object obj1 = new Object();
System.out.println(obj1.toString());

Output:

java.lang.Object@7a81197d

This is a textual representation of the object. It includes the class, @, and the hash code of the object.