Java Object Detailed

Source: Internet
Author: User

As we all know: In Java, all classes inherit the base class of object, and as we all know, there are several more common methods of object, such as Equals (), Clone (), toString (), we need to overwrite them when we use them, today, We will explore these methods in detail.

void Registernatives ()

This is a native method, called in a static block, to register the Hashcode,wait,notify,notifyall and clone methods locally.

Class<?> GetClass ()

It is also a native method that gets the class of the object.

int Hashcode ()

Native method, generate hash code, which note three points

    • An object is called multiple times in the same process, but its hash code should still be the same. However, when the same object executes in a different process, the hash code may be different. The original Javadoc is as follows

      Whenever it is invoked on the same object more than once during an execution of a Java application, the {@code hashcode} m Ethod must consistently return the same integer, provided no information used in {@code equals} comparisons on the object is modified. This integer need not remain consistent from one execution of a application to another execution of the same application.

    • If two objects return true through Equals (), then their hash code should be the same.

    • If two objects return false through Equals (), then their hash code should be different.

Boolean equals (Object obj)

This is probably one of the most commonly used methods, and we often encounter the question of when to use Equals () and when to use = = in practical applications. In general, the basic data types are used = =, because they can be compared directly to their values, but the composite data type if we use = =, then actually we compare their references, unless they point to the same object, otherwise they cannot be equal, So we tend to conform to the data type generally with equals (), and we should overwrite equals (), because the Equals () method of object is determined by default with = =:

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

Let's take a look at how the string we used to overwrite the Equals () method:

public boolean equals(Object anObject) {    if (this == anObject) {        return true;    }    if (anObject instanceof String) {        String anotherString = (String)anObject;        int n = value.length;        if (n == anotherString.value.length) {            char v1[] = value;            char v2[] = anotherString.value;            int i = 0;            while (n-- != 0) {                if (v1[i] != v2[i])                    return false;                i++;            }            return true;        }    }    return false;}

Here is a very interesting question, please see the following example

    String s1 = "String";    String s2 = "String";    System.out.println("The result is " + s1.equals(s2));

You will find that the return value is true, this is not the same as what we said above, according to our above explanation, this time should return false. In fact, this is because a string pool is maintained in the JVM, and it returns a string in the pool if it already has a string containing the object (unless, of course, you're new to a string object.) So this time the return is true.

Object Clone ()

The native method, which we typically use to copy an object, and use Clone () to make a deep copy (copying all the objects that need to be copied, rather than referring to the original object.) Examples are as follows:

public class Animal implements cloneable {private int height;    private int age;        public Animal (int height, int age) {this.height = height;    This.age = age;    } @Override Public Object clone () throws Clonenotsupportedexception {return super.clone ();                } @Override Public String toString () {return "animal{" + "height=" + height +    ", age=" + Age + '} ';    } public int getheight () {return height;    } public void setheight (int height) {this.height = height;    } public int Getage () {return age;    public void Setage (int.) {this.age = age;    }}public class people implements cloneable {private int height;    private int age;    Private Animal A;        Public people (int height, int age,animal a) {this.height = height;        This.age = age;    THIS.A = A;      } @Override Public Object clone () throws Clonenotsupportedexception {  People p = (people) Super.clone ();        P.A = (Animal) a.clone ();    return p;                } @Override Public String toString () {return "people{" + "height=" + height +    ", age=" + Age + ", a=" + A + '} ';    } public int getheight () {return height;    } public void setheight (int height) {this.height = height;    } public int Getage () {return age;    public void Setage (int.) {this.age = age;    } public void Setanimalage (int a) {this.a.setage (5); }}

Next we call this:

    Animal a1 = new Animal(100,3);    People p1 = new People(173,24,a1);    People p2 = (People) p1.clone();    p2.setAge(26);    p2.setHeight(181);    p2.setAnimalAge(6);    System.out.println(p1.toString());    System.out.println(p2.toString());

So we can finish the deep copy.

String toString ()

The most common method, which we typically use to get the value of an object member variable. Default implementation of object:

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

Native method, multi-threaded when applied, notifies other threads to wait for the end.

void Wait (long), void Wait (long,int), void Wait ()

The native method notifies the thread to wait.

void Finalize ()

Garbage collection. When the JVM determines that an object can be garbage collected, the JVM calls the Finalize () method, but remember that it can only be called once (so if you want the object to be garbage-cleared, you're going to do something in it), but generally you can't rely on doing garbage collection here, in the A usage of finalize () is illustrated in the Java programming idea, which is to judge the object end object through Finalize ().

Java Object Detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.