immutability

Is hash code of java.lang.String really cached?

匆匆过客 提交于 2019-12-22 05:07:37
问题 String s1 = "String1"; System.out.println(s1.hashCode()); // return an integer i1 Field field = String.class.getDeclaredField("value"); field.setAccessible(true); char[] value = (char[])field.get(s1); value[0] = 'J'; value[1] = 'a'; value[2] = 'v'; value[3] = 'a'; value[4] = '1'; System.out.println(s1.hashCode()); // return same value of integer i1 Here even after I changed the characters with the help of reflection, same hash code value is mainatained. Is there anything I need to know here?

Immutable game object, basic functional programming question

不羁的心 提交于 2019-12-22 04:25:06
问题 I'm in the process of trying to 'learn more of' and 'learn lessons from' functional programming and the idea of immutability being good for concurrency, etc. As a thought exercise I imagined a simple game where Mario-esq type character can run and jump around with enemies that shoot at him... Then I tried to imagine this being written functionally using immutable objects. This raised some questions that puzzled me (being an Imperative OO programmer). 1) If my little guy at position x10,y100

Caching strategy for small immutable objects in Java?

╄→гoц情女王★ 提交于 2019-12-22 04:15:16
问题 I am developing an app that creates a large number of small, immutable Java objects. An example might be: public class Point { final int x; final int y; final int z; ..... } Where it is likely that many instances of Point will need to refer to the same (x,y,z) location. To what extent does it make sense to try to cache and re-use such objects during the lifetime of the application? Any special tricks to handle this kind of situation? 回答1: The problem you are likely to have is making the

Equality of instance of functional interface in java [duplicate]

落爺英雄遲暮 提交于 2019-12-22 04:13:17
问题 This question already has answers here : Is there a way to compare lambdas? (3 answers) Closed 4 years ago . I am not sure how I can be sure about equality/immutability of functional interface. I guess there might be no way to assure equality when I use this syntactic sugar in java 8, please let me know any hint if you have any. I made a short code snippet for my question. public interface Element { void doSomething(int a); } and I've tried to add instance of this interface in functional way

Can an immutable type change its internal state?

血红的双手。 提交于 2019-12-22 01:47:11
问题 The question is simple. Can a type that can change its internal state without it being observable from the outside be considered immutable ? Simplified example: public struct Matrix { bool determinantEvaluated; double determinant; public double Determinant { get //asume thread-safe correctness in implementation of the getter { if (!determinantEvaluated) { determinant = getDeterminant(this); determinantEvaluated = true; } return determinant; } } } UPDATE : Clarified the thread-safeness issue,

Which of these are immutable in Python?

南楼画角 提交于 2019-12-21 23:00:33
问题 I am trying to figure out whether the following are immutable in Sage (which is built on Python so I believe if it is immutable in python, I believe in most cases it will be immutable in Sage) Below are objects e, f, g, i class e: pass f = e() g = pi # (g's "type" in Sage is symbolic expression. It's supposed to be 3.1415....etc) i = lambda x: x*x I gather that e is a class which means it is mutable (Does an immutable class make sense? Can't all classes be modified?). Since f is an instance

Slow performance from ImmutableList<T> Remove method in Microsoft.Bcl.Immutable

耗尽温柔 提交于 2019-12-21 17:17:14
问题 Experiencing some unexpected performance from Microsoft ImmutableList from NuGet package Microsoft.Bcl.Immutable version 1.0.34 and also 1.1.22-beta When removing items from the immutable list the performance is very slow. For an ImmutableList containing 20000 integer values (1...20000) if starting to remove from value 20000 to 1 it takes about 52 seconds to remove all items from the list. If I do the same with a generic List<T> where I create a copy of the list after each remove operation it

Scala: Contains in mutable and immutable sets

倾然丶 夕夏残阳落幕 提交于 2019-12-21 09:19:18
问题 I've discovered a strange behavior for mutable sets which I cannot understand: I have a object which I want to add to a set. The equals method for the class is overridden. When I add two different objects to the set, which produces the same output for equals method, I get a different behavior between mutable and immutable sets for the contains method. Here is the code snippet: class Test(text:String){ override def equals(obj:Any) = obj match { case t: Test => if (t.text == this.text) true

Do effectively immutable objects make sense?

我是研究僧i 提交于 2019-12-21 08:00:19
问题 In the book Java Concurrency In Practice it explains the advantages of "effectively immutable" objects versus mutable objects concurrency-wise. But it does not explain what advantage "effectively immutables" objects would offer over really immutable objects. And I don't get it: can't you always build a really immutable object at the moment you'd decide to publish safely an "effectively immutable" object? (instead of doing your "safe publication" you'd build a really immutable object and that

How to bestow string-ness on my class?

人走茶凉 提交于 2019-12-21 05:08:15
问题 I want a string with one additional attribute, let's say whether to print it in red or green. Subclassing(str) does not work, as it is immutable. I see the value, but it can be annoying. Can multiple inheritence help? I never used that. Inheriting only object and using self.value=str means I have to implement all string-ness messages (like strip) myself. Or is there a way to forward them, like Ruby's missing_method? I think using a class-level dictionary indexed by instance to store the color