【JVM001】宋红康JVM字节码举例

宋红康JVM字节码举例

1 Integer

package jvmT;
public class IntegerTest {
    public static void main(String[] args) {
        Integer i =5;
        int y =5;
        System.out.println(i==y); //true

        Integer i6 =5;
        Integer y6 =5;
        System.out.println(i6==y6);//true

        Integer i5 =128;
        Integer y5 =128;
        System.out.println(i5==y5);//false
    }
}

说明:

1 String

package jvmT;

public class StringTest {
    public static void main(String[] args) {
        String a = new String("hello") + new String("world");
        String b = "helloworld";
        System.out.println(a == b); //false
    }
}

说明:

1 Object

class Father{
    int x = 10 ;

    public Father(){
        this.print();
        x =20;
    }
    public void print(){
        System.out.println("father.x = " + x );
    }
}

class Son extends Father{
    int x = 30 ;

    public Son(){
        this.print();
        x =40;
    }

    public void print(){
        System.out.println("son.x = " + x );
    }
}

public class ObjectTest {

    public static void main(String[] args) {
        Father f =
        System.out.println(f.x);

    }
}

执行结果:
在这里插入图片描述

说明:
(1)成员变量(非静态)赋值过程:①默认初始化—>②显示初始化/代码块中初始化—>③构造器中初始化—>④有了对象之后可以f.x或者f.method()设置属性值
(2)方法有多态,属性没有多态

main方法的字节码文件
在这里插入图片描述
下面是Father类构造器字节码文件
在这里插入图片描述

下面是Son类构造器字节码文件
--->
字节码文件说明:

1、main的字节码文件第3行会执行Son的构造器方法,我们看Son的构造器方法的字节码文件第二行会调用Father的构造方法,而在Father的构造方法中,会通过this调用print方法,因为子类Son重写print方法,因此此时this调用的print的方法是Son中的print方法,因此会打印son.x =0,为什么是0,是因为子类的中x此时还未显示赋值,即还未执行说明中的第②步。
2、等执行完Father构造器中的print方法,接下来会执行构造器中x=20,即把父类中的属性x赋值20。
3、执行完Father构造器,接下来继续执行第4,5行,会将30显示赋值给Son.x, 然后继续执行第7行this.print方法,此时会打印son.x=30
4、执行完第7行之后,继续执行9,10行,将40赋值给Son.x。
5、执行完Son的构造器方法,我们回到main的字节码文件,看第7,8行,会获取Father.x值,从上面的第二步看出Father.x的值是20,因此最终会打印20.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值