immutability

Java - Immutable array thread-safety

不问归期 提交于 2020-01-01 03:04:08
问题 I have a question regarding the Java Memory Model. Here is a simple class presenting the problem: public class ImmutableIntArray { private final int[] array; public ImmutableIntArray() { array = new int[10]; for (int i = 0; i < 10; i++) { array[i] = i; } } // Will always return the correct value? public int get(int index) { return array[index]; } } As far as I know the JMM guarantees that the value of final fields will be visible to other threads after construction. But I want to ensure that

Immutable beans in Java

三世轮回 提交于 2020-01-01 01:13:10
问题 I am very curious about the possibility of providing immutability for java beans (by beans here I mean classes with an empty constructor providing getters and setters for members). Clearly these classes are not immutable and where they are used to transport values from the data layer this seems like a real problem. One approach to this problem has been mentioned here in StackOverflow called "Immutable object pattern in C#" where the object is frozen once fully built. I have an alternative

Why are C# number types immutable?

╄→尐↘猪︶ㄣ 提交于 2019-12-31 09:29:20
问题 Why are int s and double s immutable? What is the purpose of returning a new object each time you want to change the value? The reason I ask is because I'm making a class: BoundedInt , which has a value and an upper and lower bound. So I was wondering: should I make this type immutable too? (Or should it be a struct ?) 回答1: Firstly: What is the purpose of returning a new object each time you want to change the value? I think you might be mistaken about how value types work. This isn't some

Immutability of string and concurrency

依然范特西╮ 提交于 2019-12-30 08:34:12
问题 Should we synchronize on writing strings? Since string is immutable we will never get inconsistent state between write and read from the 2 different threads, right? On other words, why we don't have atomic for string type? 回答1: string values are immutable, but variables are not. Variables are–what their name say– variable , their values can be changed. You don't need synchronization for accessing a string value, that can't change. If a string value is handed to you, that (the content of the

Why continue to use getters with immutable objects?

徘徊边缘 提交于 2019-12-30 08:05:13
问题 Using immutable objects has become more and more common, even when the program at hand is never meant to be ran in parallel. And yet we still use getters, which require 3 lines of boilerplate for every field and 5 extra characters on every access (in your favorite mainstream OO language). While this may seem trivial, and many editors remove most of the burden from the programmer anyways, it is still seemingly unnecessary effort. What are the reasons for the continued use of accessors versus

Why continue to use getters with immutable objects?

≯℡__Kan透↙ 提交于 2019-12-30 08:05:06
问题 Using immutable objects has become more and more common, even when the program at hand is never meant to be ran in parallel. And yet we still use getters, which require 3 lines of boilerplate for every field and 5 extra characters on every access (in your favorite mainstream OO language). While this may seem trivial, and many editors remove most of the burden from the programmer anyways, it is still seemingly unnecessary effort. What are the reasons for the continued use of accessors versus

Is Dictionary broken or should GetHashCode() only base on immutable members?

不问归期 提交于 2019-12-30 08:04:40
问题 When an object is added to the .NET System.Collections.Generic.Dictionary class the hashcode of the key is stored internally and used for later comparisons. When the hashcode changes after its initial insertion into the dictionary it often becomes "inaccessible" and may surprise its users when an existence check, even using the same reference, returns false (sample code below). The GetHashCode documentation says: The GetHashCode method for an object must consistently return the same hash code

Strings are immutable - that means I should never use += and only StringBuffer?

天涯浪子 提交于 2019-12-30 06:01:47
问题 Strings are immutable, meaning, once they have been created they cannot be changed. So, does this mean that it would take more memory if you append things with += than if you created a StringBuffer and appended text to that? If you use +=, you would create a new 'object' each time that has to be saved in the memory, wouldn't you? 回答1: Yes, you will create a new object each time with +=. That doesn't mean it's always the wrong thing to do, however. It depends whether you want that value as a

Why Room entities don't work with immutable properties in Android

断了今生、忘了曾经 提交于 2019-12-30 04:04:12
问题 I have been exploring Room database object mapping library and I figured something weird. An entity data model cannot have immutable properties, as this answer suggests. But I checked out google's persistent example with kotlin, Room works with immutable properties too. Please check this data class from the example. What could be the reason for this behavior? This could be a good feature if we could create immutable values ( val properties), as this restrict programmers from changing unique

Is there a way to use Json.Net deserialization with immutable classes?

℡╲_俬逩灬. 提交于 2019-12-30 01:36:13
问题 I'm working with an API that uses json. I have some classes that I've created to model the API. To make life easy, my models use public properties, which are in turn used by Json.Net when deserializing the json into objects. I'd like to make my objects immutable, but I'm running into a problem because if I make my properties read only, I break the deserialization. Is there a way for me to have immutable objects, and use deserialization? 回答1: I think you should be able to use