Comparative analysis of mutable objects and immutable objects in Java
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

Wedge We know that objects are created in two main ways, one is through Python/CAPI, and the other is by calling a type object. For instance objects of built-in types, both methods are supported. For example, lists can be created through [] or list(). The former is Python/CAPI and the latter is a calling type object. But for instance objects of custom classes, we can only create them by calling type objects. If an object can be called, then the object is callable, otherwise it is not callable. Determining whether an object is callable depends on whether a method is defined in its corresponding type object. like

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

There has been news about the rounded corners of the win10 search box for a long time, but it has never been implemented. We can generally use the registry to experience the rounded corners of the win10 search box. So let's take a look at the tutorial on the rounded corners of the win10 search box. Bar. Win10 search box variable rounded corners: 1. Open the search box, enter regedit, and enter the registry. 2. Find this path in Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Search. 3. In the blank space, select New - DWORD (32-bit) value - Name the new key ImmersiveSearch - Number

Use Python's __contains__() function to define the containment operation of an object. Python is a concise and powerful programming language that provides many powerful features to handle various types of data. One of them is to implement the containment operation of objects by defining the __contains__() function. This article will introduce how to use the __contains__() function to define the containment operation of an object, and give some sample code. The __contains__() function is Pytho

Title: Using Python's __le__() function to define a less than or equal comparison of two objects In Python, we can define comparison operations between objects by using special methods. One of them is the __le__() function, which is used to define less than or equal comparisons. The __le__() function is a magic method in Python and is a special function used to implement the "less than or equal" operation. When we compare two objects using the less than or equal operator (<=), Python

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values are passed and object references are passed.
