immutability

Is Integer Immutable

耗尽温柔 提交于 2019-11-26 17:15:25
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? Travis Webb Immutable does not mean that a can never equal another value. For example, String is immutable too, but I can still do this: String str =

Examples of immutable classes

旧巷老猫 提交于 2019-11-26 17:07:16
I already know the definition of immutable classes but I need a few examples. Paŭlo Ebermann Some famous immutable classes in the Standard API: java.lang.String (already mentioned) The wrapper classes for the primitive types: java.lang.Integer, java.lang.Byte, java.lang.Character, java.lang.Short, java.lang.Boolean, java.lang.Long, java.lang.Double, java.lang.Float java.lang.StackTraceElement (used in building exception stacktraces) Most enum classes are immutable, but this in fact depends on the concrete case. (Don't implement mutable enums, this will screw you up somewhen.) I think that at

Will the jit optimize new objects

人盡茶涼 提交于 2019-11-26 17:07:04
问题 I created this class for being immutable and having a fluent API: public final class Message { public final String email; public final String escalationEmail; public final String assignee; public final String conversationId; public final String subject; public final String userId; public Message(String email, String escalationEmail, String assignee, String conversationId, String subject, String userId) { this.email = email; this.escalationEmail = escalationEmail; this.assignee = assignee;

In Rust, what's the difference between “shadowing” and “mutability”?

霸气de小男生 提交于 2019-11-26 17:04:48
问题 In Chapter 3 of the Rust Book, Variables and Mutability , we go through a couple iterations on this theme in order to demonstrate the default, immutable behavior of variables in Rust: fn main() { let x = 5; println!("The value of x is {}", x); x = 6; println!("The value of x is {}", x); } Which outputs: error[E0384]: cannot assign twice to immutable variable `x` --> src/main.rs:4:5 | 2 | let x = 5; | - | | | first assignment to `x` | help: make this binding mutable: `mut x` 3 | println!("The

Java Immutable Collections

守給你的承諾、 提交于 2019-11-26 17:04:09
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 that are referred to in the second line? Is it referring to the changes in the elements held in the

Are strings mutable in Ruby?

若如初见. 提交于 2019-11-26 16:39:26
问题 Are Strings mutable in Ruby? According to the documentation doing str = "hello" str = str + " world" creates a new string object with the value "hello world" but when we do str = "hello" str << " world" It does not mention that it creates a new object, so does it mutate the str object, which will now have the value "hello world" ? 回答1: Yes, << mutates the same object, and + creates a new one. Demonstration: irb(main):011:0> str = "hello" => "hello" irb(main):012:0> str.object_id => 22269036

Does Haskell have variables?

风流意气都作罢 提交于 2019-11-26 16:05:41
问题 I've frequently heard claims that Haskell doesn't have variables; in particular, this answer claims that it doesn't, and it was upvoted at least nine times and accepted. So does it have variables or not, and why? This question also appears to apply ML, F#, OCaml, Erlang, Oz, Lava, and all SSA intermediate languages. 回答1: Haskell has immutable variables (variables in the math sense) by default: foo x y = x + y * 2 By default variables are not mutable cells . Haskell also has mutable cells

Make immutable Java object

浪子不回头ぞ 提交于 2019-11-26 15:53:47
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? assylias Your class is not immutable strictly speaking, it is only effectively immutable. To make it immutable, you need to use final : private final String

Hashable, immutable

余生颓废 提交于 2019-11-26 15:44:42
From a recent SO question (see Create a dictionary in python which is indexed by lists ) I realized I probably had a wrong conception of the meaning of hashable and immutable objects in python. What does hashable mean in practice? What is the relation between hashable and immmutable? Are there mutable objects that are hashable or immutable objects that are not hashable? Hashing is the process of converting some large amount of data into a much smaller amount (typically a single integer) in a repeatable way so that it can be looked up in a table in constant-time ( O(1) ), which is important for

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

一曲冷凌霜 提交于 2019-11-26 15:30:57
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 --- return bar; } pub fn get_foo_mut(&mut self) -> &mut Bar { // ~20 lines of code // (exactly matching