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 create your own custom
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. Many immutable classes, such as Interger, long, and string, are
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
First look at a small program
The output of the main function does not output 3 as expected. Instead, it outputs the initialized value 0.
This is becauseSimilar to string, all packaging classes are final classes, that is, immutable classes..
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,
Immutable classes: When an instance of this class is created, the value of its member variable cannot beAdvantages:1. The value of the thread-safe object cannot be changed, reducing the likelihood of concurrency errors2. High efficiency when an
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
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
non-variable class
First, the popular Science 2 concepts, variable classes and immutable classes.
1), the immutable class means that when an instance of the class is created, the instance variable of the instance is immutable. Java provides 8
The status of an instance of an unchangeable class does not change. Such an instance can be securely shared by other associated objects and securely shared by multiple threads. To save memory space and optimize program performance, you should try to
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 immutable class can be either final or not.1. What are the
This article is published in sync with the personal blog zhiheng.me, titled: Immutable Classes in Java.Immutable classes in JavaImmutable class (Immutable Objects): When an instance of a class is created, its contents cannot be changed, i.e. its
Immutable Introduction immutable objects never change, and the values of their fields are set only once when the constructor is run, and then no longer changed . For example, the two basic data types that are common in the JDK are string and integer,
Rule of thumb: Consider a static factory method instead of a public construction methodFirst, the static factory methodFor Java classes, in order for a consumer to get an instantiated object of its own, the following methods are available:1. This
One factor that makes Java so comfortable is that it's a safe language. This means that it is overrun with buffers, arrays out of bounds, illegal pointers, and other memory corruption are automatically immune, and these errors plague unsafe
String is an immutable class in Java that cannot be modified once instantiated. Once an instance of an immutable class is created, the value of its member variable cannot be modified, and the immutable class has many advantages. Everyone knows why
1. Initial definitions of mutable and immutable objects:Variable class: When you get an instance reference of this class, you can change the content of this instance.Immutable class: When you obtain an instance reference of this class, you cannot
Immutable class, that is, the class that the instance cannot be modified. All information contained in the instance remains unchanged within the lifecycle of the object.
Common examples include string, basic encapsulation class, bigdecimal, and
How to make Java classes immutable
Immutable class: Once created, the status cannot be changedThere are many rules for creating immutable classes. The following describes these rules one by one:DirectoryGuide to defining the benefits of an immutable
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.