java语言基础(二)——java变量传值

来源:互联网 发布:王旭中医淘宝店铺好吗 编辑:程序博客网 时间:2024/06/10 05:00

一、先介绍一个查看变量内存信息的的方法: int java.lang.System.identityHashCode(Object arg0)

        这个方法是System类中的静态方法,用于根据对象内存地址来计算哈希值。

        identityHashCode是用稳定算法根据对象的物理地址计算得到的,所以任何两个不同的对象identityHashCode值一定不等。任何相同的对象identityHashCode值一定相等。

二、传值问题从本质上说是赋值问题。例如,函数调用时的传值问题,实际上就是实参为形参赋值的问题。函数返回值的传值问题实际上就是return变量为接收函数返回值的变量赋值的问题。

三、变量间传值问题。

1、回顾一下java里的数据类型:

java里面的数据类型分为两种:基本类型(primitive types);引用类型(reference types)

java语言提供了八种基本类型。其中,六种属于数字类型(四个整数型,两个浮点型),一种字符类型,一种布尔类型。

除了上述八种基本类型以外的所有数据类型均属于引用类型。

归纳见下表:


1、基本类型

java语言提供了八种基本类型。其中,六种属于数字类型(四个整数型,两个浮点型),一种字符类型,一种布尔类型。详见下表:

基本类型是构成引用类型的基础。基本类型没有父类,不是继承自Object类。基本类型的值一般称之为简单对象。简单对象的创建无需借助于new关键字。简单对象存放于栈区,具有更高的存取效率。

引用数据类型均是Object类的子类。除了上述八种基本类型外,其他java数据类型均为引用类型。引用类型的对象(实例)一般称之为复杂对象。复杂对象的实例化必须借助于new关键字。复杂对象均存放于堆区。

* 注意:有些复杂对象,如基本类型对应的包装类的对象,其创建虽然也可以不借助new关键字,但是这只是表象。从本质上讲这些对象是先由new关键字创建,然后再为复杂对象的属性赋值。

4、基本类型变量间传值的问题:

public class PrimitiveTypesTest
{
    public static void main(String[] args)
    {
        int a = 0;
        int b = a;
        System.out.println("a=" + a + " --> " + System.identityHashCode(a));
        System.out.println("b=" + b + " --> " + System.identityHashCode(b));
        b++;
        System.out.println("a=" + a + " --> " + System.identityHashCode(a));
        System.out.println("b=" + b + " --> " + System.identityHashCode(b));
        a++;
        System.out.println("a=" + a + " --> " + System.identityHashCode(a));
        System.out.println("b=" + b + " --> " + System.identityHashCode(b));
        System.out
                .println("-------------------------------------------------------");
        char cha_a = 'A';
        char cha_b = cha_a;
        System.out.println("cha_a=" + cha_a + " --> "
                + System.identityHashCode(cha_a));
        System.out.println("cha_b=" + cha_b + " --> "
                + System.identityHashCode(cha_b));
        cha_b++;
        System.out.println("cha_a=" + cha_a + " --> "
                + System.identityHashCode(cha_a));
        System.out.println("cha_b=" + cha_b + " --> "
                + System.identityHashCode(cha_b));
        cha_a++;
        System.out.println("cha_a=" + cha_a + " --> "
                + System.identityHashCode(cha_a));
        System.out.println("cha_b=" + cha_b + " --> "
                + System.identityHashCode(cha_b));
        System.out
                .println("-------------------------------------------------------");
        double c = 0;
        double d = c;
        System.out.println("c=" + c + " --> " + System.identityHashCode(c));
        System.out.println("d=" + d + " --> " + System.identityHashCode(d));
        d++;
        System.out.println("c=" + c + " --> " + System.identityHashCode(c));
        System.out.println("d=" + d + " --> " + System.identityHashCode(d));
        c++;
        System.out.println("c=" + c + " --> " + System.identityHashCode(c));
        System.out.println("d=" + d + " --> " + System.identityHashCode(d));
    }
}

