Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incorrect variable change in java

Tags:

java

variables

I have a problem in Eclipse. Why is the value of oldList different in LogCat while I don't change it between the tow Log command?

First I have an initialize method:

private void initialize() {

    list[0][0] = 2;
    list[0][1] = 4;
    list[1][0] = 3;
    list[1][1] = 7;

    oldList = list;

    going();
}

and in the going method, I printed oldList twice :

private void going() {

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            Log.i("Log", "oldList = " + oldList[i][j]);
        }
    }
    Log.i("Log", "---------------------------");

    //    ----------------------------------------------------
    list[0][0] = 0;
    list[0][1] = 5;

    list[1][0] = 0;
    list[1][1] = 0;
    //    ----------------------------------------------------

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            Log.i("Log", "oldList = " + oldList[i][j]);
        }
    }
}

but the two results is different in LogCat :

oldList = 2
oldList = 4
oldList = 3
oldList = 7
---------------------------
oldList = 0
oldList = 5
oldList = 0
oldList = 0

While i don't change it between the two logs. I just change the value of list, not oldList. Why does the output change?

like image 630
Reza Avatar asked Oct 07 '15 11:10

Reza


People also ask

Which variable Cannot be changed in Java?

A constant is a variable whose value cannot change once it has been assigned. Java doesn't have built-in support for constants. A constant can make our program more easily read and understood by others.

Can variables in Java be changed?

Remember that a variable holds a value and that value can change or vary. If you use a variable to keep score you would probably increment it (add one to the current value).

How do you make a variable not change in Java?

Using final you can define variables whose values never change. You MUST place an initial value into such a "constant" variable. If you do not place this initial value, Java will never let you assign a value at a later time because you cannot do anything to change the value of a final (constant) variable.

How do you reset a variable in Java?

The java. DoubleAdder. reset() is an inbuilt method in java that resets variables maintaining the sum to zero. When the object of the class is created its initial value is zero.


2 Answers

Both list and oldlist refer to the exact same object. When you run

oldlist = list

you have two different "names" referring to the exact same object in memory. When you assign an object (in your case the array) to a variable, this object will not be copied.

Thus, as you change the list array in your going method, you are changing the object referred to by both list and oldlist.

like image 161
Holger Just Avatar answered Sep 19 '22 00:09

Holger Just


oldlist and list are two references that point to the same array.

like image 38
Andres Avatar answered Sep 19 '22 00:09

Andres