When changing the value of a variable in C, is the new primitive created or the current primitive mutated?

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:

// Example 1: I did not mean this
-------------------------
String x = "Hello World";
String y = x;
-------------------------

// Example 2: What I meant is this
-------------------------
String x = "Hello World";
//This will print out "Hello World"
System.out.println(x);

x = "Goodbye World";
//This will print out "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.

, / . .

+4
2

, C, - . - , . .

C /

int x;

int x. ( . .)

x = 7;

7 . ,

x = y;

int, y x. (, y int.)

, int s.

Java ( - ) . - C, - - , , - ( ) .

( , .)

& C, &x x, . * . , :

int x;
int y;
/* Declares 'ptr' as a pointer, and says that it points to an int.
   The pointed-to type is used by the compiler for type checking
   and type conversions. */
int *ptr;

ptr = &x; // Store the address of 'x' in 'ptr'.
*ptr = 1; // Store 1 into the memory 'ptr' points to ('x').
ptr = &y; // Store the address of 'y' in 'ptr'.
*ptr = 2; // Store 2 into the memory 'ptr' points to ('y').

x 1 y 2. , , , , , .

(C99 // - .)

(: Java , - . , . .)

№1

x = "Goodbye World";

x String " ". , String , , , x ( ).

. .

№2

, , C .

( , "ptr - ", , "*ptr ". C, * next (IMO), int* ptr; . , .)

Java, , . , JVM ( ), , . - "" , , - . , JVM, - , , .

Java ( , , ), , .

,

x = 1;

1 x. .

, a union C (, a int ) , . , .

+6

. , .

#include <stdio.h>

int main(void) {
    // your code goes here
    int a = 5;

    printf ("%p = %i\n", (void*) &a, a);
    a = 10;
    printf ("%p = %i\n", (void*) &a, a);

    return 0;
}
+1

All Articles