Java通过方法更改变量的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16503939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java changing value of a variable through a method?
提问by Tommy
Hello I was wondering how I would be able to send a variable as a parameter to a method and have it be changed by the method. For example
您好,我想知道如何将变量作为参数发送给方法并使其由方法更改。例如
public class Main {
public static void main(String[] args) {
int i = 2;
doThis(i);
System.out.println(i);
}
public static void doThis(int i) {
i = 3;
}
}
I would like it to print out 3 instead of 2. Thanks.
我希望它打印出 3 而不是 2。谢谢。
回答by Jigar Joshi
I would like it to print out 3 instead of 2
我希望它打印出 3 而不是 2
Change method to return value
更改方法以返回值
int i = 2;
i = doThis(i);
public static int doThis(int i) {
i = 3;
return i;
}
it copies the value of primitive from caller to argument
它将原始值从调用者复制到参数
回答by MadProgrammer
Java can't do this. However you can return the value from the method...
Java无法做到这一点。但是,您可以从方法返回值...
public static int doThis(int i) {
return 3;
}
And reassign it...
并重新分配它...
int i = 2;
i = doThis(i);
回答by Peeyush
java is all about pass by values. so it wont change the way you wanted. Read here for more
java是关于传递值的。所以它不会改变你想要的方式。 在这里阅读更多
however you do following to solve this problem
但是你做以下来解决这个问题
i = doThis(i);
public static int doThis(int i) {
i = 3;
return i;
}
回答by gifpif
Java is pass by value for primitive data type or objects.
Reference data type parameters, such as objects, are also passed into methods by value.
Java 是按值传递原始数据类型或对象。
引用数据类型参数(例如对象)也按值传递给方法。
回答by Barney Govan
Java passes everything by value, so if you use the assignment operator in a class method you're not going to be changing the original object.
Java 按值传递所有内容,因此如果您在类方法中使用赋值运算符,则不会更改原始对象。
For example:
例如:
public class Main {
public static void main(String[] args) {
Integer i = new Integer(2);
setToThree(i);
System.out.println(i);
}
public static void setToThree(Integer i) {
i = new Integer(3);
}
}
is going to print 2.
将打印 2。
Having said that, if the object you're passing in a reference to is mutable you can make changes to it in they way you're thinking of.
话虽如此,如果您传入引用的对象是可变的,您可以按照您的想法对其进行更改。
For example:
例如:
public class Main {
public static void main(String[] args) {
MyMutableInt i = new MyMutableInt(2);
setToThree(i);
System.out.println(i);
}
public static void setToThree(MyMutableInt i) {
i.set(3);
}
}
This will print 3 (assuming MyMutableInt has a correct toString() method).
这将打印 3(假设 MyMutableInt 有一个正确的 toString() 方法)。
Of course, Java Integers are immutable, and so don't have the ability to be changed like that. So you have 2 choices here:
当然,Java Integers 是不可变的,所以不能像那样改变。所以你在这里有 2 个选择:
- Return the value
- Use something like MutableInt from commons-lang (http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/mutable/MutableInt.html) (or create your own mutable object).
- 返回值
- 使用来自 commons-lang ( http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/mutable/MutableInt.html) 的MutableInt 之类的东西(或创建自己的可变对象)。
Note: this doesn't work with primitives of any kind. For that you're going to have to pass back by return value. If you have multiple values to mutate, you'll have to wrap them in an object to return them, so you may also use this method.
注意:这不适用于任何类型的原语。为此,您将不得不通过返回值传回。如果您有多个值要变异,则必须将它们包装在一个对象中以返回它们,因此您也可以使用此方法。