Home  >  Article  >  Java  >  Comparative analysis of mutable objects and immutable objects in Java

Comparative analysis of mutable objects and immutable objects in Java

巴扎黑
巴扎黑Original
2017-07-20 13:18:142182browse

1. What is a mutable object?

Objects that do not create new objects when their properties are changed, such as StringBuiler, can regard the strings stored in them as properties. When calling StringBuilder.append(String str) to append a string, it is This is done based on the existing StringBuilder object, and no new object is created.

Wrapper classes of basic data types and objects of almost all classes except the String class are mutable objects.

2. What is an immutable object?

When the properties are changed, a new object must be created, such as String. If there is already "String str='abc'", assign "str='efg'" again and create a new String. The object stores "efg", and str points to this newly created object.

Wrapper classes of basic data types and objects of the String class are immutable objects.

3. Choice of mutable objects and immutable objects

Immutable objects represent a stable area in memory and multiple reference variables Pointing to the same area, when a reference variable tries to change the content of the object, it will not change the content of the original object, but create a new object. This ensures the stability of the data. Therefore, immutable objects are usually used in situations where data needs to be kept stable.

When updating the properties of a variable object, no new object will be created to save memory space. Therefore, variable objects are mainly used in situations where properties change frequently, such as counters. It is necessary to establish statistical objects and The mapping relationship between statistical results is therefore constructed as a Map collection. Integer objects are immutable objects and are not suitable for storing statistical results. A mutable object should be used to store statistical results. An array is used here. Of course, it can also be a custom object. There is an int attribute used for counting:

    public void test01() {
        String str = "abc efe hig klm nop qrs";
        String[] arr = str.split(" ");

        HashMap<String, int[]> map = new HashMap<String, int[]>();for (String x : arr) {int[] count = map.get(x);if (count != null)
                count[0]++;elsemap.put(x, new int[] { 1 });
        }
    }

Immutable objects have several advantages:
1. Memory allocation is fixed and does not need to be expanded
2. Multiple copies can be made arbitrarily, between different threads No need to lock or unlock
This is the "variable unchanged" feature in functional programming. Of course there are other benefits advocated by functional programming, such as code clarity, etc., but this is a matter of opinion.

The above is the detailed content of Comparative analysis of mutable objects and immutable objects in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn