14

As we all know,Object is the root class in Java. I found a class named Objects that looks quite similar toObject.

TheisObjects class has confused me for a while. Can anybody tell me when and where we should use the Objects class?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
andyqee
  • 3,175
  • 3
  • 21
  • 24
  • 8
    The Javadoc is pretty helpful - http://docs.oracle.com/javase/7/docs/api/java/util/Objects.html. – Perception Nov 28 '12 at 08:56
  • 8
    i am waiting for someone to submit "This class consists of static utility methods for operating on objects." as an answer and get a wandering users upvote along with undeserved rep. – Alex Lynch Nov 28 '12 at 08:58
  • 2
    @AlexLynch: I know that trivial answers get most rep on here, but how exactly is this undeserved? Do you know of a duplicate that this should be closed? Is the answer not worth being given, because it's "too simple"? – Joachim Sauer Nov 28 '12 at 09:04
  • @JoachimSauer the OP put no effort into researching the question and you didn't have to exert any effort to answer it. this is a question suited for google. i am happy that you managed to provide insight on the pluralized class name, and the sub par answers have been removed. your tiny answer earns more rep than nearly all of my fairly insightful answers - maybe i am just sour. – Alex Lynch Nov 28 '12 at 09:11
  • 1
    @AlexLynch: I understand what you mean, and it's sometimes frustrating to write well-thought-out answers and score only 1 or 2 upvotes, but remember that we're here to provide Google-able answers (i.e. we *want* to be what a Google search for "java.util.Objects" finds). The fact that the OP didn't seem to invest a lot of time is not really relevant, I didn't write the answer for him, I'm trying to improve the findability of this information on the internet. Helping the OP is just a positive side-effect. – Joachim Sauer Nov 28 '12 at 09:20
  • @JoachimSauer i understand what you are saying as well, but i do not believe it to hold true for this question. a google search for "java.util.Objects" should lead the user to the description in the API, not here. you've mirrored the API and managed provided basic insight on only a loosely related idea that the OP wasn't looking for. – Alex Lynch Nov 28 '12 at 09:29
  • 2
    I disagree: those who search for this kind of information usually *don't* understand the concept of "utility classes", otherwise they wouldn't *have* this question. So explaining that (and giving a few examples) has values. Also: the API is an excellent source for developers who know how to read it, but for many beginners it's just a heap of unreadable technical jargon. And explaining the same information in different words can help them learn interpret the actual API documentation itself. Don't forget that different skill levels need different resources. – Joachim Sauer Nov 28 '12 at 09:32

3 Answers3

23

Objects simply contains a set of utility methods that are useful in combination with Object instances. Note that it can't be instantiated (it's final and it has no public constructor) and only contains static methods.

The naming schema of putting utility methods in pluralized-name classes is pretty common in the JDK:

  • Collections
  • Arrays (although strictly speaking there is no corresponding Array class)
  • ...

Other libraries also use this scheme, for example Guava:

Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
1

One typical use of Objects class:

public void foo(SomeClass bar) {
    Objects.requireNonNull(bar, "custom msg"); // // Ensure an object is not null.
}

Output when bar is null:

Exception in thread "main" java.lang.NullPointerException: custom msg
    at java.util.Objects.requireNonNull(Unknown Source)
    at com.example.ObjectsUsage.main(ObjectsUsage.java:24)

Another one to construct hashCode from the fields:

@Override public int hashCode() {
return Objects.hash(this.foo, this.bar, this.duh);
}

And the most useful one:

if (Objects.equals(sun, moon)) {
    log("I swear I am in earth");
}
KrishPrabakar
  • 2,824
  • 2
  • 31
  • 44
1

Yes, there is lot of difference of both Object and Objects classes

Object Class

Class Object is the root of the class hierarchy. Every class has Object as a superclass and this class is available since JDK1.0

Class Declaration : public class Object

Package : java.lang.Object

Methods : It has only instance methods as clone(), equals(Object obj), hashCode(), notify(), notifyAll(), toString(), wait() etc.

Objects Class

Objects class is final class and it consists of only static utility methods for operating on objects, Using Objects class methods, one can smartly handle NullPointerException as these are null-safe methods for computing the hash code of an object, returning a string for an object, and comparing two objects. This class is available since JDK1.7

Class Declaration : public final class Objects extends Object

Package : java.util.Objects

Methods : It has only static methods as equals(Object a, Object b), hash(Object... values), isNull(Object obj), nonNull(Object obj), toString(Object o), hashCode(Object o) etc.

Note : If you have JDK1.7 then you can only use Objects class

anandchaugule
  • 901
  • 12
  • 20