运行结果:

a=0 --> 366712642
b=0 --> 366712642
a=0 --> 366712642
b=1 --> 1829164700
a=1 --> 1829164700
b=1 --> 1829164700
-------------------------------------------------------
cha_a=A --> 2018699554
cha_b=A --> 2018699554
cha_a=A --> 2018699554
cha_b=B --> 1311053135
cha_a=B --> 1311053135
cha_b=B --> 1311053135
-------------------------------------------------------
c=0.0 --> 118352462
d=0.0 --> 1550089733
c=0.0 --> 865113938
d=1.0 --> 1442407170
c=1.0 --> 1028566121
d=1.0 --> 1118140819

我们可以得出结论:两个基本类型变量传值发生之后,对其中任何一个变量的修改不会影响另外一个变量。

5、引用类型变量间传值的问题

public class ReferenceTypesTest
{
    public static void main(String[] args)
    {
        TestObj a = new TestObj();
        a.name = "aaa";
        TestObj b = a;
        System.out.println("a.name=" + a.name + " --> "
                + System.identityHashCode(a));
        System.out.println("b.name=" + b.name + " --> "
                + System.identityHashCode(b));
        b.name = "bbb";
        System.out.println("a.name=" + a.name + " --> "
                + System.identityHashCode(a));
        System.out.println("b.name=" + b.name + " --> "
                + System.identityHashCode(b));
        a.name = "aaaaaa";
        System.out.println("a.name=" + a.name + " --> "
                + System.identityHashCode(a));
        System.out.println("b.name=" + b.name + " --> "
                + System.identityHashCode(b));
        System.out
                .println("-------------------------------------------------------");
        int[] array_a = new int[] { 0 };
        int[] array_b = array_a;
        System.out.println("array_a=" + array_a[0] + " --> "
                + System.identityHashCode(array_a));
        System.out.println("array_b=" + array_b[0] + " --> "
                + System.identityHashCode(array_b));
        array_b[0]++;
        System.out.println("array_a=" + array_a[0] + " --> "
                + System.identityHashCode(array_a));
        System.out.println("array_b=" + array_b[0] + " --> "
                + System.identityHashCode(array_b));
        array_a[0]++;
        System.out.println("array_a=" + array_a[0] + " --> "
                + System.identityHashCode(array_a));
        System.out.println("array_b=" + array_b[0] + " --> "
                + System.identityHashCode(array_b));
    }
}
class TestObj
{
    public String name;
}

运行结果:

a.name=aaa --> 366712642
b.name=aaa --> 366712642
a.name=bbb --> 366712642
b.name=bbb --> 366712642
a.name=aaaaaa --> 366712642
b.name=aaaaaa --> 366712642
-------------------------------------------------------
array_a=0 --> 1829164700
array_b=0 --> 1829164700
array_a=1 --> 1829164700
array_b=1 --> 1829164700
array_a=2 --> 1829164700
array_b=2 --> 1829164700

我们可以得出结论:两个引用类型变量传值发生之后,对其中任何一个变量的修改等价于对另一个变量的修改。

 6、特殊的引用类型——包装类(Wrapper Class)

