Discover how to create immutable class in java, include the articles, news, trends, analysis and practical advice about how to create immutable class in java on alibabacloud.com
Introduction of Immutable Class
Immutable class: The so-called immutable class means that once the instance of this class has been created, it cannot change its member variable values.
Recently asked why the string was designed to be immutable, then a little confused, the problem has always been like a bug, unexpectedly did not find, did not think, summed up in this.The 1.String is immutableThe string class is final decorated and is not inheritable or modified . When a string variable is assigned a second time, instead of modifying the data at the original memory address, it re- opens a m
This.myarray = Array.clone (); } }The Getter method returns to the image clone and does not return itselfString ObjectPublic final class String implements Java.io.Serializable, ComparableApplies to constant poolsThread Safetyclass loader to use strings, immutable classes provide security for proper loading for example, if you want to load the Java.sql.Connection
1. PrefaceRecently, I've seen several interesting questions about the Java Core class string.
The string class is how to implement its immutable attributes, and the benefits of being designed to be immutable.
Why is it not recommended to use the + sign to form a
()); System.out.println ("B:" +b.getdetail () +b.getpostcode () +B.hashcode ()); System.out.println (A.equals (b));}}Results:Cause Analysis:inFinalthe declared method does not allow overwriting, nor does it allow changes, so using final,we can devise a special kind of"Read-only"of the"Immutable Classes". Create"Immutable Classes"Object , the properties of this o
Answer one:
One of the most popular Java face questions is: What are immutable objects (immutable object), what are the benefits of immutable objects, what should be used, or more specifically, why should Java string classes be set to i
mutable classes: After an instance of a class is created, you can also modify the contents of the instance.For example, to create a 3*3 matrix, if Set function is set, you can change the size of the corresponding position element with set in main.Immutable classes: When an instance of a class is established, the State does not change and the member variable canno
, but the array to which value points is placed in the heap heap, The value of this can be changed .The purpose of the private adornment is to prevent value from being accessed by other classes, which prevents other classes from directly manipulating the data in the heap that the value reference points to.3). The string designer avoids the direct manipulation of data in the heap in all subsequent methodsAs mentioned in the 2nd, "value points to an array that is placed in the heap, and its value
The state of an instance of an immutable class does not change, such instances can be safely shared by other objects associated with it, and can be safely shared by multiple threads. To save memory space and optimize the performance of your program, you should reuse instances of immutable classes as much as possible , and avoid repeatedly creating instances of in
The first thing to use is collections. unmodifiable xxx () this function See the following code for the rest.It is said that this function has a bug, and then out of guava1ImportJava.util.Collection;2ImportJava.util.Collections;3 ImportJava.util.HashSet;4ImportJava.util.Set;567 PublicclassGuava {8 PublicStatic voidMain (string[] args) {9SetNewHashsetTenSet.add ("Hello"); OneSet.add ("Insert"); ASet -SYSTEM.OUT.PRINTLN (set); - the} - }How to use the immutabl
Disclaimer: this blog is an original blog and cannot be reproduced without permission! If you see it in other places, it is recommended to take a look at csdn (the original link for the http://blog.csdn.net/bettarwang/article/details/26744661), read the code and ask questions, discussion is more convenient.
In Java, final is mainly used in three aspects: Variable modification, modification method and modification
changed, you can use the StringBuilder and StringBuffer classes to create strings that are more flexible and allow you to add, insert, and append new content. [Expand, the following piece of code is not related to immutable] look at another interesting string instance: Package Date0804.demo2;public class Immutablestring {public static void main (string[] args)
Immutable (immutable) collectionI. OverviewGuava is a library of Google, to make up for many aspects of the Java language deficiencies, many in the JAVA8 has been implemented, temporarily do not expand. Collections is a tool class provided by the JDK.Guava the difference between im
There is many reasons due to thestring classhas been made immutableinchJava. These reasonsinchView, concurrency issues, security issues and performance issues. Here isA list of various valid reasons to go forImmutablestring class: string is designed to be immutable in Java for a number of reasons. This is done primarily for concurrency issues, security issues, an
Immutable class (immutable) means that the field of the instance is immutable after the instance is created for the mine. 8 Java-supplied wrapper classes and Java.lang.String classes are immutable classes
If you need to
In Java, strings are string constants that can be analyzed in terms of memory, synchronization mechanism, data structure, etc. 1: Need for constant pool in stringThe place where a string differs from the common base variable type is the object. String objects in Java are stored in a string constant pool, and when you create a string, you first look for the same
, introduce additional overhead and problems, such as context switching, latency, and ABA issues. The intent of the immutable object pattern is to make the shared object "inherently" thread-safe by using an externally visible state immutable object (that is, immutable object) without additional synchronization access control. This ensures data consistency and avo
is the 0.final modified class an immutable class?A: No. The final decorated class is called an inheritable class. There is no relationship between the two. That is, the class name of the immut
1. What is an immutable object?As we all know, in Java, the string class is immutable. So what exactly is an immutable object?It can be said that if an object cannot change its state after it has been created, then the object is immutabl
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.