<img alt="" src="https://secure.leadforensics.com/150446.png " style="display:none;">
Go to top icon

Java Utility Class - Objects

Madhura Oak Nov 12, 2012

Objects Java Technology Java Utility Class

Java 7 has introduced Objects class which provides static utility methods for operating on objects. The Java developers certainly welcome this class as it saves writing few lines of code. These utility methods were earlier available in Guava libraries. Now they are part of the Java API within the utility class Objects.

In this blog post, I’m writing how the methods of Objects class simplify code.

static String toString(Object obj)

This method returns null when the argument is null. When the argument is not null it returns the String returned by its toString() method. Listing 1 shows the code prior to Java 7 and Listing 2 shows the code in Java 7.

Object obj;
...
if(obj == null) {
    return null;
}
else {
    return obj.toString();
}

Listing 1. Code prior to Java 7 - Return null if the object is null otherwise returns result of its toString() method

Object obj;
...
return Objects.toString(obj);

Listing 2. Code in Java 7 - Return null if the object is null otherwise returns result of its toString() method

static String toString(Object obj, String nullDefault)

This method returns the default String value passed as second argument when the first argument is null. When the first argument is not null it returns the String returned by its toString() method. Listing 3 and 4 show the code prior to Java 7 and using Java 7 respectively.

Object obj;
...
if(obj == null) {
    return “Unknown”;
}
else {
    return obj.toString();
}

Listing 3. Code prior to Java 7 - Return “Unknown” if the object is null otherwise returns result of its toString() method

Object obj;
...
return Objects.toString(obj, “Unknown”);

Listing 4. Code in Java 7 - Return “Unknown” if the object is null otherwise returns result of its toString() method

static <T> T requireNonNull(T obj)

This method throws NullPointerException when the argument is null. When it is not null it returns the same argument. This method is designed for parameter validation in methods and constructors. Listing 5 shows the code prior to Java 7 and Listing 6 shows the use of this method.

class Project {
    String name;
    void setName(String name) {
        if(name == null) {
            throw new NullPointerException();
        }
        this.name = name;
    }
}

Listing 5. Code prior to Java 7 – Throw NullPointerException if the name is null

class Project {
    String name;
    void setName(String name) {
        this.name = Objects.requireNonNull(name);
    }
}

Listing 6. Code in Java 7 – Throw NullPointerException if the name is null. If name is not null set the name property

static <T> T requireNonNull(T obj, String message)

This method throws NullPointerException when the first argument is null. The second argument is passed as a message to the NullPointerException. When the first argument is not null it returns the same argument. This method allows to set the exception message. Listing 7 shows code prior to Java 7 and Listing 8 shows the use of this method.

class Project {
    String name;
    void setName(String name) {
        if(name == null) {
            throw new NullPointerException(
                “Name cannot be null”);
        }
        this.name = name;
    }
}

Listing 7. Code prior to Java 7 – Throw NullPointerException if the name is null with the message “Name cannot be null”

class Project {
    String name;
    void setName(String name) {
        this.name = Objects.requireNonNull(name,
            “Name cannot be null”);
    }
}

Listing 8. Code in Java 7 – Throw NullPointerException if the name is null with the message “Name cannot be null”. If name is not null set the name property

static boolean equals(Object a, Object b)

This method compares two objects for equality. When the object references are same or both objects are null this method returns true. When the objects are not identical, this method returns the result of the equals() method of first argument if it is not null. If the first argument is null and the other argument is not null, this method returns false. Listing 9 and 10 show the code prior to Java 7 and the code in Java 7 respectively.

String str1, str2;
...
if(str1 == str2 || (str1 != null && str1.equals(str2))) {

Listing 9. Code prior to Java 7 – Compare two objects for equality

String str1, str2;
...
if(Objects.equals(str1,str2)) {

Listing 10. Code in Java 7 – Compare two objects for equality

static boolean deepEquals(Object a, Object b)

This method returns true if both arguments are deeply equal to each other otherwise it returns false. If both arguments of this method are null, it returns true. If both argument references are not same, both are not null and both are arrays, the result of Arrays.deepEquals() is returned. Listing 11 shows the code prior to Java 7 and Listing 12 shows the use of this method.

int[][] x,y;
...
if(x == y || (x != null && y != null && Arrays.deepEquals(x,y))) {

Listing 11. Code prior to Java 7 – Check if two objects are deeply equal

int[][] x,y;
...
if(Objects.deepEquals(x,y)) {

Listing 12. Code in Java 7 – Check if two objects are deeply equal

staticint <T> int compare(T a, T b, Comparator<? super T> c)

If first two arguments are identical this method returns 0 otherwise it returns the result of compare() method of the Comparator implementation passed as third argument. The Comparator implementation decides whether a NullPointerException is thrown when the objects to be compared are null. Listing 13 and 14 show the code prior to Java 7 and using the Objects class respectively.

int compare(Project project1, Project project2,
        Comparator<Project> comparator) {
    return project1 == project2 ? 0 :
            comparator.compare(project1, project2);
}

Listing 13. Code prior to Java 7 – Compare two projects

int compare(Project project1, Project project2,
        Comparator<Project> comparator) {
    return Objects.compare(project1, project2, comparator);
}

Listing 14. Code in Java 7 – Compare two projects

static int hashCode(Object obj)

If the argument passed to this method is null it returns 0. If the argument is not null, this method returns the hash code of the argument. Listing 15 shows the code prior to Java 7. Listing 16 shows the use of this method.

Object obj;
...
int hashcode = obj == null ? 0 : obj.hashCode();

Listing 15. Code prior to Java 7 – Computing hashcode

Object obj;
...
int hashcode = Objects.hashCode(obj);

Listing 16. Code in Java 7 – Computing hashcode

Similar Blog

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.