其他分享
首页 > 其他分享> > String常量池-练习1

String常量池-练习1

作者:互联网

public class Javatest70 {
    /**
     * String常量池
     * 代码输出结果
     * =========使用双引号直接创建字符串=======
     * a5e75
     * 9e0ad
     * 31a7df
     * 31a7df
     * equals:true
     * name==name2: true
     *
     * =========使用构造方法创建字符串=======
     * 31a7df
     * 31a7df
     * equals:true
     * name3==name4: false
     *
     * =========两种方式对象比较=======
     * name==name3: false
     * equals:true
     */
    public static void main(String[] args) {
        System.out.println("=========使用双引号直接创建字符串=======");
        String addr = "北京";
        System.out.println(Integer.toHexString(addr.hashCode()));
        addr = "上海";
        System.out.println(Integer.toHexString(addr.hashCode()));
        String name = "jack";
        System.out.println(Integer.toHexString(name.hashCode()));
        String name2 = "jack";
        System.out.println(Integer.toHexString(name2.hashCode()));
        System.out.println("equals:" + name.equals(name2));
        System.out.println("name==name2: "+(name==name2));
        System.out.println();

        System.out.println("=========使用构造方法创建字符串=======");
        String name3 = new String("jack");
        System.out.println(Integer.toHexString(name3.hashCode()));
        String name4 = new String("jack");
        System.out.println(Integer.toHexString(name4.hashCode()));
        System.out.println("equals:" + name3.equals(name4));
        System.out.println("name3==name4: "+(name3==name4));
        System.out.println();

        System.out.println("=========两种方式对象比较=======");
        System.out.println("name==name3: "+(name==name3));
        System.out.println("equals:" + name.equals(name3));
    }
}

 

标签:String,常量,练习,System,name,name3,println,out
来源: https://www.cnblogs.com/yixiaobaitest/p/16030380.html