I know that “mutable” and “immutable” are terms that should be used to describe the ability of objects to change meaning in object-oriented languages such as Java and Objective C. However, I would like to raise it because it refers to mine a question about these primitives. I know that when I change the value of a variable containing an immutable object, I actually create a new object. It is interesting, however, if primitive data in C behaves similarly to immutable objects. By this I mean that when I change the value of a variable containing primitive data, new data is created and refers to the variable. Or did existing existing primitives actually mutate / modify stored data values?
Edit # 1:
Problem # 1: I would like to clarify some misunderstandings (for my part or with others), because I did not make it clear when I said: "When I change the value of a variable while holding an immutable object, I actually create a new object." When I said this, I did not want to assign a variable to an existing object. For instance:
-------------------------
String x = "Hello World";
String y = x;
-------------------------
-------------------------
String x = "Hello World";
System.out.println(x);
x = "Goodbye World";
System.out.println(x);
-------------------------
Of course, by assigning the variable y to the variable x, as in Example 1, which was the case when you guys brought up, only the reference variable y refers to the object referenced by x. Of course, in this case there is no new object; only the same "Hello World" object referenced by 2 variables.
, x = "Goodbye World" 2, x String "Goodbye World" String "Hello World" , x . String - Immutable Java. String - OR String. ( "Goodbye World" String ), String x . ? , , .
№ 2: , Ulfalizer:
1) 2 , :
a) " " - C, Java Objective C . : int x = 1. x , integer 1.
b) "" - Java, () . : String x = "Hello World" . x / " - , " Hello World " .
2) C, Java Objective C " " . , :
int x = 10;
x = 2;
x ( aka-memory) 10 2. , , " " , /.
3) C , "* - ". Ulfalizer: int * ptr. ptr - , (aka ), : ptr = & x. x : int x = 10, x , 10. ,
-------------------------
int x;
int *ptr;
ptr = &x;
*ptr = 1;
-------------------------
, x ( aka - ) - ptr.
, / . .