public class WrapperClassTest
{
    public static void main(String[] args)
    {
        Integer a = 0;
        Integer b = a;
        System.out.println("a=" + a + " --> " + System.identityHashCode(a));
        System.out.println("b=" + b + " --> " + System.identityHashCode(b));
        b++;
        System.out.println("a=" + a + " --> " + System.identityHashCode(a));
        System.out.println("b=" + b + " --> " + System.identityHashCode(b));
        a++;
        System.out.println("a=" + a + " --> " + System.identityHashCode(a));
        System.out.println("b=" + b + " --> " + System.identityHashCode(b));
        System.out
                .println("-------------------------------------------------------");
        Character cha_a = 'A';
        Character cha_b = cha_a;
        System.out.println("cha_a=" + cha_a + " --> "
                + System.identityHashCode(cha_a));
        System.out.println("cha_b=" + cha_b + " --> "
                + System.identityHashCode(cha_b));
        cha_b++;
        System.out.println("cha_a=" + cha_a + " --> "
                + System.identityHashCode(cha_a));
        System.out.println("cha_b=" + cha_b + " --> "
                + System.identityHashCode(cha_b));
        cha_a++;
        System.out.println("cha_a=" + cha_a + " --> "
                + System.identityHashCode(cha_a));
        System.out.println("cha_b=" + cha_b + " --> "
                + System.identityHashCode(cha_b));
        System.out
                .println("-------------------------------------------------------");
        Double c = 0.0;
        Double d = c;
        System.out.println("c=" + c + " --> " + System.identityHashCode(c));
        System.out.println("d=" + d + " --> " + System.identityHashCode(d));
        d++;
        System.out.println("c=" + c + " --> " + System.identityHashCode(c));
        System.out.println("d=" + d + " --> " + System.identityHashCode(d));
        c++;
        System.out.println("c=" + c + " --> " + System.identityHashCode(c));
        System.out.println("d=" + d + " --> " + System.identityHashCode(d));
    }
}

运行结果:

a=0 --> 366712642
b=0 --> 366712642
a=0 --> 366712642
b=1 --> 1829164700
a=1 --> 1829164700
b=1 --> 1829164700
-------------------------------------------------------
cha_a=A --> 2018699554
cha_b=A --> 2018699554
cha_a=A --> 2018699554
cha_b=B --> 1311053135
cha_a=B --> 1311053135
cha_b=B --> 1311053135
-------------------------------------------------------
c=0.0 --> 118352462
d=0.0 --> 118352462
c=0.0 --> 118352462
d=1.0 --> 1550089733
c=1.0 --> 865113938
d=1.0 --> 1550089733

这说明:包装类变量间传值的特性与基本数据类型类似

7、由此我们总结可以得出如下结论:

两个基本类型变量传值发生之后,对其中任何一个变量的修改不会影响另外一个变量。

两个引用类型变量传值发生之后,对其中任何一个变量的修改等价于对另一个变量的修改。

其中,包装类量间传值例外,它遵守基本类型变量间传值的规则。






0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 眼睛让电焊晃了怎么办 眼被电焊打了怎么办 眼镜弹簧腿坏了怎么办 眼镜框铰链坏了怎么办 金属眼镜框歪了怎么办 眼镜框螺丝断了怎么办 眼镜被压变形了怎么办 金属眼镜腿断了怎么办 眼镜弹簧腿断了怎么办 眼镜腿螺丝太紧怎么办 眼镜金属柄断了怎么办 金属眼镜腿折了怎么办 眼镜腿中间断了怎么办 塑料眼镜腿断了怎么办 眼镜上的螺丝拧不紧怎么办 眼镜的把坏了怎么办 把眼镜坐坏了怎么办 梦见眼镜腿掉了怎么办 眼镜的腿掉了怎么办 眼镜腿的螺丝掉了怎么办 爱大爱眼镜掉腿了怎么办 合金眼镜腿断了怎么办 手关节复位h疼痛怎么办 我叫mt红色卡牌怎么办 星盟冲突qq登录怎么办 汽车雷达下雨一直响怎么办 火山小视频封火力怎么办 电脑被当成矿机怎么办 哥华有线机顶盒反应慢怎么办 电脑绣花机编码器坏了怎么办? 伺服电机开起没有力怎么办 西门子冰箱排水孔堵塞怎么办 数控车床西门子系统卡顿怎么办 手机系统不支持多屏互动怎么办 伺服驱动器系统错误报警怎么办 防雷接地电阻不符合规范怎么办 微信支付风控了怎么办 伺服电机没有配原点开关怎么办 电脑自带游戏打不开怎么办 战地1更新很慢怎么办 客户端提示初始化控件失败怎么办