immutability

Is Integer Immutable

≯℡__Kan透↙ 提交于 2019-11-26 05:19:40
问题 I know this is probably very stupid, but a lot of places claim that the Integer class in Java is immutable, yet the following code: Integer a=3; Integer b=3; a+=b; System.out.println(a); Executes without any trouble giving the (expected) result 6. So effectively the value of a has changed. Doesn\'t that mean Integer is mutable? Secondary question and a little offtopic: \"Immutable classes do not need copy constructors\". Anyone care to explain why? 回答1: Immutable does not mean that a can

Java Immutable Collections

巧了我就是萌 提交于 2019-11-26 05:15:54
问题 From Java 1.6 Collection Framework documentation: Collections that do not support any modification operations (such as add , remove and clear ) are referred to as unmodifiable . [...] Collections that additionally guarantee that no change in the Collection object will ever be visible are referred to as immutable . The second criteria confuses me a bit. Given the first collection is unmodifiable, and assuming that the original collection reference has been disposed away, what are the changes

a mutable type inside an immutable container

假如想象 提交于 2019-11-26 04:54:19
问题 I\'m a bit confused about modifying tuple members. The following doesn\'t work: >>> thing = ([\'a\'],) >>> thing[0] = [\'b\'] TypeError: \'tuple\' object does not support item assignment >>> thing ([\'a\'],) But this does work: >>> thing[0][0] = \'b\' >>> thing ([\'b\'],) Also works: >>> thing[0].append(\'c\') >>> thing ([\'b\', \'c\'],) Doesn\'t work, and works (huh?!): >>> thing[0] += \'d\' TypeError: \'tuple\' object does not support item assignment >>> thing ([\'b\', \'c\', \'d\'],)

Make immutable Java object

别来无恙 提交于 2019-11-26 04:39:01
问题 My goal is to make a Java object immutable. I have a class Student . I coded it in the following way to achieve immutability: public final class Student { private String name; private String age; public Student(String name, String age) { this.name = name; this.age = age; } public String getName() { return name; } public String getAge() { return age; } } My question is, what is the best way to achieve immutability for the Student class? 回答1: Your class is not immutable strictly speaking, it is

How to avoid writing duplicate accessor functions for mutable and immutable references in Rust?

爷,独闯天下 提交于 2019-11-26 04:28:14
问题 A few times, I\'ve run into the scenario where an accessor method is needed for both mutable and immutable references. For ~3 lines it isn\'t a problem to duplicate the logic, but when the logic gets more complex, it\'s not nice to copy-paste large blocks of code. I\'d like to be able to re-use the code for both. Does Rust provide some way handle this better then copy-pasting code, or using unsafe casts? e.g.: impl MyStruct { pub fn get_foo(&self) -> &Bar { // ~20 lines of code // --- snip --

Must all properties of an immutable object be final?

南楼画角 提交于 2019-11-26 04:20:14
问题 Must immutable objects have all properties be final ? According to me not. But I don\'t know, whether I am right. 回答1: 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:

Immutability of structs [duplicate]

心已入冬 提交于 2019-11-26 03:56:41
问题 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? 回答1: Structs should represent values . Values do not change. The number 12 is eternal. However, consider: Foo foo = new Foo(); // a mutable

Why would one declare an immutable class final in Java?

試著忘記壹切 提交于 2019-11-26 03:51:09
问题 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 ? 回答1: 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

Why are C# structs immutable?

試著忘記壹切 提交于 2019-11-26 03:50:55
问题 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? 回答1: 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

Aren't Python strings immutable? Then why does a + “ ” + b work?

依然范特西╮ 提交于 2019-11-26 03:50:20
My understanding was that Python strings are immutable. I tried the following code: a = "Dog" b = "eats" c = "treats" print a, b, c # Dog eats treats print a + " " + b + " " + c # Dog eats treats print a # Dog a = a + " " + b + " " + c print a # Dog eats treats # !!! Shouldn't Python have prevented the assignment? I am probably missing something. Any idea? First a pointed to the string "Dog". Then you changed the variable a to point at a new string "Dog eats treats". You didn't actually mutate the string "Dog". Strings are immutable, variables can point at whatever they want. The string