Results 1 to 3 of 3
  1. #1
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

    [Edit]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.
    Last edited by pbrockway2; 11-08-2012 at 08:09 AM.

Similar Threads

  1. Comparing elements in two arrays
    By Adomini in forum New To Java
    Replies: 3
    Last Post: 10-21-2012, 07:06 PM
  2. Array of Arrays all unique elements
    By dusker in forum New To Java
    Replies: 9
    Last Post: 04-02-2011, 07:21 PM
  3. copy arrays without using arraycopy Method !
    By M7MD in forum Introductions
    Replies: 0
    Last Post: 10-10-2010, 06:59 PM
  4. Replies: 16
    Last Post: 07-08-2010, 09:13 PM
  5. Replies: 5
    Last Post: 01-18-2010, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •