immutability

Does String.Replace() create a new string if there's nothing to replace?

女生的网名这么多〃 提交于 2019-12-10 00:53:32
问题 For example: public string ReplaceXYZ(string text) { string replacedText = text; replacedText = replacedText.Replace("X", String.Empty); replacedText = replacedText.Replace("Y", String.Empty); replacedText = replacedText.Replace("Z", String.Empty); return replacedText; } If I were to call "ReplaceXYZ" even for strings that do not contain "X", "Y", or "Z", would 3 new strings be created each time? I spotted code similar to this in one of our projects. It's called repeatedly as it loops through

Immutability and synchronization in Java

让人想犯罪 __ 提交于 2019-12-09 23:22:12
问题 Since I've read the book Java Concurrency in Practice I was wondering how I could use immutability to simplify synchronization problems between threads. I perfectly understand that an immutable object is thread-safe . Its state cannot change after initialization, so there cannot be "shared mutable states" at all. But immutable object have to be use properly to be considered useful in synchronization problems. Take for example this piece of code, that describes a bank wich owns many accounts

Scala val has to be guarded with synchronized for concurrent access?

流过昼夜 提交于 2019-12-09 16:39:24
问题 As I read, Scala immutable val doesn't get translated to Java final for various reasons. Does this mean that accessing a val from an other Thread must be guarded with synchronization in order to guarantee visibility? 回答1: As object members, once initialized, val s never change their values during the lifetime of the object. As such, their values are guaranteed to be visible to all threads provided that the reference to the object didn't escape in the constructor. And, in fact, they get Java

Is String get/set threadsafe?

妖精的绣舞 提交于 2019-12-09 15:59:08
问题 Let's say I have the following, public class Foo{ private String bar; public String getBar(){ return bar; } public void setBar(String bar){ this.bar = bar; } } Are these methods automatically threadsafe due to the immutable nature of the String class, or is some locking mechanism required? 回答1: No, this is not threadsafe. Foo is mutable, so if you want to ensure that different threads see the same value of bar – that is, consistency – either: Make bar volatile , or Make the methods

Is there a way to freeze an ES6 Map?

徘徊边缘 提交于 2019-12-09 14:40:57
问题 I'm looking for a way to freeze native ES6 Maps. Object.freeze and Object.seal don't seem to work: let myMap = new Map([["key1", "value1"]]); // Map { 'key1' => 'value1' } Object.freeze(myMap); Object.seal(myMap); myMap.set("key2", "value2"); // Map { 'key1' => 'value1', 'key2' => 'value2' } Is this intended behavior since freeze freezes properties of objects and maps are no objects or might this be a bug / not implemented yet? And yes I know, I should probably use Immutable.js, but is there

Are all final class immutable?

匆匆过客 提交于 2019-12-09 14:29:13
问题 Are all final classes in Java immutable. String and Integer both are final classes and both are immutable i beleive. 回答1: No - a final class means you cannot inherit from it. It has nothing to do with mutability. The following class is final yet mutable: public final class FinalMutable { int value; public void setValue(int v) { value=v; } public int getValue() { return value; } } 回答2: No, final means the class can not be extended. It says nothing about mutability. For example: final class

Why does CouchDB use an append-only B+ tree and not a HAMT

血红的双手。 提交于 2019-12-09 12:11:44
问题 I'm reading up on datastructures, especially immutable ones like the append-only B+ tree used in CouchDB and the Hash array mapped trie used in Clojure and some other functional programming languages. The main reason datastructures that work well in memory might not work well on disk appears to be time spent on disk seeks due to fragmentation, as with a normal binary tree. However, HAMT is also very shallow, so doesn't require any more seeks than a B tree. Another suggested reason is that

Why no immutable double linked list in Scala collections?

安稳与你 提交于 2019-12-09 09:25:45
问题 Looking at this question, where the questioner is interested in the first and last instances of some element in a List , it seems a more efficient solution would be to use a DoubleLinkedList that could search backwards from the end of the list. However there is only one implementation in the collections API and it's mutable. Why is there no immutable version? 回答1: Because you would have to copy the whole list each time you want to make a change. With a normal linked list, you can at least

Unit testing for object immutability

爷,独闯天下 提交于 2019-12-09 09:04:08
问题 I want to make sure that a given group of objects is immutable. I was thinking about something along the lines of: check if every field is private final check if class is final check for mutable members So I guess my question is: is 3. possible ? I can check recursively whether every member of a class has its fields private final , but this is not enough since a class can have e method named getHaha(param) which adds the given param to an array for instance. So is there a good way to check if

Mutable vs Immutable for parallel applications [closed]

独自空忆成欢 提交于 2019-12-09 07:40:55
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . In the application I am writing, I need to write lots of base types, which will most likely be immutable. But I am wondering how mutable types compare in parallel applications to immutable ones. You can use locks with mutable objects, right? How does it compare to other