JAVA Object & Objects Class

Overview

JAVA provides us with lots of in-built classes for the easiness and faster productivity of the developers. Object and Objects class are two out of these in-built classes. In this blog, We will see what is the significance of these two classes.

Object Class

As per Java docs, Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class. This class exist since Java 1.0.

Package : java.lang.Object

Object.PNG

Let's decode the above definition with an example:

public class ObjectClass {
    public static void main(String[] args) {
        User userObject = new User("Harsh", "Noida");
        User userDupObject = new User("Harsh", "Noida");
        System.out.println(userObject.equals(userDupObject));
        System.out.println(userObject.hashCode());
        System.out.println(userObject.toString());
    }
}
class User{
    String userName;
    String userCity;

    public User(String name , String city){
        userName = name;
        userCity = city;
    }
}

We can notice the fact that our User class object userObject in the above code snippet can use the methods like equals(), hashCode(), toString() without their implementation in the User class. Hence, the User class is extending the Object class implicitly without actually extending it explicitly.

The output of the above program will be:

false //false,because objects belong to different instances with different hashCodes
460141958 //hashCode of userObject 
com.dedug.java.User@1b6d3586 // strange hashCode output because we did not over-ride toString method in our User class

Now many of us will get doubt why we are getting false from equals() even after having the same values for the objects. For getting the desired results from the Object class methods we need to over-ride them in our User class. But that is not the concern of this blog, Checkout the blog - Overriding equals()/hashCode()/toString().

Objects Class

As per Java docs, This class consists of static utility methods for operating on objects. These utilities include null-safe or null-tolerant methods for computing the hash code of an object, returning a string for an object, and comparing two objects. This class exists since Java 1.7 or Java 7.

Package : java.util.Objects

What is the null-safe or null-tolrant method? There can be instances when the value of one object or both objects can be null when comparing it using equals() method. For example :

Object o1 = new Object(null);
Object o2 = new Object("Harsh");
o1.equals(o2); //will give us NullPointerException
Objects.equals(o1,o2); //This object class method is null-safe and will not throw NullPointerException

Hence a method that is capable of processing null values also without throwing NullPointerException is called a null-safe or null-tolerant method.

Object class gives us all these utility methods. Some Objects class methods are:

  1. equals(): Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned.
  2. isNull() : Returns true if the provided reference is null otherwise returns false.
  3. nonNull() : Returns true if the provided reference is non-null otherwise returns false.
  4. hashCode() : Returns the hash code of a non-null argument and 0 for a null argument.
  5. hash(): Generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array was hashed by calling Arrays.hashCode(Object[]).
  6. toString() : Returns the result of calling toString() for a non-null argument and "null" for a null argument.

You can check out more about Objects class Here

Conclusion

In this blog article, we discussed the functionality and use of Java Object and Objects classes. Going further the point we should remember are:

  • Every Java class implicitly extends the Objects class and can use its methods.

  • Objects class provides us with the utility methods that are null-safe or null-tolerant. Hence, we can use the methods of this class for less coding and to avoid NullPointerException.