zoukankan      html  css  js  c++  java
  • Java中的对象池技术

    java中的对象池技术,是为了方便快捷地创建某些对象而出现的,当需要一个对象时,就可以从池中取一个出来(如果池中没有则创建一个),则在需要重复重复创建相等变量时节省了很多时间。对象池其实也就是一个内存空间,不同于使用new关键字创建的对象所在的堆空间。本文只从java使用者的角度来探讨java对象池技术,并不涉及对象池的原理及实现方法。个人认为,如果是真的专注java,就必须对这些细节方面有一定的了解。但知道它的原理和具体的实现方法则不是必须的。

     
    1,对象池中对象和堆中的对象
    public class Test{
      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,另外两种浮点数类型的包装类则没有实现。另外Byte,Short,Integer,Long,Character这5种整型的包装类也只是在对应值小于等于127时才可使用对象池,也即对象不负责创建和管理大于127的这些类的对象。以下是一些对应的测试代码:
    public class Test{
    public static void main(String[] args){
      //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
      
    }
    }
     
    3,String也实现了对象池技术
    String类也是java中用得多的类,同样为了创建String对象的方便,也实现了对象池的技术,测试代码如下:
    public class Test{
    public static void main(String[] args){
     //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
    }
    }
    最后:
    细节决定成败,写代码更是如此。
  • 相关阅读:
    Flask第二篇——服务器相关
    Flask第一篇——URL详解
    Appium 定位方法例子(4)
    selenium 上传文件方法补充——SendKeys、win32gui
    Appium+python (3) 异常处理
    Appium+python (3) 元素定位(1)
    "http://127.0.0.1:4723/wd/hub"的解释
    Appium + Python App自动化(2)第一个脚本
    Appium+python(1)简单的介绍环境搭建
    用fiddler设置手机代理
  • 原文地址:https://www.cnblogs.com/heartstage/p/3417026.html
Copyright © 2011-2022 走看看