简体   繁体   中英

Passing Argument in Java

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..?

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 .

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.

原始变量按值传递,而不像您建议的那样引用。

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 

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM