Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   I need help with pointers and passing-by-refrence (https://www.askmehelpdesk.com/showthread.php?t=808506)

  • Feb 20, 2015, 01:36 PM
    spiderman20084
    I need help with pointers and passing-by-refrence
    Hello
    I've been working on a program and I've encountered a little problem
    Here is the simplest form of my problem
    Code:

    public static void main(String[] args) {                int[] a = new int[]{1,2,3};
                    trial3(a);
            }
           
            private static void trial3(int[] i) {
                    int[] b = i;
                    b[1] = 0;
                    System.out.println(Arrays.toString(b)+ Arrays.toString(i));
            }

    I expect the result would be [1, 0, 3][1, 2, 3] but instead the real result is
    [1, 0, 3][1, 0, 3]
    I don't really know why this happen could someone please explain it to me and
    Tell me a simple way to get the expected result.

  • All times are GMT -7. The time now is 02:00 AM.