zoukankan      html  css  js  c++  java
  • JVM学习分享-思考题

    package zero.desk.stringconstantpool;

    import org.junit.Test;

    /**
    * @author Zero
    * @since 2019-09-17.
    * Description:
    * 当调用intern方法时,
    * 如果池已经包含此字符串(equals确定),则返回池中字符串。
    * 否则,将此String对象添加到池中并且返回该String对象的引用。
    */
    public class StringConstantPool_Intern {

    @Test
    public void test1() {
    String a1 = "AA";//当调用intern方法时,如果池已经包含此字符串(equals确定),则返回池中字符串。否则,将此String对象添加到池中并且返回该String对象的引用。
    a1.intern();
    }
    }

    package zero.desk.stringconstantpool;

    import org.junit.Test;

    /**
    * @author Zero
    * @since 2019-09-17.
    * Description:
    * /Users/zero/Code/Myself/Project/zero-Project/jvm/target/classes/zero/desk/stringconstantpool
    * javap -verbose StringConstantPoolAA
    * cd ../stringconstantpool
    */
    public class StringConstantPoolAA {

    @Test
    public void test1() {
    String a1 = "AA";//在常量池上创建常量AA
    String a2 = "AA";//直接返回已经存在的常量AA
    System.out.println(a1 == a2); //true
    }

    }

    package zero.desk.stringconstantpool;

    import org.junit.Test;

    /**
    * @author Zero
    * @since 2019-09-17.
    * Description:
    * 思考题
    */
    public class StringConstantPoolAAAA {

    @Test
    public void test1() {
    String a1 = new String("AA");//在堆上创建常量AA
    String a11 = a1.intern();
    String a2 = "AA";//在常量池上创建常量AA
    System.out.println(a1 == a2); //false
    System.out.println(a1 == a11); //false
    System.out.println(a11 == a2); //true
    }

    }

    package zero.desk.stringconstantpool;

    import org.junit.Test;

    /**
    * @author Zero
    * @since 2019-09-17.
    * Description:
    * 思考题
    */
    public class StringConstantPoolAABB {

    @Test
    public void test1() {
    String a1 = "AA"+"BB";//在常量池上创建常量AABB
    String a2 = "AABB";//在常量池上创建常量AABB
    System.out.println(a1 == a2); //true
    }

    }

    package zero.desk.stringconstantpool;

    import org.junit.Test;

    /**
    * @author Zero
    * @since 2019-09-17.
    * Description:
    * 思考题
    */
    public class StringConstantPoolNAA {

    @Test
    public void test1() {
    String a1 = "AA";//在常量池上创建常量AA
    String a2 = new String("AA");//在堆上创建对象AA
    String a3 = a2.intern();//常量池上存在常量AA,直接返回常量池上常量AA,即a1
    System.out.println(a1 == a2); //false
    System.out.println(a1 == a3); //true
    System.out.println(a2 == a3); //false
    System.out.println(System.identityHashCode(a1));
    System.out.println(System.identityHashCode(a2));
    }

    }

    package zero.desk.stringconstantpool;

    import org.junit.Test;

    /**
    * @author Zero
    * @since 2019-09-17.
    * Description:
    * 思考题
    */
    public class StringConstantPoolNAABB {

    @Test
    public void test1() {
    String a3 = "AA"+new String("BB"); //在堆上创建对象AABB
    // String a33 = a3.intern();
    String a333 = "AABB";//在常量池上创建常量AABB
    System.out.println(a333 == a3); //false
    //
    System.out.println(System.identityHashCode(a3));
    System.out.println(System.identityHashCode(a333));
    }
    }

    package zero.desk.stringconstantpool;

    import org.junit.Test;

    /**
    * @author Zero
    * @since 2019-09-17.
    * Description:
    * 思考题
    */
    public class StringConstantPoolNAABBi {

    @Test
    public void test1() {
    String a3 = "AA"+new String("BB"); //在常量池上创建常量AABB
    String a33 = a3.intern();
    String a333 = "AABB";//在常量池上创建常量AABB
    System.out.println(a333 == a3); //true
    //
    System.out.println(System.identityHashCode(a3));
    System.out.println(System.identityHashCode(a33));
    System.out.println(System.identityHashCode(a333));
    }
    }

    package zero.desk.stringconstantpool;

    import org.junit.Test;

    /**
    * @author Zero
    * @since 2019-09-17.
    * Description:
    * 思考题
    */
    public class StringConstantPoolNAANBB {

    @Test
    public void test1() {
    String a3 = new String("AA")+new String("BB"); //在堆上创建对象AABB
    // String a33 = a3.intern();
    String a333 = "AABB";//在常量池上创建常量AABB
    System.out.println(a333 == a3); //false
    //
    System.out.println(System.identityHashCode(a3));
    System.out.println(System.identityHashCode(a333));
    }
    }

    package zero.desk.stringconstantpool;

    import org.junit.Test;

    /**
    * @author Zero
    * @since 2019-09-17.
    * Description:
    * 思考题
    */
    public class StringConstantPoolNAANBBi {

    @Test
    public void test1() {
    String a3 = new String("AA")+new String("BB"); //在常量池上创建常量AABB
    String a33 = a3.intern();
    String a333 = "AABB";//在常量池上创建常量AABB
    System.out.println(a333 == a3); //true
    //
    System.out.println(System.identityHashCode(a3));
    System.out.println(System.identityHashCode(a33));
    System.out.println(System.identityHashCode(a333));
    }
    }
  • 相关阅读:
    Sql Serer 常用函数
    分享5个viewport相关的jQuery插件 java程序员
    struts2.xml中使用chain和redirectAction这两个类型结果(typeresult)时,报检查错误(validation) java程序员
    Android开发之Intent跳转到系统应用中的拨号界面、联系人界面、短信界面 java程序员
    详解struts2中struts.properties java程序员
    Struts2输入校验总结 java程序员
    struts.xml配置文件中result的语法:<result name="" type="">xxxxx</result> java程序员
    挺立在孤独,失败与屈辱的废墟上(俞敏洪) 读书心得 java程序员
    超棒的响应式jQuery网格布局插件 grida licious java程序员
    struts2国际化 java程序员
  • 原文地址:https://www.cnblogs.com/DeskZero/p/11538147.html
Copyright © 2011-2022 走看看