In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the analysis of Mutability variability in java. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Brief introduction
Mutable (mutable) and immutable (immutable) objects are often used in java programming.
A mutable type object means that after the object is created, its internal data may be modified. So its security is not guaranteed.
The immutable type object means that once the object is created, its internal data cannot be modified, and we can fully trust this object.
Although the mutable object is not secure enough, it can effectively reduce the copy of the object because it can be modified.
Because the immutable object is immutable, attempts to modify the object will result in a copy of the object, resulting in a new object.
The most common String we use is an immutable object.
Mutable and immutable objects
Now that we know the difference between a mutable object and an immutable object, let's take a look at how to determine whether the object is immutable or immutable.
First of all, the simplest point is that immutable objects cannot be modified after they are created, so there are basically no methods such as setXXX in immutable objects, while mutable objects provide methods such as setXXX that can modify the state of internal variables.
Look at an example. Java.util.Date is a mutable object and java.time.LocalTime is an immutable object.
What's the difference in their method definition?
The first is Date, where we can see that many setXXX methods are defined.
In LocalTime, we basically don't see the setXXX method.
At the same time, the fields of immutable objects are basically final, preventing them from being modified twice.
Second, immutable objects are generally uninheritable and are qualified by the final keyword in java:
Public class Datepublic final class LocalTime
Third, immutable objects generally hide constructors and use methods similar to factory patterns to create objects, which provides more mobility for instance creation.
Create a copy of the mutable object
So what if we want to use the mutable object and don't want to be modified by others?
The easy way is to make a copy of the object you want to use:
Public class CopyOutput {private final java.util.Date date;... Public java.util.Date getDate () {return (java.util.Date) date.clone ();}}
Here, we should also pay attention to the problems of deep copy and shallow copy.
Create a copy method for the mutable class
Now that you want to create a copy of the mutable object, the corresponding mutable class also needs to provide a copy method to assist with the copy.
A deep copy and a shallow copy need to be considered here.
Don't trust equals.
Do we know how to find a key in HashMap? First look for the hash value of the key, and then determine whether the key.equals method is equal, consider the following situation:
Private final Map extras = new HashMap (); public void op (Window window) {Extra extra = extras.get (window);}
The op method receives a Window object and then pulls the corresponding value from the HashMap as a key.
If, at this time, we have a class A that inherits Window, and the hash value and equals are the same as another Window object B, then using the key of A can get the data stored by the key of B.
How to solve this problem?
There is a special HashMap:IdentityHashMap in Java. The key and value of this Map are compared with the = = method instead of the equals method, so the above problems can be effectively avoided.
Private final Map extras = new IdentityHashMap (); public void op (Window window) {Extra extra = extras.get (window);}
If such a Map is not available, you can use an immutable object as a key or use a private variable of Window, so that a malicious attacker cannot obtain this variable.
Public class Window {/ * pp * / class PrivateKey {Window getWindow () {return Window.this;}} final PrivateKey privateKey = new PrivateKey (); private final Map extras = new WeakHashMap () Public class WindowOps {public void op (Window window) {/ / Window.equals may be overridden, / / but safe as we don't use it. Extra extra = extras.get (window.privateKey);.}} do not directly expose modifiable properties
If a property in a mutable class does need to be exposed for external use, be sure to define the property as private and wrap it with the wrapper method.
If directly exposed, then basically there is no access control to speak of, as long as any program can get you this object, you can modify the properties. Considering the following application, we add a parameter checksum permission control to the method of modifying state.
Public final class WrappedState {/ / private immutable object private String state; / / wrapper method public String getState () {return state;} / / wrapper method public void setState (final String newState) {this.state = requireValidation (newState) } private static String requireValidation (final String state) {if (...) {throw new IllegalArgumentException ("...");} return state;}} public static fields should be set to final
Similarly, if you are a class variable and certainly do not want this variable to be modified by anyone, you need to set it to final.
Public class Files {public static final String separator = "/"; public static final String pathSeparator = ":";} public static final field should be immutable
If the class variable is public static final, then the variable must be immutable.
Some people will ask, has it been defined as final, is it already immutable?
In fact, this is not the case. For example, we define a List of final. Although this list cannot be changed, the value in list can be changed. We need to change a mutable variable to an immutable variable, as follows:
Import static java.util.Arrays.asList; import static java.util.Collections.unmodifiableList;... Public static final List names = unmodifiableList (asList ("Fred", "Jim", "Sheila"))
If you use the of () or ofEntries () methods introduced in JDK9, you can directly create an immutable collection:
Public static final List names = List.of ("Fred", "Jim", "Sheila"); so much for the analysis of Mutability variability in java. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 259
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.