immutability

Immutable array in Java

不羁岁月 提交于 2019-11-26 01:49:40
问题 Is there an immutable alternative to the primitive arrays in Java? Making a primitive array final doesn\'t actually prevent one from doing something like final int[] array = new int[] {0, 1, 2, 3}; array[0] = 42; I want the elements of the array to be unchangeable. 回答1: Not with primitive arrays. You'll need to use a List or some other data structure: List<Integer> items = Collections.unmodifiableList(Arrays.asList(0,1,2,3)); 回答2: My recommendation is to not use an array or an

Immutable array in Java

江枫思渺然 提交于 2019-11-26 01:21:00
Is there an immutable alternative to the primitive arrays in Java? Making a primitive array final doesn't actually prevent one from doing something like final int[] array = new int[] {0, 1, 2, 3}; array[0] = 42; I want the elements of the array to be unchangeable. Not with primitive arrays. You'll need to use a List or some other data structure: List<Integer> items = Collections.unmodifiableList(Arrays.asList(0,1,2,3)); ColinD My recommendation is to not use an array or an unmodifiableList but to use Guava 's ImmutableList , which exists for this purpose. ImmutableList<Integer> values =

Why are exclamation marks used in Ruby methods?

烂漫一生 提交于 2019-11-26 00:06:53
问题 In Ruby some methods have a question mark ( ? ) that ask a question like include? that ask if the object in question is included, this then returns a true/false. But why do some methods have exclamation marks ( ! ) where others don\'t? What does it mean? 回答1: In general, methods that end in ! indicate that the method will modify the object it's called on . Ruby calls these as " dangerous methods " because they change state that someone else might have a reference to. Here's a simple example

Why are mutable structs “evil”?

淺唱寂寞╮ 提交于 2019-11-25 23:55:41
问题 Following the discussions here on SO I already read several times the remark that mutable structs are “evil” (like in the answer to this question). What\'s the actual problem with mutability and structs in C#? 回答1: Structs are value types which means they are copied when they are passed around. So if you change a copy you are changing only that copy, not the original and not any other copies which might be around. If your struct is immutable then all automatic copies resulting from being

How to make an immutable object in Python?

白昼怎懂夜的黑 提交于 2019-11-25 23:55:29
Although I have never needed this, it just struck me that making an immutable object in Python could be slightly tricky. You can't just override __setattr__ , because then you can't even set attributes in the __init__ . Subclassing a tuple is a trick that works: class Immutable(tuple): def __new__(cls, a, b): return tuple.__new__(cls, (a, b)) @property def a(self): return self[0] @property def b(self): return self[1] def __str__(self): return "<Immutable {0}, {1}>".format(self.a, self.b) def __setattr__(self, *ignored): raise NotImplementedError def __delattr__(self, *ignored): raise

Is a Java string really immutable?

折月煮酒 提交于 2019-11-25 23:09:16
We all know that String is immutable in Java, but check the following code: String s1 = "Hello World"; String s2 = "Hello World"; String s3 = s1.substring(6); System.out.println(s1); // Hello World System.out.println(s2); // Hello World System.out.println(s3); // World Field field = String.class.getDeclaredField("value"); field.setAccessible(true); char[] value = (char[])field.get(s1); value[6] = 'J'; value[7] = 'a'; value[8] = 'v'; value[9] = 'a'; value[10] = '!'; System.out.println(s1); // Hello Java! System.out.println(s2); // Hello Java! System.out.println(s3); // World Why does this

About the changing id of an immutable string

泪湿孤枕 提交于 2019-11-25 23:05:15
问题 Something about the id of objects of type str (in python 2.7) puzzles me. The str type is immutable, so I would expect that once it is created, it will always have the same id . I believe I don\'t phrase myself so well, so instead I\'ll post an example of input and output sequence. >>> id(\'so\') 140614155123888 >>> id(\'so\') 140614155123848 >>> id(\'so\') 140614155123808 so in the meanwhile, it changes all the time. However, after having a variable pointing at that string, things change: >>

Why .NET String is immutable? [duplicate]

我们两清 提交于 2019-11-25 22:59:34
问题 This question already has an answer here: Why can't strings be mutable in Java and .NET? 17 answers As we all know, String is immutable. What are the reasons for String being immutable and the introduction of StringBuilder class as mutable? 回答1: Instances of immutable types are inherently thread-safe, since no thread can modify it, the risk of a thread modifying it in a way that interferes with another is removed (the reference itself is a different matter). Similarly, the fact that aliasing

What is the difference between shallow copy, deepcopy and normal assignment operation?

て烟熏妆下的殇ゞ 提交于 2019-11-25 22:57:21
问题 import copy a = \"deepak\" b = 1, 2, 3, 4 c = [1, 2, 3, 4] d = {1: 10, 2: 20, 3: 30} a1 = copy.copy(a) b1 = copy.copy(b) c1 = copy.copy(c) d1 = copy.copy(d) print(\"immutable - id(a)==id(a1)\", id(a) == id(a1)) print(\"immutable - id(b)==id(b1)\", id(b) == id(b1)) print(\"mutable - id(c)==id(c1)\", id(c) == id(c1)) print(\"mutable - id(d)==id(d1)\", id(d) == id(d1)) I get the following results: immutable - id(a)==id(a1) True immutable - id(b)==id(b1) True mutable - id(c)==id(c1) False mutable

Immutability of Strings in Java

谁说胖子不能爱 提交于 2019-11-25 22:55:53
问题 Consider the following example. String str = new String(); str = \"Hello\"; System.out.println(str); //Prints Hello str = \"Help!\"; System.out.println(str); //Prints Help! Now, in Java, String objects are immutable. Then how come the object str can be assigned value \"Help!\". Isn\'t this contradicting the immutability of strings in Java? Can anybody please explain me the exact concept of immutability? Edit: Ok. I am now getting it, but just one follow-up question. What about the following