immutability

String is immutable. What exactly is the meaning? [duplicate]

夙愿已清 提交于 2019-11-25 22:04:41
问题 This question already has an answer here: Immutability of Strings in Java 26 answers I wrote the following code on immutable Strings. public class ImmutableStrings { public static void main(String[] args) { testmethod(); } private static void testmethod() { String a = \"a\"; System.out.println(\"a 1-->\" + a); a = \"ty\"; System.out.println(\"a 2-->\" + a); } } Output: a 1-->a a 2-->ty Here the value of variable a has been changed (while many say that contents of the immutable objects cannot

Remove specific characters from a string in Python

六眼飞鱼酱① 提交于 2019-11-25 21:57:38
问题 I\'m trying to remove specific characters from a string using Python. This is the code I\'m using right now. Unfortunately it appears to do nothing to the string. for char in line: if char in \" ?.!/;:\": line.replace(char,\'\') How do I do this properly? 回答1: Strings in Python are immutable (can't be changed). Because of this, the effect of line.replace(...) is just to create a new string, rather than changing the old one. You need to rebind (assign) it to line in order to have that variable

Immutable vs Mutable types

放肆的年华 提交于 2019-11-25 21:43:03
问题 I\'m confused on what an immutable type is. I know the float object is considered to be immutable, with this type of example from my book: class RoundFloat(float): def __new__(cls, val): return float.__new__(cls, round(val, 2)) Is this considered to be immutable because of the class structure / hierarchy?, meaning float is at the top of the class and is its own method call. Similar to this type of example (even though my book says dict is mutable): class SortedKeyDict(dict): def __new__(cls,