immutability

Confusion on string immutability

吃可爱长大的小学妹 提交于 2019-12-06 12:27:57
I have following code:- public class StaticImplementer { private static String str= "ABC"; public static void main(String args[]) { str = str + "XYZ"; } } Questions:- Here String str is static, then where this object will be stored in memory? Since String is immutable, where the object for "XYZ" will be stored? Where will be final object will be Stored? And how will garbage collection will be done? Here String str is static, then where this object will be stored in memory? String str is not an object, it's a reference to an object. "ABC" , "XYZ" & "ABCXYZ" are three distinct String objects.

Is there a convention that objects created using the Builder pattern be immutable?

折月煮酒 提交于 2019-12-06 11:30:50
According to the book Design Patterns: Elements of Reusable Object-Oriented Software,: The Builder Pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. In general the Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object. With the builder pattern we go to have a build method to generate an object witch is immutable. My Question: Can I

Make Object Immutable at Runtime [C#]

爷,独闯天下 提交于 2019-12-06 09:52:36
Is there any way (utilizing Reflection I hope) that I can make an instantiated object immutable along with all of its public properties ? I have a class from someone else's codebase (no source available) that I need to utilize and I basically want an exception to be thrown if any piece of code anywhere tries to call a public setter within this class after it has been instantiated. Note: I do not want to create a wrapper object around the class in order to implement this. I am lazy. No there is not via reflection. Type definitions cannot be altered at runtime via reflection and hence it cannot

Can I have StringBuilder in an immutable class

房东的猫 提交于 2019-12-06 09:38:34
If I create an immutable class. All the fields have to be final. If I use stringbuilder in that like this final StringBuilder s = new StringBuilder("Hello "); , then the append function can append the value of the s and the class wont be immutable. Please advice. It's "shallow-immutable" in that you can't change the fields themselves, but it's not fully immutable - which means you lose pretty much all the benefits associated with immutability. Basically to achieve immutability, all the constituent parts must either be naturally immutable, or sometimes you can get away with using something

When does pandas do pass-by-reference Vs pass-by-value when passing dataframe to a function?

烈酒焚心 提交于 2019-12-06 09:26:21
问题 def dropdf_copy(df): df = df.drop('y',axis=1) def dropdf_inplace(df): df.drop('y',axis=1,inplace=True) def changecell(df): df['y'][0] = 99 x = pd.DataFrame({'x': [1,2],'y': [20,31]}) x Out[204]: x y 0 1 20 1 2 31 dropdf_copy(x) x Out[206]: x y 0 1 20 1 2 31 changecell(x) x Out[208]: x y 0 1 99 1 2 31 In the above example dropdf() doesnt modify the original dataframe x while changecell() modifies x. I know if I add the minor change to changecell() it wont change x. def changecell(df): df = df

Synchronize to ensure that reference to immutable object will be seen by another thread

有些话、适合烂在心里 提交于 2019-12-06 09:02:23
问题 I was studying this to understand the behavior of final fields in the new JMM (5 onwards). This concept is clear: guaranteed visibility of initialized final fields to all threads after the object is properly constructed. But then at the end of the section, I read this, which simply confuses me: Now, having said all of this, if, after a thread constructs an immutable object (that is, an object that only contains final fields), you want to ensure that it is seen correctly by all of the other

Using F# Datatypes in C#

北战南征 提交于 2019-12-06 08:57:29
More particularly, I really want an immutable/shared linked list, and I think having immutable maps and sets would be nice too. As long as I don't have to worry about the core implementation, I can easily add extension methods/subclass/wrap it to provide a reasonably slick external interface for myself to use. Is there any reason I shouldn't do this? Performance, incompatibility, etc.? The types in the F# library (such as Set , Map and list ) were not designed to be used from C#, so I wouldn't generally recommend using them directly. It can be done and some basic operations will work well (e.g

Pointers Arrays and Read Only Memory in C

烈酒焚心 提交于 2019-12-06 08:36:48
I am learning the C programming language and am running into difficulties in understanding the minute differences between pointers, arrays and read only memory. I am working with the following example: char *myCards = "JQK"; From what I understand what this line of code accomplishes is it creates a null terminated string of characters in a read only section of memory that is accessible via the char pointer myCards . This means that I am able to compile the following but would receive an error due to the immutability of the string: *myCards = 'A'; I am trying to achieve a similar functionality

Practical example for Immutable class

末鹿安然 提交于 2019-12-06 07:46:52
问题 It is obvious that immutability increases the re-usability since it creates new object in each state change.Can somebody tells me a practical scenario where we need a immutable class ? 回答1: Consider java.lang.String . If it weren't immutable, every time you ever have a string you want to be confident wouldn't change underneath you, you'd have to create a copy. Another example is collections: it's nice to be able to accept or return a genuinely immutable collection (e.g. from Guava - not just

Why isn't “is” comparison used in place of “==” for primitive types?

旧街凉风 提交于 2019-12-06 06:24:32
问题 When I'm using Pytest for Python formatting, it complains about doing something like: >>> assert some_function_ret_val() == True E712 comparison to True should be 'if cond is True:' or 'if cond:' and wants: assert some_function_ret_val() is True I know there can only be one copy of True/False/None, but I thought all primitives are immutable types. Under what circumstances would "==" and "is" comparison be different for primitive types?? Otherwise, why has "==" become the norm in comparison