我们知道得到String对象有两种办法:
String str1="hello";
String str2=new String("hello");
这两种创建String对象的方法有什么差异吗?当然有差异,差异就在于第一种方法在对象池中拿对象,第二种方法直接生成新的对象。在JDK5.0里面,Java虚拟机在启动的时候会实例化9个对象池,这9个对象池分别用来存储8种基本类型的包装类对象和String对象。当我们在程序中直接用双引号括起来一个字符串时,JVM就到String的对象池里面去找看是否有一个值相同的对象,如果有,就拿现成的对象,如果没有就在对象池里面创建一个对象,并返回。所以我们发现下面的代码输出true:
String str1="hello";
String str2="hello";
System.out.println(str1==str2);
这说明str1和str2指向同一个对象,因为它们都是在对象池中拿到的,而下面的代码输出为false:
String str3="hello"
String str4=new String("hello");
System.out.println(str3==str4);
因为在任何情况下,只要你去new一个String对象那都是在堆里创建了一个新的对象。
与此类似的,在JDK5.0里面8种基本类型的包装类也有这样的差异:
Integer i1=5;//在对象池中拿
Integer i2 =5;//所以i1==i2
Integer i3=new Integer(5);//重新创建新对象,所以i2!=i3
对象池的存在是为了避免频繁的创建和销毁对象而影响系统性能,那我们自己写的类是否也可以使用对象池呢?当然可以,请看以下代码:
import java.util.HashSet;
class Student {
private String name;
private int age;
private static HashSet<Student> pool = new HashSet<Student>();// 对象池
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// 使用对象池来得到对象的方法
public static Student newInstance(String name, int age) {
// 循环遍历对象池
for (Student stu : pool) {
if (stu.name.equals(name) && stu.age == age) {
return stu;
}
}
// 如果找不到值相同的Student对象,则创建一个Student对象
// 并把它加到对象池中然后返回该对象。
Student stu = new Student(name, age);
pool.add(stu);
return stu;
}
}
public class MyPool {
public static void main(String[] args) {
Student stu1 = Student.newInstance("zhangsan", 30);// 对象池中拿
Student stu2 = Student.newInstance("zhangsan", 30);// 所以stu1==stu2
Student stu3 = new Student("zhangsan", 30);// 重新创建,所以stu1!=stu3
System.out.println(stu1 == stu2);
System.out.println(stu1 == stu3);
}
}
在这顺便说一句:不要滥用哈希表,有一定开发经验的开发人员经常会使用hash表(hash表在JDK中的一个实现就是HashMap)来缓存一些数据,从而提高系统的运行速度。比如使用HashMap缓存一些物料信息、人员信息等基础资料,这在提高系统速度的同时也加大了系统的内存占用,特别是当缓存的资料比较多的时候。其实我们可以使用操作系统中的缓存的概念来解决这个问题,也就是给被缓存的分配一个一定大小的缓存容器,按照一定的算法淘汰不需要继续缓存的对象,这样一方面会因为进行了对象缓存而提高了系统的运行效率,同时由于缓存容器不是无限制扩大,从而也减少了系统的内存占用。现在有很多开源的缓存实现项目,比如ehcache、oscache等,这些项目都实现了FIFO、MRU等常见的缓存算法。