Setting a variable to another

I read somewhere that when you set a variable to another like say x = y, all that is done is the path of y is copied to x, so whenever y changes from thatpoint on, they always stay the same. Idk how true that is, but i seem to be getting an issue involving something like that in the game I am making. To check to see if a variable has changed, i just set a variable to the old value of it, or rather what the screen is drawing, and check it with the current. in the init, and in the fix, i just set the current variables to the old storeage ones, so the path is getting copied, so whenever i change the current one, they change with them. sorry if thats confusing, anyone have any idea of how to fix that?

EDIT: So I’ve verified myself that java copies thel ocation of the variable to the other, linking the too, atleast with a string. I need a fix for this though. I tried adding “” to the end, but then it says that the 2 strings are different, when they are exactly the same. I havnt any idea how to fix this, so any help would be great.

This only happens with objects, not primitives. You will need to create a new object that has the values of the old object.
One way to do this for strings is:


String newString = new String( oldString );

Bad advice is often worse than no advice at all. Since strings are immutable, there’s absolutely no point in your code, other than wasting cpu time and memory.

Zerolife: What you’ve described should be impossible with strings. Post actual code which illustrates the problem. I suspect you’re confused between ‘==’ (tests if two references are the same object) and ‘.equals()’ (tests if two different objects represent the same value/state).

well my issue wasnt the == vs .equals, i had that correct, i even tried the string = new String thing, and that didnt work, so heres my code

I call this to set that variables to make them the same

wep2 = wep; wepid2 = wepid;
body2 = body; bodyid2 = bodyid;
bodyc2= bodyc;

i call this to check the variables to see if they are the same, which they never are

if(!wep2.equals(wep)||!wepid2.equals(wepid)||!body2.equals(body)||!bodyid2.equals(bodyid)||!bodyc2.equals(bodyc))
return true;
return false;

that is the only place they issue shows up. Maybe I missed something small, idk.

Well I found my issue, and now I feel pretty stupid. I was checking the wrong variables for changes. The values I check change, but not quite yet in my game. So I tried updating values that are incapable of being different in my game yet. I don’t know how i managed to skip over that. I fixed it though, and its all working. Thanks for the help though. My issues never seem to be what I think they are -.-