# If a and b are both int arrays, will a = b copy all elements of b into a?

• 11-08-2012, 05:44 AM
psx2514
If a and b are both int arrays, will a = b copy all elements of b into a?
I put that on a multiple choice test, and it got marked wrong, but I read otherwise that it's right. This is a simple yes or no question. Please just answer with yes or no. I'm just trying to figure out if there's a mistake on my test.
• 11-08-2012, 05:47 AM
Fubarable
Re: If a and b are both int arrays, will a = b copy all elements of b into a?
The mistake on your test is your answer. The correct answer is no. a = b will not copy all elements of b into a. There is no copying being done here. a is simply being referred to the same array that b refers to.
• 11-08-2012, 08:54 AM
pbrockway2
Re: If a and b are both int arrays, will a = b copy all elements of b into a?
= does assignment, not copying. Whatever value the right hand side evaluates to is assigned to the variable on the left hand side. Even if you were to regard *that* as a sort of copying the right hand side expression only evaluates to a single value.

Another observation: a and b aren't arrays into or from which things are copied. (although we are all lazy and speak that way). They are variables whose values are references to arrays. Like signposts. Suppose at dead of night someone takes a sign pointing to Aton and swivels it around so that it points to the place that Bgrad signs usually point to - the inhabitants of Bgrad sleep on unaware of the change and undisturbed by it.