immutability

What is difference between mutable and immutable String in java

旧时模样 提交于 2019-11-26 15:23:40
问题 As per my knowledge, a mutable string can be changed, and an immutable string cannot be changed. Here I want to change the value of String like this, String str="Good"; str=str+" Morning"; and other way is, StringBuffer str= new StringBuffer("Good"); str.append(" Morning"); In both the cases I am trying to alter the value of str . Can anyone tell me, what is difference in both case and give me clear picture of mutable and immutable objects. 回答1: Case 1: String str = "Good"; str = str + "

Must all properties of an immutable object be final?

左心房为你撑大大i 提交于 2019-11-26 15:19:24
Must immutable objects have all properties be final ? According to me not. But I don't know, whether I am right. assylias The main difference between an immutable object (all properties final) and an effectively immutable object (properties aren't final but can't be changed) is safe publication. You can safely publish an immutable object in a multi threaded context without having to worry about adding synchronization, thanks to the guarantees provided by the Java Memory Model for final fields : final fields also allow programmers to implement thread-safe immutable objects without

Correct way to push into state array

南楼画角 提交于 2019-11-26 15:13:55
问题 I seem to be having issues pushing data into a state array. I am trying to achieve it this way: this.setState({ myArray: this.state.myArray.push('new value') }) But I believe this is incorrect way and causes issues with mutability? 回答1: Array push returns length this.state.myArray.push('new value') returns the length of the extended array, instead of the array itself.Array.prototype.push(). I guess you expect the returned value to be the array. Immutability It seems it's rather the behaviour

Immutability of structs [duplicate]

与世无争的帅哥 提交于 2019-11-26 14:32:27
Possible Duplicate: Why are mutable structs evil? I read it in lots of places including here that it's better to make structs as immutable. What's the reason behind this? I see lots of Microsoft-created structs that are mutable, like the ones in xna. Probably there are many more in the BCL. What are the pros and cons of not following this guideline? Structs should represent values . Values do not change. The number 12 is eternal. However, consider: Foo foo = new Foo(); // a mutable struct foo.Bar = 27; Foo foo2 = foo; foo2.Bar = 55; Now foo.Bar and foo2.Bar is different, which is often

Python, subclassing immutable types

不想你离开。 提交于 2019-11-26 14:22:58
问题 I've the following class: class MySet(set): def __init__(self, arg=None): if isinstance(arg, basestring): arg = arg.split() set.__init__(self, arg) This works as expected (initialising the set with the words of the string rather than the letters). However when I want to do the same with the immutable version of set, the __init__ method seems to be ignored: class MySet(frozenset): def __init__(self, arg=None): if isinstance(arg, basestring): arg = arg.split() frozenset.__init__(self, arg) Can

Why are integers immutable in Python? [duplicate]

☆樱花仙子☆ 提交于 2019-11-26 14:22:12
问题 This question already has an answer here: Immutable vs Mutable types 16 answers I understand the differences between mutable and immutable objects in Python. I have read many posts discussing the differences. However, I have not read anything regarding WHY integers are immutable objects. Does there exist a reason for this? Or is the answer "that's just how it is"? Edit: I am getting prompted to 'differentiate' this question from other questions as it seems to be a previously asked question.

Why would one declare an immutable class final in Java?

人盡茶涼 提交于 2019-11-26 14:19:44
I read that to make a class immutable in Java, we should do the following, Do not provide any setters Mark all fields as private Make the class final Why is step 3 required? Why should I mark the class final ? templatetypedef If you don't mark the class final , it might be possible for me to suddenly make your seemingly immutable class actually mutable. For example, consider this code: public class Immutable { private final int value; public Immutable(int value) { this.value = value; } public int getValue() { return value; } } Now, suppose I do the following: public class Mutable extends

Why are C# structs immutable?

流过昼夜 提交于 2019-11-26 14:18:25
I was just curious to know why structs, strings etc are immutable? What is the reason for making them immutable and rest of the objects as mutable. What are the things that are considered to make an object immutable? Is there any difference on the way how memory is allocated and deallocated for mutable and immutable objects? If this subject interests you, I have a number of articles about immutable programming at http://blogs.msdn.com/b/ericlippert/archive/tags/immutability/ I was just curious to know why structs, strings etc are immutable? Structs and classes are not immutable by default,

Immutable Type: public final fields vs. getter

微笑、不失礼 提交于 2019-11-26 12:42:07
问题 I need a small Container-Class for storing some Strings which should be immutable. As String itself is an immutable type, I thought of something like that: public final class Immu { public final String foo; public final String bar; public Immu(final String foo, final String bar) { this.foo = foo; this.bar = bar; } } Many people seem to object using public fields at all and use Getters instead. IMHO this would be just boilerplate in this case, because String itself is immutable. Other thoughts

How do I identify immutable objects in Java

非 Y 不嫁゛ 提交于 2019-11-26 12:02:28
问题 In my code, I am creating a collection of objects which will be accessed by various threads in a fashion that is only safe if the objects are immutable. When an attempt is made to insert a new object into my collection, I want to test to see if it is immutable (if not, I\'ll throw an exception). One thing I can do is to check a few well-known immutable types: private static final Set<Class> knownImmutables = new HashSet<Class>(Arrays.asList( String.class, Byte.class, Short.class, Integer