简体   繁体   English

Java中字符串不可变的概念

[英]Concept of Immutability of String in Java

i know the reason, why String is immutable but my question is why the concept of immutability sticks to only String class in java , why it won't apply to others. 我知道原因,为什么String是不可变的,但是我的问题是为什么不变性的概念仅坚持java中的String类,为什么它不适用于其他人。

one of the reason i found why it is immutable is HERE 我发现它为什么不可变的原因之一是在这里

but why not all other classes like Integer and so on.. how java people decided changing their values might not effect .. 但是,为什么不是所有其他类(例如Integer等)呢?。Java人们如何决定更改其值可能不会生效..

please explain . 请解释 。

The point is that you could confuse a String (which is an Object) with a primitive type. 关键是您可以将字符串(是对象)与原始类型混淆。 In fact, The way strings typically appear in Java programs can lead to confusion. 实际上,字符串通常在Java程序中出现的方式会引起混乱。 There are shortcuts available for strings that are not available for other objects in Java. 对于Java中的其他对象而言,有些字符串无法使用的快捷方式。 These shortcuts are highly used and they give strings the appearance of a primitive data type. 这些快捷方式得到了广泛使用,它们使字符串具有原始数据类型的外观。 But, strings are objects and they possess all the attributes and behaviours of objects in Java 但是,字符串是对象,它们拥有Java中对象的所有属性和行为。

When designing a class, to make it immutable or not is a design choice and usually a tradeoff. 在设计类时,使其不可变是一种设计选择,通常是一种折衷方案。 The classes String and Integer were made immutable for many reasons including security. 由于许多原因,包括安全性,使String和Integer类不可变。

A class like java.util.Random is all about managing some state, so it has to be mutable. 诸如java.util.Random之类的类都是关于管理某种状态的,因此它必须是可变的。

A class like java.awt.Rectangle is mutable, which has the advantage of saving memory management costs when you modify one rather than create a new one, but unfortunately it means that when a Rectangle instance that's shared (eg by two different window objects), changing it for one of them will change it out from under the others, causing unhappy surprises. 诸如java.awt.Rectangle之类的类是可变的,当您修改一个而不是创建一个新的类时,它具有节省内存管理成本的优点,但是不幸的是,这意味着当一个Rectangle实例被共享时(例如,由两个不同的窗口对象共享) ,对其中一个进行更改会使其从其他状态更改为其他状态,从而导致不愉快的意外。 Modern garbage collectors are very efficient [I hear they're more efficient than C's malloc() and free() ] so mutability may not actually save memory management overhead, certainly not if you often have to copy objects to protect against accidental shared changes. 现代垃圾收集器非常高效[我听说它们比C的malloc()free()更高效],因此可变性实际上可能不会节省内存管理开销,如果您经常必须复制对象以防止意外的共享更改,则肯定不会这样。

You can freely share an immutable object without worrying about it changing out from under you. 您可以自由共享一个不变的对象,而不必担心它会从您身下变出来。

Integer is immutable, so each operation on it creates a new instance. 整数是不可变的,因此对其进行的每个操作都会创建一个新实例。

Immutable does not mean that a refernece can never assign another value. 不可变并不意味着参照不能分配其他值。 For example, String is immutable too, but we can do this: 例如,String也是不可变的,但是我们可以这样做:

String ex = "good";    // ex == "good"
ex = ex + "morning";    // now ex == "goodmorning"

So what happened there? 那么那里发生了什么? Since String is immutable, clearly "ex" was not changed. 由于String是不可变的,因此显然“ ex”没有更改。 But it now being assigned something different. 但是现在分配了一些不同的东西。 This is because "ex" is now a completely newly instantiated object. 这是因为“ ex”现在是一个完全新实例化的对象。

So this is same the case of Integer. 因此,与Integer相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM