views:

493

answers:

5

Hello,

I have a questions about changing the values of variables in methods in Java.

I have the following example class:

public class Test {

    public static void funk(int a, int[] b) { 
     b[0] = b[0] * 2; 
     a = b[0] + 5;
    } 

    public static void main(String[] args) {

     int bird = 10;
     int[] tiger = {7};

     Test.funk(bird, tiger);

    }

}

After the execution of the method Test.funk(bird, tiger), the value of bird is not changed - it remains 10, though in the funk() method we have the following statement:

a = b[0] + 5;

On the other hand, the value of the element in the array changes, because we have this statement:

b[0] = b[0] * 2;

However, I don't understand why one thing changes and the other not ?

Thanks a lot.

Regards

+4  A: 

This should help answer your question: Is Java pass by reference?

matt b
+3  A: 

Look at Jon Skeet's article about Parameter-Passing in Java, which explains this.

In short (look at his site for a more throughout explanation):

Arrays are reference types. If you pass a reference that points to an array, the value of the reference is copied and assigned to the parameter of the function. So the parameter will point to the same array as the argument that was passed. Thus changes you make to the array through the parameter of your function will be visible in the calling function. Changing the parameter itself (b), for example by setting it to null, however, will not be noticed by the calling function, since the parameter (b) is just a copy of the argument (tiger) passed.

Integers are so-called primitive types. Passing the integer copies its value and assigns it to the parameter too. But that value is not a reference to the actual data, but is the data itself. So changes to the paramter in the function will affect the parameter (a), but not the argument passed in the calling function (bird).

Johannes Schaub - litb
+2  A: 

See: Passing Information to a Method or a Constructor.

Fabian Steeg
+1  A: 

That's because when you declare

 public static void funk(int a, int[] b)

The scope of the variable a is only that method. Then when you change the value, you are changing only the value of that variable in that method.

About the b. Thats a new object reference to the same array created in main, that's why it seems the value does change ( what is changing is the array object underneath )

But try this:

public static void funk(int a, int[] b) { 
    // create a new reference for b
    int[] c = new int[b.length];
    c[0] = b[0];
    b = c;

    // The same.
    b[0] = b[0] * 2; 
    a = b[0] + 5;
}

When you do that you'll the the value of tiger didn't change either ( only the content of the new array c created in funk )

You can simulate the pass by ref using a wrapper. See this post.

Although I haven't got any comments about that

EDIT Just for fun:

I have modified your code to use the wrapper posted above.

It looks pretty strange but looks like it works.

// By ref simulated.
public class Test {

    public static void funk(_<Integer> a, int[] b) { 
        b[0] = b[0] * 2; 
        a.s(  b[0] + 5 ) ;
    } 

    public static void main(String[] args) {
        _<Integer> bird = new _<Integer>(10);
        int[] tiger = {7};

        Test.funk( bird , tiger );

        System.out.println("bird = " + bird );
        System.out.println("tiger = " + tiger[0] );

    }

}

Prints

bird = 19
tiger = 14

:-S

OscarRyz
A: 

Basically, objects (like arrays) are passed into methods "by reference". So when you change the object, it changes the same object that was passed into the method.

Primitives (like int) are "passed by value", so the variable you are assigning a value to in a is not the same as the int variable that was passed in.

I hope this helps...

stephendl
No, it doesn't help. Java is ALWAYS pass-by-value. Read the links provided in the other answers; you need to understand this.
Alan Moore
For this example my 'basic' explanation is fine. I made it clear that it was a basic explanation. If the questioner wants to read a more in depth explanation the links are there for him. I have no way of telling how much detail he requires, so thought he might appreciate the choice.
stephendl
Yes, I did appreciate it. I also read the links, but shorter explanations are also helpful to get you started. Thank you all.