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?
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.
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).
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.
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.
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
.
oldlist
and list
are two references that point to the same array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With