Answer the question
In order to leave comments, you need to log in
All the same, in java passes an array by reference or not?
Everywhere they write that java passes primitives by value, and objects by reference.
And they also write that an array is an object passed by reference.
So if it should be passed by reference then why do I have an array passed by value?
import java.util.Arrays;
public class ssulk
{
public static void main(String[] args)
{
int[][] a = new int[2][2];
a[0][0]=1;
a[0][1]=2;
a[1][0]=3;
a[1][1]=4;
System.out.println("1 "+a[0][0]);
a[0][0]=a[1][1];
System.out.println("2 "+a[0][0]);
put(a[0],a[1]);
System.out.println("3 "+a[0][0]);
}
static void put(int[] a,int[] b)
{
System.out.println("4 "+a[0]);
a=b;
//a=Arrays.copyOf(b, 2);
System.out.println("5 "+a[0]);
}
}
Answer the question
In order to leave comments, you need to log in
Everything worked correctly. An array is an object and is passed
by reference. But the reference is passed by value.
a=b;
here you are already manipulating copies of references inside the method.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question