Why are Python strings immutable? 字符串是否可以改变
实践
1、
python
golang
// UnassignableOperand occurs when the left-hand side of an assignment is
// not assignable.
//
// Example:
// func f() {
// const c = 1
// c = 2
// }
UnassignableOperand
Immutables
When to use and why
Bill Venners: How about immutables? When should I use immutables versus non-immutables?
James Gosling: I would use an immutable whenever I can.
Bill Venners: Whenever you can, why?
James Gosling: From a strategic point of view, they tend to more often be trouble free. And there are usually things you can do with immutables that you can't do with mutable things, such as cache the result. If you pass a string to a file open method, or if you pass a string to a constructor for a label in a user interface, in some APIs (like in lots of the Windows APIs) you pass in an array of characters. The receiver of that object really has to copy it, because they don't know anything about the storage lifetime of it. And they don't know what's happening to the object, whether it is being changed under their feet.
You end up getting almost forced to replicate the object because you don't know whether or not you get to own it. And one of the nice things about immutable objects is that the answer is, "Yeah, of course you do." Because the question of ownership, who has the right to change it, doesn't exist.
One of the things that forced String
s to be immutable was security. You have a file open method. You pass a String
to it. And then it's doing all kind of authentication checks before it gets around to doing the OS call. If you manage to do something that effectively mutated the String
, after the security check and before the OS call, then boom, you're in. But String
s are immutable, so that kind of attack doesn't work. That precise example is what really demanded that String
s be immutable.
Bill Venners: How about the wrapper types? I was asked a couple of weeks ago, "Why are wrapper types immutable?" I wasn't exactly sure what to say.
James Gosling: Same answer.
Bill Venners: Because you can, and it helps with caching?
James Gosling: Yes.
Bill Venners: Okay. The tradeoff seems to be that sometimes I may end up creating a lot more little objects.
James Gosling: You may. One of the proposals that keeps surfacing -- I actually wrote up a proposal for this four or five years ago. It's something that happened after Java got released, and so it gets really hard. And this one has really resurfaced with a vengeance lately: this notion of strengthening the support for immutables.
Another thing about immutable objects: if you have a class that's final and whose fields are final, except for one nasty problem, the optimizers can do really cool things with them, because they don't necessarily have to allocate them in the heap. They can have pure stack lifetime. You can copy them at will. You can replicate them at will, which is what happens with primitives.
That's one of the reasons that primitives are not objects, because it is so nice to be able to just replicate them. When you pass an integer to a method, you don't have to pass the pointer to that integer. You can just replicate that integer and push it on the stack. So, it's a different integer. The same value, but a different integer and you can't actually tell. And if you look at a complex number class in C++ versus complex numbers in Fortran. In Fortran, they do all kinds of goofy things allocating complex numbers to registers, which really doesn't work in C++. And that mostly has to do with the fact that in C++, they are still objects and they have an identity. It's this whole platonic thing about identity. The nit that causes problems with optimizers and immutable objects is that as soon as you have a notion of identity that is independent of the value, then various things get really hard.
Strings in Java - GeeksforGeeks https://www.geeksforgeeks.org/strings-in-java/
栈区 堆区
String s1 =
"TAT"
;
String s2 =
"TAT"
;
String s3 =
new
String(
"TAT"
);
String s4 =
new
String(
"TAT"
);
2. Why string objects are immutable in Java?
Because java uses the concept of string literal. Suppose there are 5 reference variables, all refer to one object “Sachin”. If one reference variable changes the value of the object, it will be affected by all the reference variables. That is why string objects are immutable in java.
3. Why Java uses the concept of string literal?
To make Java more memory efficient (because no new objects are created if it exists already in the string constant pool).
2种方式创建字符串
Ways of Creating a String
There are two ways to create a string in Java:
- String Literal
- Using new Keyword
Syntax:
<String_Type> <string_variable> = "<sequence_of_string>";
1. String literal 字符串字面值
To make Java more memory efficient (because no new objects are created if it exists already in the string constant pool).
Example:
String s = “GeeksforGeeks”;
2. Using new keyword
- String s = new String(“Welcome”);
- In such a case, JVM will create a new string object in normal (non-pool) heap memory and the literal “Welcome” will be placed in the string constant pool. The variable s will refer to the object in the heap (non-pool)
Example:
String s = new String (“GeeksforGeeks”);
Design and History FAQ — Python 3.11.3 documentation https://docs.python.org/3/faq/design.html#why-are-python-strings-immutable
为什么Python字符串是不可变的?
有几个优点。
一个是性能:知道字符串是不可变的,意味着我们可以在创建时为它分配空间,并且存储需求是固定不变的。这也是元组和列表之间区别的原因之一。
另一个优点是,Python 中的字符串被视为与数字一样“基本”。 任何动作都不会将值 8 更改为其他值,在 Python 中,任何动作都不会将字符串 "8" 更改为其他值。
Why are Python strings immutable?
There are several advantages.
One is performance: knowing that a string is immutable means we can allocate space for it at creation time, and the storage requirements are fixed and unchanging. This is also one of the reasons for the distinction between tuples and lists.
Another advantage is that strings in Python are considered as “elemental” as numbers. No amount of activity will change the value 8 to anything else, and in Python, no amount of activity will change the string “eight” to anything else.