Java 中的简单交换 [英] Simple swap in Java
本文介绍了Java 中的简单交换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这些在我看来是一样的,但为什么它们会产生不同的输出?我是 Java 新手,请耐心等待!
These look the same to me but why do they produce different outputs? I'm new to Java so bear with me!
这个交换功能有效
//Swap 1 Output is "4,8"
public class SampleSwap {
public static void main(String[] args)
{
int a=8;
int b=4;
int temp;
temp=a;
a=b;
b=temp;
System.out.println("a:" +a);
System.out.println("b:" +b);
}
}
这个交换功能不起作用
//Swap 2 Output is "8,4"
public class Swap {
public static void main(String[] args) {
int a = 8, b = 4;
swap(a, b);
System.out.print(a + "," + b);
System.out.println();
}
public static void swap(int a, int b) {
int tmp = a;
a = b;
b = tmp;
}
}
推荐答案
那些参数是按值传递的.他们不会改变原件.
Those parameters are passed by value. They don't change the originals.
这篇关于Java 中的简单交换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文