1.String类和对象池
我们知道得到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); //输出结果为true
这说明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 对象池的存在是为了避免频繁的创建和销毁对象而影响系统性能,那我们自己写的类是否也可以使用对象池呢?当然可以,考察以下代码:
class Student{
private String name;
private int age;
private static HashSet pool=new HashSet ();//对象池
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 Test{ public static void main(String[] args){
Student stu1=Student.newInstance("tangliang",30);//对象池中拿
Student stu2=Student.newInstance("tangliang",30);//所以stu1==stu2
Student stu3=new Student("tangliang",30);//重新创建,所以stu1!=stu3
System.out.println(stu1==stu2);
System.out.println(stu1==stu3); } }