1

Possible Duplicate : Is Java “pass-by-reference” or “pass-by-value”?

i am trying to do initialization of an array in this way -

class Main {
public static void main(String[] args) {
    int n = 4;
    int[] s = null;
    int[] f = null;
    init(n, s, f);
    System.out.println(s.length);
}
public static void init(int n, int[] s, int[] f) {
    s = new int[n];
    f = new int[n];
}
}

but i am getting NullPointerException for the s.length. so this clear passing the parameter to another method is pass by value. therefor objects are not initialize. but when i do swap in to value in a different method then this is working. like -

class Main {
public static void main(String[] args) {
    int n = 4;
    int[] s = new int[n];
    int[] f = new int[n];
    s[0] = 10;
    f[0] = 20;
    System.out.println(s[0] + " " + f[0]);
    swap(s, f);
    System.out.println(s[0] + " " + f[0]);
}
public static void swap(int[] s, int[] f) {
    int t = s[0];
    s[0] = f[0];
    f[0] = t;
}
}

on the other hand this isn't swap values.

class Main {
public static void main(String[] args) {
    Integer a = 10;
    Integer b = 20;
    swap(a , b);
    System.out.println(a + " " + b);
}
public static void swap(Integer a, Integer b) {
    int t = a;
    a = b;
    b = t;
}
}

therefor i need some explanation whether java “pass-by-reference” or “pass-by-value”? this explanation could help me to understand why it is not possible to create object in another method.

sorry for my bad English. thanks in advance.

Community
  • 1
  • 1
seal
  • 1,122
  • 5
  • 19
  • 37
  • One sets a value, one initializes something (by reassigning). Big difference when it comes to pass by value of reference. – keyser Dec 07 '14 at 20:16
  • primitives are passed by value (copy is given), and objects have their pointers passed by value. – EpicPandaForce Dec 07 '14 at 20:16

1 Answers1

4

This won't work because Java has pass by value, not pass by reference.

When you declare

int[] s = null;

then you have a (null) reference to an int[]. But when you pass s to another method, what happens is that a new variable (let's call it x) is created, and the reference s is copied into it. Any changes to what x references will also change what s references, because they're the same thing; but changes to x itself won't change s, because x and s are two different references to the same thing.

Think of it like this. Your s is an entry on a clipboard, and the clipboard entry tells you the number of the box to look in to find the int[]. When you pass s to a method, you get a new entry x on the clipboard, with the same box number as was under the s reference. That means that any operation that looks in the box and changes its contents will affect what you get when you look at what s refers to: they both look at the same box number. But if you change the entry on the clipboard for x, it won't change the entry on the clipboard for s, because they're two different entries.

As another example, think of s and x as being road signs pointing to the same town. If you change the town that x points to, e.g. by introducing a one way system in the town centre, then of course what s points to will also have changed. But if you change the road sign x, and write a different town's name on it, that won't automatically change the road sign s too. They're different signs, but signifying the same thing.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
  • so basically in the first segment of code, `init` function `s = new int[n]` creates different object that is not pointing my passed parameter from the caller function. that is understandable. thanks a ton. – seal Dec 07 '14 at 20:34