class Object(java.lang)
Note: The ancestor class of all Java classes.
private static native void registerNatives();
static {
registerNatives();
}
-
Defines registerNatives native method, register the class implement all the native methods in addition to the method
- native keyword modified method is called localization method is to contact the Java program and the underlying host operating system connection method
-
Method calls the method to register the local static static block of code, the local implementation of the registration process action
-
After the class is loaded, convenient to call localization method, without caring about the realization of the principle underlying
-
If the native method implementation is modified, can be achieved by re-calling reload the updated local registration method
-
public final native Class<?> getClass();
- Acquiring the actual type of the object instance in memory
Example 1: acquiring the actual type of the object
Object obj = new Date();
System.out.println("实例对象实际类型:" + obj.getClass().getName());
//输出结果:实例对象实际类型:java.util.Date
public native int hashCode();
- hash code value object class instance, the value is a value based on certain rules related to the object information maps into.
- Because different values for different hash code of an object instance, can be judged by comparing the hash code value for the same object is a target object.
public boolean equals(Object obj) {
return (this == obj);
}
- Compare the current object and incoming objects for equality and returns true if equal, otherwise false.
- "==" to compare two objects address in memory are the same. In practice, some classes had to rewrite this method, for example: String class, Math class.
protected native Object clone() throws CloneNotSupportedException;
public interface Cloneable {}
- Quickly create a copy of an object (clone).
- The method was modified protected keywords, so the class must inherit from the Object class, and implement the Cloneable interface to use. Otherwise it will throw an exception CloneNotSupportedException.
- A copy of the original object is not a reference to the object, but creates a new object of the same content.
- Although Cloneable interface does not define any method, but must be implemented when the object cloning.
Example 2: Cloning of the object Jane
public class MyClone implements Cloneable {
private String name;
public MyClone(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public static void main(String[] args) throws CloneNotSupportedException {
MyClone obj = new MyClone("小明");
MyClone obj1 = (MyClone) obj.clone();
System.out.println("两个对象是否同一个对象:" + obj.equals(obj1));
//输出结果:两个对象是否同一个对象:false
System.out.println("克隆对象名称:" + obj1.getName());
//输出结果:克隆对象名称:小明
}
}
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
- Gets a string representation of the object, the default is "class full path name @ hexadecimal representation of the hash code value."
public final native void notify();
public final native void notifyAll();
public final native void wait(long timeout) throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos > 0) {
timeout++;
}
wait(timeout);
}
public final void wait() throws InterruptedException {
wait(0);
}
- notify and notifyAll methods used to wake up processes waiting on this object's monitor.
- The method used to set the thread wait wait. To milliseconds timeout denotes the wait, nanos nanoseconds represents the value of the additional wait (in the range between 0 to 999999).
protected void finalize() throws Throwable { }
- Garbage collector ready to release memory when will first call finalize method.
final class Objects(java.util)
Note: This class is final, and its constructor is private. This class represents the other classes can not be inherited or modified.
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
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);
}
//Arrays.deepEquals0方法
static boolean deepEquals0(Object e1, Object e2) {
assert e1 != null;
boolean eq;
if (e1 instanceof Object[] && e2 instanceof Object[])
eq = deepEquals ((Object[]) e1, (Object[]) e2);
else if (e1 instanceof byte[] && e2 instanceof byte[])
eq = equals((byte[]) e1, (byte[]) e2);
else if (e1 instanceof short[] && e2 instanceof short[])
eq = equals((short[]) e1, (short[]) e2);
else if (e1 instanceof int[] && e2 instanceof int[])
eq = equals((int[]) e1, (int[]) e2);
else if (e1 instanceof long[] && e2 instanceof long[])
eq = equals((long[]) e1, (long[]) e2);
else if (e1 instanceof char[] && e2 instanceof char[])
eq = equals((char[]) e1, (char[]) e2);
else if (e1 instanceof float[] && e2 instanceof float[])
eq = equals((float[]) e1, (float[]) e2);
else if (e1 instanceof double[] && e2 instanceof double[])
eq = equals((double[]) e1, (double[]) e2);
else if (e1 instanceof boolean[] && e2 instanceof boolean[])
eq = equals((boolean[]) e1, (boolean[]) e2);
else
eq = e1.equals(e2);
return eq;
}
- The method determines whether the two are equal two objects. If the object is an array, using deepEquals depth comparison method.
public static int hashCode(Object o) {
return o != null ? o.hashCode() : 0;
}
public static int hash(Object... values) {
return Arrays.hashCode(values);
}
//Arrays.hashCode
public static int hashCode(Object a[]) {
if (a == null)
return 0;
int result = 1;
for (Object element : a)
result = 31 * result + (element == null ? 0 : element.hashCode());
return result;
}
- and hashCode method for obtaining an object hash value hash code or a collection of objects. If the object is null, 0 is returned.
- The hash code value array of objects (result = 31 * result + single object hash code value).
public static String toString(Object o) {
return String.valueOf(o);
}
public static String toString(Object o, String nullDefault) {
return (o != null) ? o.toString() : nullDefault;
}
- Get string representing the object, if the object is empty, can be passed through the second return parameter to set the default string.
public static <T> int compare(T a, T b, Comparator<? super T> c) {
return (a == b) ? 0 : c.compare(a, b);
}
- By the comparator is provided to compare the size of two objects, for the same object if the two objects, 0 is returned.
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;
}
- Determining whether the object is empty, if it is empty, then a NullPointerException exception abnormality content settings. If empty, the original object is returned.
public static boolean isNull(Object obj) {
return obj == null;
}
public static boolean nonNull(Object obj) {
return obj != null;
}
- Determine whether the object is empty.