• JVM常量池


      在jvm规范中,每个类型都有自己的常量池。常量池是某类型所用常量的一个有序集合,包括直接常量(基本类型,String)和对其他类型、字段、方法的符号引用。之所以是符号引用而不是像c语言那样,编译时直接指定其他类型,是因为java是动态绑定的,只有在运行时根据某些规则才能确定具体依赖的类型实例,这正是java实现多态的基础。

    为了对常量池有更具体的认识,下面引用几个例子:

    1,常量池中对象和堆中的对象

            Integer i1 = new Integer(1);
    
            Integer i2 = new Integer(1);
    
            // i1,i2分别位于堆中不同的内存空间
    
            System.out.println(i1 == i2);// 输出false
    
            Integer i3 = 1;
    
            Integer i4 = 1;
    
            // i3,i4指向常量池中同一个内存空间
    
            System.out.println(i3 == i4);// 输出true
    
            // 很显然,i1,i3位于不同的内存空间
    
            System.out.println(i1 == i3);// 输出false

    2,8种基本类型的包装类和对象池

      java中基本类型的包装类的大部分都实现了常量池技术,这些类是Byte, Short, Integer, Long, Character, Boolean, 另外两种浮点数类型(Float、Double)的包装类则没有实现。另外Byte, Short, Integer, Long, Character这5种整型的包装类也只在大于等于-128并且小于等于127时才使用常量池,也即对象不负责创建和管理大于127的这些类的对象。以下是一些对应的测试代码:

       //5种整形的包装类Byte,Short,Integer,Long,Character的对象,
    
       //在值小于127时可以使用常量池
    
       Integer i1=127;
    
       Integer i2=127;
    
       System.out.println(i1==i2)//输出true
    
       //值大于127时,不会从常量池中取对象
    
       Integer i3=128;
    
       Integer i4=128;
    
       System.out.println(i3==i4)//输出false
    
       //Boolean类也实现了常量池技术
    
       Boolean bool1=true;
    
       Boolean bool2=true;
    
       System.out.println(bool1==bool2);//输出true
    
       //浮点类型的包装类没有实现常量池技术
    
       Double d1=1.0;
    
       Double d2=1.0;
    
       System.out.println(d1==d2)//输出false 

    当我们给Integer赋值时,实际上调用了Integer.valueOf(int)方法,查看源码,其实现如下:

    public static Integer valueOf(int i) {  
        if(i >= -128 && i <= IntegerCache.high)  
            return IntegerCache.cache[i + 128];  
        else  
            return new Integer(i);  
    }  

    而IntegerCache实现如下:

    private static class IntegerCache {  
        static final int high;  
        static final Integer cache[];  
      
        static {  
            final int low = -128;  
      
            // high value may be configured by property  
            int h = 127;  
            if (integerCacheHighPropValue != null) {  
                // Use Long.decode here to avoid invoking methods that  
                // require Integer's autoboxing cache to be initialized  
                int i = Long.decode(integerCacheHighPropValue).intValue();  
                i = Math.max(i, 127);  
                // Maximum array size is Integer.MAX_VALUE  
                h = Math.min(i, Integer.MAX_VALUE - -low);  
            }  
            high = h;  
      
            cache = new Integer[(high - low) + 1];  
            int j = low;  
            for(int k = 0; k < cache.length; k++)  
                cache[k] = new Integer(j++);  
        }  
      
        private IntegerCache() {}  
    }  

    cache数组是静态的。

    3.String也实现了常量池技术

    String类也是java中用得多的类,同样为了创建String对象的方便,也实现了常量池的技术,测试代码如下:

    //s1,s2分别位于堆中不同空间
    
    String s1=new String("hello");
    
    String s2=new String("hello");
    
    System.out.println(s1==s2)//输出false
    
    //s3,s4位于池中同一空间
    
    String s3="hello";
    
    String s4="hello";
    
    System.out.println(s3==s4);//输出true

    4.Integer面试题

    public class IntegerTest {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            Integer i11 = 127; 
            Integer i12 = 127; 
            Integer i13 = 0; 
            Integer i14 = new Integer(127); 
            Integer i15 = new Integer(127); 
            Integer i16 = new Integer(0); 
             
            System.out.println("i11=i12	" + (i11 == i12)); 
            System.out.println("i11=i12+i13	" + (i11 == i12 + i13)); 
            System.out.println("i14=i15	" + (i14 == i15)); 
            System.out.println("i14=i15+i16	" + (i14 == i15 + i16));
        }
    
    }

    输出结果:

    i11=i12  true
    i11=i12+i13  true
    i14=i15  false
    i14=i15+i16  true

    解析:
    Integer已经默认创建了数值【-128-127】的Integer缓存数据,所以使用Integer i11=127时,JVM会直接在该在对象池找到该值的引用,i12也是,所以i11=i12,都是从对象缓存池中取得该对象引用。但是i14和i15每次都会重新Create新的Integer对象,不会被放入到对象池中,所以他们不是同一个引用,输出false

    对于i11==i12+i13、i14==i15+i16结果为True,是因为,Java的数学计算是在内存栈里操作的,Java会对i15、i16进行拆箱操作,其实比较的是基本类型(127=127+0),他们的值相同,因此结果为True

    把127换成128运行结果是:

    i11=i12  false
    i11=i12+i13  true
    i14=i15  false
    i14=i15+i16  true

    因为128不在Integer对象缓冲池中,所以每次都创建新的Integer(128)对象,所以i11也就不等于i12了。

    5.字符串比较更丰富的一个例子

    public class AAA {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            String hello = "Hello", lo = "lo";
            System.out.print((hello == "Hello") + " ");
            System.out.print((Other.hello == hello) + " ");
            System.out.print((other.Other.hello == hello) + " ");
            System.out.print((hello == ("Hel" + "lo")) + " ");
            System.out.print((hello == ("Hel" + lo)) + " ");
            System.out.println(hello == ("Hel" + lo).intern());
        }
    }
    
    class Other {
        static String hello = "Hello";
    }
    package other;
    
    public class Other { 
        static String hello = "Hello";
    }

    输出结果:

    true true true true false true

    输出结果的分别解释如下:

    在同包同类下,引用自同一String对象.

    在同包不同类下,引用自同一String对象.

    在不同包不同类下,依然引用自同一String对象

    在编译成.class时能够识别为同一字符串的,自动优化成常量,所以也引用自同一String对象

    在运行时创建的字符串具有独立的内存地址,所以不引用自同一String对象

    String的intern()方法会查找在常量池中是否存在一份equal相等的字符串,如果有则返回一个引用,没有则添加自己的字符串进进入常量池,

    注意,只是字符串部分,所以这时会存在2份拷贝,常量池的部分被String类私有持有并管理,自己的那份按对象生命周期继续使用.

    原文出处:http://www.cnblogs.com/wenfeng762/archive/2011/08/14/2137820.html

  • 相关阅读:
    探索javascript----事件对象下的各种X和Y
    css2----兼容----ie67的3像素bug
    探索javascript----拖拽
    一、Rabbitmq安装与配置信息
    四、RABBITMQ特点
    一,activemq安装和配置相关信息
    三,activemq持久化消息到mysql数据库中
    三、RABBITMQ的几个基本概念
    二、JMS和AMQP的对比
    hibernate的工作原理
  • 原文地址:https://www.cnblogs.com/SaraMoring/p/5688795.html
Copyright © 2020-2023  润新知