#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2

    Copying arrays in Java


    Hi guys.
    Simple question:
    I just found out that the System class has an arraycopy() method.
    Why do anyone need it if they can just use the assignment operator to copy one array to another?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Originally Posted by so.very.tired
    Hi guys.
    Simple question:
    I just found out that the System class has an arraycopy() method.
    Why do anyone need it if they can just use the assignment operator to copy one array to another?
    When you assign one array variable to another, you're simply asking the new variable to hold the address to the same array object. When you arraycopy, you're "copying" the data to another memory location and the new variable now refers to this location.

    Look at the below two examples and may be you'll understand better:

    Code:
    package com.test;
    
    public class TestClass1 {
        public static void main (String[] args) {
            int[] x = {1,2,3};
    
            // Assignment y = x;
            int[] y = x;
            
            x[1] = 4; // Mutating x
            // Observe that both the for loops print the same
            for (int i: x) {
                System.out.println(i);
            }
            System.out.println();
            for (int i: y) {
                System.out.println(i);
            }
        }
    }


    Code:
    package com.test;
    
    public class TestClass1 {
        public static void main (String[] args) {
            int[] x = {1,2,3};
            int[] y = new int[3];
            
            System.arraycopy(x, 0, y, 0, 3);
            x[1] = 4;
            // observe that both the for loops print different data
            for (int i: x) {
                System.out.println(i);
            }
            System.out.println();
            for (int i: y) {
                System.out.println(i);
            }
        }
    }

    Comments on this post

    • stdunbar agrees : Nice description and examples!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    Thanks noobie1000.

    So using the assignment operator is kinda like shallow copying reference variables.
    does that mean arrays that declared with the ' [] ' parentheses "beahave" like objects?
    Last edited by so.very.tired; September 7th, 2013 at 01:01 PM.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    arrays that declared with the ' [] ' parentheses "behave" like objects?
    Yes arrays have some of the properties of objects. Their reference variables can have null values and they have the .length field.

IMN logo majestic logo threadwatch logo seochat tools logo