Java four kinds of references, creating objects 5 ways, object method

Source: Internet
Author: User

one or four kinds of references

References to Java objects include

Strong references, soft references, weak references, virtual references

There are two main purposes for providing these four types of references in Java:


The first is to allow programmers to determine the life cycle of certain objects through code;


the second is to facilitate the JVM for garbage collection.


Two, there are 5 ways to create objects in Java

There are 5 ways to create objects in Java, and here are examples of them and their bytecode

Use the New keyword }→ called the constructor
The Newinstance method using class classes }→ called the constructor
Newinstance method using the constructor class }→ called the constructor
Using the Clone method }→ did not call the constructor
Using deserialization }→ did not call the constructor

three, object common use method


1. Clone method

The protection method, realizes the object the shallow duplication, only then realizes the Cloneable interface may call this method, otherwise

Throws a Clonenotsupportedexception exception.


Mainly in Java in addition to 8 basic types of parameters is the value of transfer, the other class object pass parameters are reference

Hand, we sometimes do not want to change the parameters in the method, this is the need to copy the Clone method in the class.


2. GetClass Method

Final method to obtain the Run-time type.


3. ToString Method

This method is used more often, and the general subclasses are covered.


4. Finalize Method

This method is used to free resources. Because it is not possible to determine when the method is invoked, it is rarely used.


5. Equals Method

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


6. Hashcode Method

This method is used for hashing lookups, reducing the number of equals used in lookups, and overriding the Equals method generally overrides the Hashcode method. This method is used in some collection that have a hash function.

General must meet Obj1.equals (OBJ2) ==true. Can launch Obj1.hash-code () ==obj2.hashcode (), but hashcode equal does not necessarily satisfy equals. However, in order to improve efficiency, we should try to make the above two conditions close to equivalent.

If you do not rewrite hashcode (), add two equals objects to the HashSet, and all two objects are added.


7. Wait Method

The wait method is to keep the current thread waiting for the lock on the object, and the current thread must be the owner of the object, that is, the lock with the object. The wait () method waits until a lock is obtained or is interrupted. Wait (long timeout) sets a time-out interval that is returned if the lock is not obtained within the specified time.

When this method is invoked, the front thread goes to sleep until the following events occur.

(1) Other threads invoke the Notify method of the object.

(2) Other threads invoke the Notifyall method of the object.

(3) Other threads call the interrupt interrupt the thread.

(4) The time interval is up.

The thread can then be dispatched, and a Interruptedexception exception is thrown if it is interrupted.


8. Notify Method

This method wakes a thread that is waiting on the object.


9. Notifyall Method

This method wakes all threads that are waiting on the object.

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.