Object and Objects in Java

Foreword

All classes in Java ultimately inherit from Object. Well, Object What marvelous place, what are the methods or properties it does?

In addition, there are no tools can simplify some operations that we use very high frequency of it? For example: Analyzing null, comparison operations, string operations and the like.

The originator of Java objects - Object

1. getClass()

public final native Class<?> getClass();. Getting run object class objects, class objects the object is to describe the object's class. This method is usually Java reflection and with use.

2. hashCode()

public native int hashCode();. Gets the object hash value, the hash value is mainly used in the hash table. In Java, the most commonly implemented as a hash table java.util.HashMap. Object of this method returns the default heap memory address of the object.

3. equals()

public boolean equals(Object obj) {
    return (this == obj);
}

The method used to compare two objects, if the two object references point to the same object, it returns true, otherwise false.

4. clone()

protected native Object clone() throws CloneNotSupportedException;

This method for cloning an object. The cloned object must implement java.lang.Cloneablethe interface, otherwise it will throw an CloneNotSupportedExceptionexception.

The default clone method 浅拷贝模式. The so-called shallow copy, refers to the object within the object attribute references cited only copy the address, the object will not re-allocate memory references. Deep copy the object reference will not even be re-created.

5. toString()

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

The method of this frequency is very high, used to return a string object can represent.

The default response format is as follows: 对象的class名称+ @+hashCode的十六进制字符串

6. finalize()

When this method is used in the GC again is called, if we implement this method, the object may be again in this process 复活, so as to avoid being recovered GC.

Under normal circumstances, we do not need to implement this method yourself.

Some students may feel, you can use this method to free up some resources, such as closing the database connection, the object pool object recovery, closed IO streams, and so on. This is often not a good choice, because the GC does not guarantee that this method will be called, is not guaranteed to be called a success.

7. wait() / notify() / notifyAll()

Topic these methods are mainly used for communication between the multi-threaded, and multi-threaded communications is a relatively complex, this article will not introduce too much.

Objects tools - simplify our operations

In JDK7 version of the time, Java introduction of java.util.Objectstools, used to encapsulate some of the usual high frequency of use, or error-prone operations that formed Objects of various methods, we look down to these methods.

1. Objects () Constructor

private Objects() {
    throw new AssertionError("No java.util.Objects instances for you!");
}

This method is not special place, but there are two points to note:

  1. This method is a private method
  2. Its implementation is throwing an exception (or error)

Its main purpose is to avoid configuration object, even by the reflection can not do!

2. equals()

Unlike Object.equals()this method to avoid null pointer exception.

public static boolean equals(Object a, Object b) {
    return (a == b) || (a != null && a.equals(b));
}

3. deepEquals()

Object.equals()Used to compare two object references are the same, but deepEquals()has expanded into an array can support.

 public static boolean deepEquals(Object a, Object b) {
    if (a == b)
        return true;
    else if (a == null || b == null)
        return false;
    else
        return Arrays.deepEquals0(a, b);
}

4. hashCode(Object o)

And Object.hashCode()the like, but when the object is returned null hash value is 0 only.

public static int hashCode(Object o) {
    return o != null ? o.hashCode() : 0;
}

5. hash()

A hash value generation target, including arrays.

public static int hash(Object... values) {
    return Arrays.hashCode(values);
}

6. toString()

Ultimately, its internal end up calling the object's toString()methods. Just a null pointer extra judge it.

public static String toString(Object o) {
    return String.valueOf(o);
}
public static String toString(Object o, String nullDefault) {
    return (o != null) ? o.toString() : nullDefault;
}

7. compare()

Used to compare two objects.

public static <T> int compare(T a, T b, Comparator<? super T> c) {
    return (a == b) ? 0 :  c.compare(a, b);
}

8. requireNonNull()

When the object is a null pointer, throws a null pointer exception specific message.

public static <T> T requireNonNull(T obj) {
    if (obj == null)
        throw new NullPointerException();
    return obj;
}
public static <T> T requireNonNull(T obj, String message) {
    if (obj == null)
        throw new NullPointerException(message);
    return obj;
}
public static <T> T requireNonNull(T obj, Supplier<String> messageSupplier) {
    if (obj == null)
        throw new NullPointerException(messageSupplier.get());
    return obj;
}

9. isNull() 和 nonNull()

These two methods for determining the object is null and the object is not null. Under normal circumstances, we will not use these methods directly, but using comparison operators ==and !=. These two methods are mainly used in the flow calculation jdk8 began to support the inside.

public static boolean isNull(Object obj) {
    return obj == null;
}
public static boolean nonNull(Object obj) {
    return obj != null;
}

to sum up

Our comprehensive analysis of the root class Java object - Object. Meanwhile, we also analyzed the most frequently used package into some of the operations of the tools - Objects.

Through this article, we have these two classes have more in-depth understanding.

Reproduced in: https: //www.jianshu.com/p/0235759d8103

Guess you like

Origin blog.csdn.net/weixin_34405332/article/details/91068211