All of the Object classs methods, plus more on the toString() method - 1

"Hi, Amigo!"


"Today we're going to study the Object class.
You've already encountered it, and you know that Object is the base class for all classes. It has practically no data, but it does have several methods."

"Why does it need methods? Does anyone really create instances of the Object class?"

"Look at it this way: the methods in the Object class are common across all classes. In other words, Java's creators identified several methods that, in their opinion, every class should have and added they them to the Object class."

"And when combined with polymorphism (the ability to override the Object class's methods in derived classes), this becomes a very powerful tool."

"Let's see what these methods are:"

Method Description
public String toString()
Returns a string representation of the object.
public native int hashCode()
public boolean equals(Object obj)
A pair of methods used to compare objects.
public final native Class getClass()
Returns a special object that describes the current class.
public final native void notify()
public final native void notifyAll()
public final native void wait(long timeout)
public final void wait(long timeout, intnanos)
public final void wait()
Methods for controlling access to an object from different threads. For thread synchronization.
protected void finalize()
This method lets you release native non-Java resources: close files, streams, etc.
protected native Object clone()
This method lets you clone an object: creates a duplicate of the object.

"These methods can be divided into 6 groups. You're already familiar with some of them, and we'll get acquainted with the rest in subsequent lessons."

"For some reason, I'm not seeing anything useful here."

"Amigo! If these methods weren't important, they wouldn't have added them to every single object! So, I'd advise you to look more closely at what these are and why they are needed. If they seem unimportant to you, then you either didn't understand something or didn't understand something correctly."

"OK. I'll listen carefully."

"Let's start with the toString() method.

"This method lets you get a text description of any object. Its implementation in the Object class is very simple:"

return getClass().getName() + "@" + Integer.toHexString(hashCode());

"getClass() and hashCode() are also methods of Object class.
Calling this method typically produce a result like this:"


"And what good is such a description?"

"This description lets you know the class of the object that the method was called on. You can also distinguish between objects; different objects will have different digits after the @ symbol."

"But this method's real value lies elsewhere. This method can be overwritten in any class to return a more detailed or suitable object description."

"But there's more. Because you can get a text representation of each object, Java made it possible for you to implement support for 'adding' Strings to objects.
Check it out:"

Code What really happens
int age = 18;
System.out.println("Age is " + age);
String s = String.valueOf(18);
String result = "Age is " + s;
Student st = new Student("Vincent");
System.out.println("Student is " + st);
Student st = new Student("Vincent");
String result = "Student is " + st.toString();
Car car = new Porsche();
System.out.println("My car is " + car);
Car car = new Porsche();
String result = "My car is " + car.toString();

"Yeah, I use this regularly. Especially when I'm writing a program or looking for bugs. It's a useful operation."