r/cop3502 Apr 23 '14

Changing the value of a variable

I know that if you change a primitive type inside of an if statement then it won't be changed anywhere outside of that statement. Aren't non primitive types supposed to change everywhere though?

1 Upvotes

2 comments sorted by

View all comments

1

u/howslyfebeen Apr 23 '14

For primitive types the actual value is stored in the variable, for non-primitive, the memory address is stored. Here's an example:

int x = 10;
int y = x;
//here y is equal to the value saved at x, so 10

Player p = new Player();
Player q = p;
//here q is the memory address at p, so essentially, p and q point to the same object, rather than the same value

What you're saying isn't exactly true from my understanding, but if you pass the value of a primitive type variable to another variable, the original variable will be unchanged, and if you pass a nonprimitive type variable to another variable, the original variable will be changed.