CodeGym /Courses /Java Multithreading /All of the Object class's methods, plus more on the toStr...

All of the Object class's methods, plus more on the toString() method

Java Multithreading
Level 1 , Lesson 3
Available
All of the Object classs methods, plus more on the toString() method - 1

"Hi, Amigo!"

"Hi!"

"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:"

java.lang.Object@12F456

"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;
System.out.println(result);
Student st = new Student("Vincent");
System.out.println("Student is " + st);

Student st = new Student("Vincent");
String result = "Student is " + st.toString();
System.out.println(result);
Car car = new Porsche();
System.out.println("My car is " + car);

Car car = new Porsche();
String result = "My car is " + car.toString();
System.out.println(result);

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

Comments (5)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
yehuda b Level 23, Beersheba, Israel
24 December 2020
"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." "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 they added them to the Object class." fixed it. I see Daniel Tinsley pointed it out already.....
Ruslan Skaldin Level 33, Tashkent, Uzbekistan
14 December 2020
For those who first encountered "native" keyword in Java. It marks a method, that it will be implemented in other languages, not in Java. It works together with JNI (Java Native Interface). A method that is native is implemented in platform-dependent code, typically written in another programming language such as C, C++, FORTRAN,or assembly language. The body of a native method is given as a semicolon only, indicating that the implementation is omitted, instead of a block.
Johannes Level 27, Centurion, Pretoria, South-Africa
24 April 2020
At least this makes me get hope for programming in Java again. The first lesson with the 3 operator exercises went totally over my head.
Daniel Tinsley Level 22, United States, United States
30 January 2020
In the fifth paragraph: "every class should have and added they them to the Object class" should be: "every class should have and added them to the Object class"
Seb Level 41, Crefeld, Germany
14 January 2020
haha - yeah, I use that one regularly too - very convenient for debugging... :-)