Unchanged Java class

Source: Internet
Author: User

The unchanged class means that the class will not change, that is, the class whose status will not change after the class instance is created.

 

For example, if a person is a class, every one of us is a specific instance of the class. If a person has only one state of being a parent, so it is a constant class, because every person is born at that moment, the birth of the parent has been set, and will not change for life.

What are the benefits of a constant class?

1) The unchanged class is thread-safe. Since the status of the unchanged class does not change after creation, it can be shared among threads without synchronization.

2) the instance of the unchanged class can be reuse

 

It takes CPU time to create a class instance. When this instance is no longer referenced, it will be reclaimed by garbage collection. At this time, it will take CPU time. For unchanged classes, you can cache common instances to reduce object creation. For example, true and false are the most common values for a boolean type. The Boolean class in JDK is a constant class, and the two instances are buffered.

Public final class Boolean implements java. Io. serializable {

/**

* The <code> Boolean </code> object corresponding to the primitive

* Value <code> true </code>.

*/

Public static final Boolean true = new Boolean (true );

/**

* The <code> Boolean </code> object corresponding to the primitive

* Value <code> false </code>.

*/

Public static final Boolean false = new Boolean (false );

// This method does not create a new object, but reuse the created instance.

Public static Boolean valueof (Boolean B ){

Return (B? True: false );

}

}
3) Some methods of the same class can cache the calculation results.

The hashcode method is derived from the object class. This method is used to return the hashcode of an object. It is mainly used to store an object in hashtable to determine the storage location of the object. For an instance of the same class, its hashcode is also unchanged, so the computing results can be cached to improve performance and avoid unnecessary operations, the string class in JDK is an example.

Public final class string {

/** Cache the hash code for the string */

Private int hash; // default to 0

Public int hashcode (){

Int H = hash;

If (H = 0 ){

// Compute the value

Hash = H; // cache the value
}
Return h;
}
}

In JDK, string, the primitive Wrapper Classes, and biginteger and bigdecimal are all unchanged classes.

If a class is a constant class, cannot this class change its status?

The answer is no, of course. string is a constant class, and there are still replace, replaceall methods, while string is still a constant class, because in these methods of changing the state, each time, A New String object is created.

If you understand the unchanged class, it is not difficult to understand why stringbuffer should be used instead of the + operator when constructing the concatenate of string.

How to use string correctly?

1) do not use new to create a String object.

If you use new to create a string, a new object is created each time.

Public static void main (string [] ARGs ){

String a1 = "";

String a2 = "A"; // it won't create a new object

Checkinstance (a1, a2); // result: they are same instances

String b1 = new string ("A"); // create a new object

String b2 = new string ("A"); // creat a new object

Checkinstance (b1, b2); // result: they are different instances

}

Private Static void checkinstance (string A1, string A2 ){

If (a1 = a2 ){

System. Out. println ("they are same instances ");

} Else {

System. Out. println ("they are different instances ");

}

}

2) stringbuffer should be used for connection operations

Because string is a constant class, a temporary object will be created during the connection operation to save the intermediate calculation result, while stringbuffer is a mutable class, in this way, you do not need to create a temporary object to save the results, thus improving the performance.

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.