java 参数传递

来源:互联网 发布:编程原本pdf 编辑:程序博客网 时间:2024/06/01 08:15

自己写的一个例子:

public class TestString {public static void main(String[] args) {// TODO Auto-generated method stubString s = new String("hello");String s1 = "hello";String s2 = "hello";if(s == s1) {System.out.println(true);} else {System.out.println(false);}if(s2 == s1) {System.out.println(true);} else {System.out.println(false);}//等价于s2 = new String(new StringBuffer(s2).append("world"));s2 = s2 + "world";String s3 = "helloworld";if(s2 == s3) {System.out.println(true);} else {System.out.println(false);}Integer a = new Integer(10);Integer b = new Integer(20);System.out.println(a);System.out.println(b);swap(a, b);System.out.println(a);System.out.println(b);}public static void swap(Integer a, Integer b) {Integer c =  a;a = b;b = c;System.out.println(a);System.out.println(b);}}

输出结果:

falsetruefalse102020101020

结论:

自我感觉java中参数传递都是按照值传递进行的,请大家好好思考思考吧!

String类型有些怪异,因为new 出来的String(放在堆中),跟直接赋值的String(放在data segment),其中的内容所存放的地方不一样,所以引用也就不一样。

0 0