A detailed description of the object class function in Java

Source: Internet
Author: User
Tags shallow copy

1. Clone method(Shallow copy)

Protection method, implement shallow copy of object, only implement the Cloneable interface can call this method, otherwise throw clonenotsupportedexception exception.

Basically Java in addition to 8 basic types of parameters is the value of the transfer, the other class object parameters are reference passing, we sometimes do not want to say in the method parameter changes, this is the need to replicate the Clone method in the class.

2. GetClass method

Final method, which gets the run-time type.

3. ToString Method

This method is used more and the general subclass has coverage.

4. Finalize method

This method is used to release resources. It is seldom used because it is not possible to determine when the method is invoked.

5. Equals method

This method is a very important method. The general equals and = = are not the same, but the two are the same in object. Subclasses generally have to override this method.

6. Hashcode method

This method is used for hash lookups, which reduces the number of equals used in lookups, overriding the Equals method, which generally overrides the Hashcode method. This method is used in some collection with hash functions.

The Obj1.equals (OBJ2) ==true must be met in general. Obj1.hash-code () ==obj2.hashcode () can be introduced, but hashcode equality does not necessarily satisfy equals. However, in order to improve efficiency, we should try to make the above two conditions nearly equivalent.

If you do not rewrite hashcode (), adding two equals objects to the HashSet will add two objects.

7. Wait method

The wait method is to have the current thread wait for the lock on the object, which must be the owner of the object, which is the lock that owns the object. The wait () method waits until the lock is acquired or interrupted. Wait (long timeout) sets a time-out interval that returns if the lock is not acquired within the specified time.

When the method is called, the thread goes to sleep until the following event occurs.

(1) The Notify method of the object is called by other threads.

(2) The Notifyall method of the object is called by other threads.

(3) Another thread called Interrupt interrupts the thread.

(4) The time interval is up.

At this point the thread can be dispatched, and if it is interrupted, throws a Interruptedexception exception.

8. Notify method

The method wakes up a thread waiting on the object.

9. Notifyall method

The method wakes up all the threads waiting on the object.

--------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------

Details:

Java's getclass () function

Java Reflection Learning

The so-called reflection can be understood as the operation of obtaining the object type information during the run time. Traditional programming methods require programmers to decide which type to use during the compilation phase, but with the help of reflection, programmers can dynamically obtain this information to write more portable code. Strictly speaking, reflection is not a feature of programming languages, because the reflection mechanism can be implemented in any language, but the implementation of reflection is much more convenient if the programming language itself supports reflection.

1, Get type class

We know that in Java everything is an object, and we generally use objects that are directly or indirectly inherited from the object class. The object class contains a method called GetClass, which allows you to obtain an instance of the type class. A type class refers to a class that represents a type, because everything is an object, and the type is no exception, and in Java the Type class is used to represent a type. All type classes are instances of class classes. For example, there is the following code:

A = new A ();

if (A.getclass () ==a.class)

System.out.println ("equal");

else System.out.println ("unequal");

The result is a print "equal".

As you can see, object A is an instance of a, a class where the result of using A.getclass () in an If statement is the type class of a, and in Java it means that a type class of a particular type can be obtained by using the "type. Class" method, because A.getclass () Get the type of a class, which is a.class, so the result of the above code execution is to print out "equal". It is particularly important to note that the type class is one by one corresponding to the type class of the parent class and the subclass is different, so if a is a subclass of B, then the following code will get the output of "unequal":

A = new A ();

if (A.getclass () ==b.class)

System.out.println ("equal");

else System.out.println ("unequal");

So, if you know an instance, you can get the type class of the object by using the "GetClass ()" Method of the instance, and if you know a type, you can use the ". Class" method to get the type class for that type.

2. Get the type of information

After you get the type class, you can call some of these methods to get the type of information, the main methods are:

GetName (): String: Gets the full name of the type.

Getsuperclass (): class: Gets the direct parent of the type, and returns null if the type does not have a direct parent class.

Getinterfaces (): class[]: Gets all interfaces implemented by the type.

IsArray (): Boolean: Determines whether the type is an array.

Isenum (): Boolean: Determines whether the type is an enumeration type.

Isinterface (): Boolean: Determines whether the type is an interface.

Isprimitive (): Boolean: Determines whether the type is a basic type, that is, int,boolean,double, and so on.

IsAssignableFrom (Class CLS): Boolean: Determines whether this type is a parent (ancestor) class or parent (ancestor) interface of type CLS.

Getcomponenttype (): Class: If the type is an array, then the component type of the array is returned.

An explanation of the equals and Hashcode methods in Java

http://blog.csdn.net/fenglibing/article/details/8905007

http://blog.csdn.net/jiangwei0910410003/article/details/22739953

A detailed description of the object class function in Java

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.