1

New to Java here, please help. How arguments are passed in java? Why am I unable to change argument value in the calling method from within called method?

Code

public class PassTest {
    public static void changeInt(int value)
    {
         value=55;
    }

    int val;
    val=11;
    changeInt(val);
    System.out.println("Int value is:" + val);// calling modifier changeInt 
}

Output

Int value is: 11

why it is not 55..?

Todd
  • 30,472
  • 11
  • 81
  • 89
sum2000
  • 1,363
  • 5
  • 21
  • 36

6 Answers6

5

Java passes by value, not by reference. In your method value contains a copy of the value from val. Modifying the copy does not change the original variable.

You could pass an int wrapped inside an object if you want your changes to be visible to the caller. You can for example use the class org.apache.commons.lang.mutable.MutableInt.

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 4
    Err... More specific about what? Can you be more specific? – Mark Byers Dec 16 '11 at 21:33
  • "You could pass an int wrapped inside an object if you want your changes to be visible to the caller. " exactly what this means.? – sum2000 Dec 16 '11 at 21:36
  • 1
    Can you both specify the specificity of the specific context in question? Specifically the part about how "Modifying the copy does not change the original variable" is not specific. Seems right to me Mark, +1. – Travis J Dec 16 '11 at 21:38
  • @SUMEETJAIN: If you have a class that contains a mutable int (for example the MutableInt class from Apache Commons, or a custom class that you wrote yourself) and you pass this to a method then changes to the integer inside the class can be seen by the caller. See this example code online: http://ideone.com/urhzi – Mark Byers Dec 16 '11 at 21:48
1

Java passes by Value, it makes a copy which is completely dis-associated with the original variable reference, which means it doesn't have access to change the original int. This is true for primitives as well as object references as well.

You can use AtomicInteger or something like it, to achieve what you are desiring to do.

1

Primitive variables are passed by value not reference as you are suggesting.

Jasoneer
  • 2,078
  • 2
  • 15
  • 20
  • 2
    All variables are passed by value, not just primitive. – JB Nizet Dec 16 '11 at 21:33
  • 2
    That is true but in the case of an object the value is the reference. I just didn't want to confuse the OP with a lengthy explanation. – Jasoneer Dec 16 '11 at 21:36
  • @JB Nizet; Java can pass by reference, you pass a pointer to an object instead of the value itself, that allows you to edit the values. not ALL variables are passed by value. – D3_JMultiply Dec 16 '11 at 22:01
  • 1
    @D3_JMultiply: The pointer is still passed by value. If you make the argument point to another object, the original variable still points to the old object. See http://stackoverflow.com/questions/40480/is-java-pass-by-reference – JB Nizet Dec 16 '11 at 22:29
  • Pointers are passed by value, yes, however they POINT TO AN OBJECT, when you edit that pointer it affects the object that the pointer points to, which has the same exact effect as if you passed the object by reference. http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html this says the exact same thing I just said, only in different, probably more clearer, words. – D3_JMultiply Dec 16 '11 at 23:54
  • 1
    @D3 Not true. If you pass an object by reference you can change the reference to a completely different object and you'll see the effect when returning from the function. Now try that in Java. The link is correct though - but then it disagrees with your interpretation completely. – Voo Dec 17 '11 at 00:54
  • I think you're just completely not understanding my words, I'm saying the exact same thing as the page, only stating it differently, sorry if it's not seeming to make sense... Yes, if you change the object completely of course it loses the pointer, but if you only change the pointers attributes it affects the pointed object. – D3_JMultiply Dec 17 '11 at 14:57
1

As others said, Java passes byValue by default which means that you are just getting a copy in the function. You can pass byReference, which will pass a pointer to the object and allow you to directly edit but this is not seen as best practice. I would suggest doing it like this:

public class PassTest {
 public int changeInt(int value)
 {
  value = 55;
  return value;
 }
int val;

val=11;
val = changeInt(val);
System.out.println("Int value is:" + val);// calling modifier changeInt 
Travis J
  • 81,153
  • 41
  • 202
  • 273
1

Java passes ByValue, meaning the value of the object you put as a parameter is passed, but not the object itself, therefore

val=11;
changeInt(val);

does the exact same thing as

int val=11;
int val2=val
changeInt(val2);

int is a primitive, primitives don't "wrap" a value, you could try to use an Integer class, or make your own class that stores an integer, and then change that classes integer value. Instances of an object are sometimes passed ByReference if setup right. here is an example

MyStringClass.java

public class MyStringClass{

    private String string = null;

    public MyStringClass(String s){
        string = s;
    }
    public String getValue(){
        return string;
    }
    public void setValue(String s){
        string = s;
    }
}

and then the workings

public static void addTo(String s){
    s += " world";
}
public static void addTo(MyStringClass s){
    s.setValue(s.getValue() + " world");
}
public static void main(String[] args){
    String s = "hello";
    MyStringClass s1 = new MyStringClass("hello");
    addTo(s);
    addTo(s1);
    System.out.println(s);//hello
    System.out.println(s1);//hello world
}

I would wonder why you need to change the value instead of just returning it? isn't it easier?

D3_JMultiply
  • 1,022
  • 2
  • 12
  • 22
0

Here is a Example to pass argument:

class Test {
int a,b;

public Test(int j, int k) {
    a=j;
    b=k;
}
void change(Test ko){
    ko.a=ko.b+ko.a;
    ko.a=ko.b-12;


}

}


class sdf {
public static void main(String[] args){
 Test op=new Test(12,32);
 System.out.println(op.a+" "+op.b);

 op.change(op);

 System.out.println(op.a+" "+op.b);


}

}

Take a look at this piece of code::

you can see , in this case the action inside change() have affected the object passed to the method

When an object reference is passed to the method ,the reference itself is passed to the method call-by-value . therefore , the parameter receives a copy of the reference used in this argument .As a result A change to the parameter (such as making it refers to the different object ) will not affect the reference used as the argument . however , since the parameter and the argument both refer to the same object , a change through the parameter will affect the object reffered by the argument.