我几天前开始学习Java。我对C++有一些经验,这就是为什么我习惯于指针,这将使这更容易。
所以现在我的问题。
假设我有以下代码:
public class Main {
public static void main(String[] args) {
int variable1=2, variable2=2;
input(variable1, variable2);
System.out.println(variable1 + " " + variable2);
// this should output "1 3" on the console
}
private static void input(int variable1, int variable2) {
System.out.println(variable1 + " " + variable2);
// this will output "2 2" on the console
variable1 = 1;
variable2 = 3;
}
}
因此,函数input()
从 main 获取变量并(正确)输出它们。但是如何将更改的变量传输回主函数呢?
正如其他人所说,基元是按值传递的。 不能传递对它们的引用。 但是,您可以传递任何Object
的实例并(假设它是可变的)修改它,并且更改将可用于引用该实例的任何范围。
对于您的示例,您可以做两件事 - 使用数组...
public class Main{
public static void main(String[] args){
int[] values = {2, 2};
input(values);
System.out.println(values[0] + ", " + values[1]); // prints 1, 3
}
private static void input(int[] values){
values[0] = 1;
values[1] = 3;
}
}
或带有对象
public class ValueHolder{
private int val1;
private int val2;
public void setValue1(int i){ val1 = i; }
public void setValue2(int i){ val2 = i; }
public int getValue1(){return val1;}
public int getValue2(){return val2;}
public String toString(){
return String.valueOf(val1) + ", " + String.valueOf(val2);
}
}
public class Main{
public static void main(String[] args){
ValueHolder vh = new ValueHolder();
vh.setValue1(2);
vh.setValue2(2);
System.out.println(vh); // prints 2, 2
input(vh);
System.out.println(vh); // prints 1, 3
}
private static void input(ValueHolder vh){
vh.setValue1(1);
vh.setValue2(3);
}
}
在 Java 中,所有参数都是按值传递的,而不是按引用传递的。因此,上面的代码不起作用。如果你想把修改传回去,你必须传递一个对象引用(它是按值传递的,但现在你传递了对象)。当然,您必须使用可变对象,例如集合。在您的情况下,您可能还会传递整数数组。
当然:所有这些都是糟糕的编程风格。应通过返回值传递结果,并在可能的情况下使用功能更强大的编程。
input(Variable1, Variable2);
调用此方法时,会将两个变量的值复制到新定义的两个变量int Variable1, int Variable2
。你可以这样想:
private static void input(int Variable1, int Variable2) {
Variable1 = 2;
Variable2 = 2;
System.out.println(Variable1 + " " + Variable2); // this will output "2 2" on the console
Variable1 = 1; // this is different from the one that is inside main()
Variable2 = 3; // this is different from the one that is inside main()
}
以下是我对另一个问题的回答,它可能有助于您理解按值传递的概念:https://stackoverflow.com/a/9404727/597657
在 Java 中传递原语是通过值完成的。
可以删除静态方法,并将基元用作 Main 类的实例属性,如下所示:
public class Main {
int firstVariable = 1, secondVariable = 2;
// instance block of Main, prints the fields
{
System.out.println("First variable: " + firstVariable);
System.out.println("Second variable: " + secondVariable);
}
// main (static) method: initializes an instance of Main class and calls
// (now) instance method "changeInstanceFields", then prints the change
public static void main(String[] args) {
Main main = new Main();
main.changeInstanceFields();
System.out.println("First variable: " + main.firstVariable);
System.out.println("Second variable: " + main.secondVariable);
}
// instance method: accesses Main class' instance fields
public void changeInstanceFields() {
firstVariable = 1;
secondVariable = 3;
}
}
输出:
First variable: 1
Second variable: 2
First variable: 1
Second variable: 3
设置为类变量
public class Main {
public int variable1, variable2;
public static void main(String[] args) {
Variable1=2;
Variable2=2;
input(Variable1, Variable2);
System.out.println(Variable1 + " " + Variable2);
// this should output "1 3" on the console
}
private static void input(int Variable1, int Variable2) {
System.out.println(Variable1 + " " + Variable2);
// this will output "2 2" on the console
Variable1 = 1;
Variable2 = 3;
}
}
是基元类型 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html。整数是装箱整数。它们都不会改变它们的值。而是创建新对象。但是,您可以使用类包装 int 值。请参阅示例:
public class Main {
int v;
@Override
public String toString() {
return "Main [v=" + v + "]";
}
public static void main(String... strings) {
int v1 = 2;
int v2 = 3;
input(v1, v2);
System.out.println(v1 + " " + v2);
Integer i1 = 2;
Integer i2 = 3;
inputI(i1, i2);
System.out.println(i1 + " " + i2);
Main m1 = new Main();
m1.v = 2;
Main m2 = new Main();
m2.v = 3;
inputM(m1, m2);
System.out.println(m1 + " " + m2);
}
public static void input(int v1, int v2) {
v1 = 3;
v2 = 4;
}
public static void inputI(Integer v1, Integer v2) {
v1 = 3;
v2 = 4;
}
public static void inputM(Main v1, Main v2) {
v1.v = 3;
v2.v = 4;
}
}
输出:
2 3
2 3
Main [v=3] Main [v=4]
发生这种情况是因为变量的值被复制到方法input()。基元变量始终在方法之间复制。