本文共 990 字,大约阅读时间需要 3 分钟。
一:基本类型比较:
1、情形一:
public static void compare(Integer i , Integer x){ System.out.println(i == x); System.out.println(i > x); System.out.println(i < x); } public static void main(String[] args){ Integer j = new Integer(1); Integer k = new Integer(1); compare(j,k); }结果:
falsefalsefalse2、情形二:
public static void compare(Integer i , Integer x){ System.out.println(i == x); System.out.println(i > x); System.out.println(i < x); } public static void main(String[] args){ Integer j = 1; Integer k = 1; compare(j,k); }结果:
truefalsefalse
二:传递包装类
public static void chenge( Integer x, Integer y){ x++; y++; } public static void main(String[] args){ Integer j = 1; Integer k = 1; chenge(j,k); System.out.println(j + " " + k); }结果:
1 1
结论:
java传递的都是值,那么x和y也都是值传递,也就是说传递的是指针。但是为什么x和y指向的地址不会改变呢?这就涉及到创建对象的问题,当执行“x++”的时候,进行拆箱,获得x的值然后再创建新的对象,并赋值为x++;
转载地址:http://jmryb.baihongyu.com/