java常量池
2021-01-16 19:14
                         标签:integer   部分   iter   lan   intern方法   static   system   包括   并且    常量池大体可分为两类: 常量池是为了避免频繁的创建和销毁对象而影响系统性能,其实现了对象的共享。 例如字符串常量池,在编译阶段就把所有的字符串文字放到一个常量池中。 两个方法使用常量池: 注:在java 中,直接使用==操作符,比较的是两个字符串的引用地址,并不是比较内容,比较内容为String.equals()。 解析: 新建String对象的时候,如果直接使用字符串字面量赋值,那么字面量会直接放入class文件的常量池中。 编译器的优化,Hotspot中编译时"Hel" + "lo"将直接变成"Hello",s7+s8则不会优化,因为不知道在之前的步骤中s7和s8会不会发生变化 Java对于String的相加是用语法糖,通过StringBuilder实现的,例如s7+s8,先构造一个StringBuilder里面存放"Hel",然后调用append()方法追加"lo",然后将值为"Hello"的StringBuilder转化为String对象 对于所有包含new方式新建对象(包括null)的“+”连接表达式,它所产生的新对象都不会被加入字符串池中。 第一句: 第二句: 自动装箱就是 除了包装类Long,Double,Float 没有实现这个缓存技术,其它的包装类均实现了它。 当我们给Integer对象赋一个int值的时候,会调用Integer类的静态方法 可以发现,如果使用 但是,如果使用 java常量池 标签:integer   部分   iter   lan   intern方法   static   system   包括   并且    原文地址:https://www.cnblogs.com/mytlx/p/12923044.html一、概述
二、常量池的好处
三、字符串常量池
String s1 = "Hello";
String s2 = "Hello";
String s3 = "Hel" + "lo";
String s4 = "Hel" + new String("lo");
String s5 = new String("Hello");
String s6 = s5.intern();
String s7 = "Hel";
String s8 = "lo";
System.out.println(s1 == s2);  // true
// 新建String对象的时候,如果直接使用字符串字面量赋值,那么字面量会直接放入class文件的常量池中。
System.out.println(s1 == s3);  // true
// Hotspot中编译时"Hel" + "lo"将直接变成"Hello"
System.out.println(s1 == s4);  // false
// new String("lo")这部分不是已知字面量,是一个不可预料的部分,编译器不会优化,
// 必须等到运行时才可以确定结果
System.out.println(s7+s8 == s1);  // false
// 相当于new String(s7 + s8),存放在堆中
System.out.println(s4 == s5);  // false
// 都在堆中,指向不同地址
System.out.println(s1 == s6);  // true
// intern方法,首先在常量池中查找是否存在一份字面量相等的字符串,如果有的话就返回该字符串的引用,
// 没有的话就将它加入到字符串常量池中,并返回在常量池中的地址
String s = new String("abc"); // 创建了几个对象
String s = "abc";	// 创建了几个对象
四、包装类的常量池(缓存)
valueOf这个方法,自动拆箱就是intValue方法。valueOf,下面是valueOf的源码@HotSpotIntrinsicCandidate
public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer cache[];
    static {
        // high value may be configured by property
        int h = 127;
        String integerCacheHighPropValue =
            VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        if (integerCacheHighPropValue != null) {
            try {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
            } catch( NumberFormatException nfe) {
                // If the property cannot be parsed into an int, ignore it.
            }
        }
        high = h;
        cache = new Integer[(high - low) + 1];
        int j = low;
        for(int k = 0; k = 127;
    }
    private IntegerCache() {}
}
Integer e = 1 或 Integer e = Integer.valueOf(1),并且整型字面量在-128 ~127之间,那么不会new新的Integer对象,而是直接引用常量池中的Integer对象,可以当做基本类型比较。Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
System.out.println(f1 == f2);	// true
System.out.println(f3 == f4);	// false
Integer e = new Integer(1),无论值是多少,都要作为对象比较。Integer n1 = new Integer(47);
Integer n1 = new Integer(47);
System.out.println(n1 == n2);	// false
System.out.println(n1 != n2);	// true