0
boolean temp1,temp2;

        temp1 = true;
        temp2 = temp1;
        Log.d("rvg", "temp1:"+temp1+"    temp2:"+temp2);

        temp1 = false;
        Log.d("rvg", "temp1:"+temp1+"    temp2:"+temp2);

When i execute this, i got

temp1:true    temp2:true
temp1:false      temp2:true

But i've Changed to array

boolean[] Temp1,Temp2;
        Temp1 = new boolean[1];
        Temp2 = new boolean[1];
        Temp1[0] = true;
        Temp2   = Temp1;

        Log.d("rvg", "Temp1:"+Temp1[0]+"    Temp2:"+Temp2[0]);
        Temp1[0] = false;
        Log.d("rvg", "Temp1:"+Temp1[0]+"    Temp2:"+Temp2[0]);

When i execute this, i got same value for Temp1 and Temp 2.

Temp1:true  Temp2:true
Temp1:false     Temp2:false

What to do to avoid this?

2
  • Use Temp2[0] = Temp1[0]; instead of Temp2 = Temp1; Commented Jul 10, 2013 at 10:39
  • how about copying values from temp1 to temp2 by iterating in a loop not by directly this Temp2[] = Temp1, which causes to copying by reference, so you are implicitly binding them which causes to affect each other Commented Jul 10, 2013 at 10:39

6 Answers 6

2

You need to copy the array. Instead of

Temp2 = Temp1;

do

Temp2 = Arrays.copyOf(Temp1, Temp1.length);
Sign up to request clarification or add additional context in comments.

Comments

1

In the first case temp2 reads temp1 (true) and sets the true value in it. In the second case you're assigning the same memory to both arrays so the values inside them are the same.

Solutions:

Temp2[0]   = Temp1[0];

or

Temp2 = Arrays.copyOf(Temp1, Temp1.length);

The first case will read the value inside temp1[0] and will assign it to temp2[0]. In the second case, Temp2 will be a new copy of Temp1, without sharing the same memory so you can independently modify each

Comments

1

Array variables are references. When you say Temp2 = Temp1; you assign reference to array named Temp2 into variable Temp1, so that both refer to the same array. Not equal, the same. Therefore all changes done in this array using one of the references are visible when you use other reference. This is why you get the same results.

Comments

0

Short answer: you cannot avoid this. Since an array in Java is basically an object (or at least treated as such), Temp2 = Temp1; will have Temp2 point to the same block of memory as Temp1. So if you change Temp1, Temp2 will reflect those changes.

The reason it behaves different when using the primitive type boolean is that temp2 = temp1; make a copy of the value of temp1 instead. But temp2 still has its own block of memory.

In order to have the same behaviour with arrays as with primitive types, you could use Arrays.copyOf(boolean[], int) instead. It will work regardless of the size of the array.

Comments

0

I don't understand what's wrong with the code you posted and what you'd like to achieve.

However I'm pretty sure the issue is related to the fact that a boolean is a primitive data type in java. This means that each boolean variables stores a value and that the = operator copies the value from a variable to another.

On the opposite, when you use an array you have the two variables that are the reference to the same memory location. So when you change the value of one variable you observe the change propagate to the other too because you're reading from the same memory location.

Comments

0

In the first example temp1 and temp2 are primitive types (not objects). When you assign temp1 to temp2 you only assign the value, but there are two different variables, temp1 and temp2. If you change the value of one, the other doesn't change.

In the second case, Temp1 and Temp2 are arrays (objects). When you assign Temp1 to Temp2 you are saying that Temp2 is a pointer to the same object that Temp1 points to: same object, two variables to access it. If you change that object accessing by Temp1 or accessing by Temp2 you're changing the same object.

If you want to avoid that, you have to maintain two different objects and change only the values, i.e:

Temp2[0]=Temp1[0]
instead

Temp2=Temp1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.