This question already has answers here:
Why is String immutable in Java?
(13 answers)
Closed 7 years ago.
I'm new to java programming. And I don't understand why string objects are immutable in java.
String a = "Vehicle";
Because java uses the concept of string literal. Suppose there are five reference variables, all refers to one object "Vehicle".If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.
Related
This question already has an answer here:
What is the rationale behind allowing variable shadowing in Rust? [closed]
(1 answer)
Closed 2 years ago.
First time I am encountering a typed language allowing to declare a variable name twice in the same scope. Wouldn't there be a chance to override an existing variable by mistake? What advantage does it bring?
There is a chapter in the book about this.
Shadowing is different from marking a variable as mut, because we’ll get a compile-time error if we accidentally try to reassign to this variable without using the let keyword. By using let, we can perform a few transformations on a value but have the variable be immutable after those transformations have been completed.
The other difference between mut and shadowing is that because we’re effectively creating a new variable when we use the let keyword again, we can change the type of the value but reuse the same name. For example, say our program asks a user to show how many spaces they want between some text by inputting space characters, but we really want to store that input as a number
let spaces = " "; // String
let spaces = spaces.len(); // number
In short, it allows you to "modify" a value, in a way that is technically immutable. Rust ensures that you cannot use the shadowed variable, so it's perfectly typesafe.
I'm no Rust expert, but from a language design perspective it's an interesting thing to encourage. But I think the point is to discourage the use of mutable values whenever possible by allowing you to immutably override a name with a new type and value.
This question already has answers here:
How do I implement Copy and Clone for a type that contains a String (or any type that doesn't implement Copy)?
(2 answers)
What is the difference between Copy and Clone?
(5 answers)
Closed 3 years ago.
An article that I read recently stated that a struct that holds a Box<T> as a field is not Copy.
This confuses me, since I thought that any type whose size is known can be stored on the stack - and is therefore Copy.
Isn't the Box's size always the same? I thought that it was just a reference to a heap allocated memory - and does therefore have always the same size.
This question already has answers here:
How do I create a global, mutable singleton?
(7 answers)
Closed 3 years ago.
Is there a way I can create a static String in Rust?
I tried this:
static somestring: String = String::new();
but I got this error:
error: `std::string::String::new` is not yet stable as a const fn
--> src/main.rs:2:29
|
2 | static somestring: String = String::new();
|
How to create a static string at compile time does not solve my problem because it's about &'static str, not String. I need the String to be globally addressable.
Don't confuse the String type with the str type.
What are the differences between Rust's `String` and `str`?
A String is mutable and always heap-allocated.
A str, usually presented as &str is not mutable and is simply a view into a string.
Your question seems to confuse the idea of static and globally adressable. You may refer to (&'static str) as a string with a 'static lifetime. Strings with 'static lifetimes are common because they (mostly) represent hardcoded strings in the program. A 'static lifetime means the object (in this case, a string) will live until the end of the program. Most 'static objects are known at compile time. This should be natural to think about, because hardcoded strings are known at compile time. These strings can not mutate.
Strings on the other hand, are mutable. And you can make a String from a &'static str.
Given the lack of context in the question, if what you want is a String that is globally adressable and want to define it static I may suggest the macro lazy-static:
https://crates.io/crates/lazy_static
As sugested by mcarton, this question seems to boil down to the singleton pattern. You can learn more about its implementation in Rust here: How do I create a global, mutable singleton?
This question already has answers here:
How can you make a safe static singleton in Rust?
(3 answers)
How do I create a global, mutable singleton?
(7 answers)
Closed 5 years ago.
Is is possible to define a global constant, value of which is computed at the beginning of the runtime? Something like
static START_TIME: time::Timespec = time::get_time();
if it were possible. static and const declaration requires compile time value (calls in constants are limited to struct and enum constructors) and let can't be put outside function (error: expected item, found `let`).
I think something like lazy_static can help with this.
This question already has answers here:
How many objects are created
(4 answers)
Closed 9 years ago.
for this statement
String a="MAM"+"BCD"+"EFG"+"GFE";
How many objects will be created? (I am confused is it created 4 or 5 or 7)
Most smart compilers will realize that concatenated string constants can be concatenated at compile time. If your compiler chooses to make that optimization, the answer is one.
Otherwise, you have each of your literal strings, plus one for each concatenation. Without the optimization, the answer is 7 because you have 4 strings and 3 +es.
If you're talking about Java, the answer is one, at compile time, as specified in the JLS.
If you're not, the question is unanswerable as posed.
Only one object will be created. In this case because the plus sign is used to concatenate "string literals". :)