关于字符串缓冲池的讨论

来源:互联网 发布:淘宝 图片裁剪 不清楚 编辑:程序博客网 时间:2024/05/21 11:17

看到一个关于字符串缓冲池的讨论

(转)
大家先来看看一段奇怪的程序: 
public class TestString { 
    public static void main(String[] args) { 
        String s1 = "Monday"; 
        String s2 = "Monday"; 
    } 

这个程序真是简单啊!可是有什么问题呢? 

1. 来自 String 的忧虑 
上面这段程序中,到底有几个对象呢? 
可能很多人脱口而出:两个,s1 和 s2 
为什么? 
String 是 final 类,它的值不可变。 
看起来似乎很有道理,那么来检测一下吧,稍微改动一下程序 
就可以看到结果了: 

public class TestString { 
    public static void main(String[] args) { 
        String s1 = "Monday"; 
        String s2 = "Monday"; 
        if (s1 == s2) 
            System.out.println("s1 == s2"); 
        else 
            System.out.println("s1 != s2"); 
    } 

呵呵,很多人都会说已经不止两个对象了 
编译并运行程序,输出:s1 == s2 
啊! 
为什么 s1 == s2 ? 
== 分明是在说:s1 与 s2 引用同一个 String 对象 -- "Monday"! 

2. 千变万化的 String 
再稍微改动一下程序,会有更奇怪的发现: 
public class TestString { 
    public static void main(String[] args) { 
        String s1 = "Monday"; 
        String s2 = new String("Monday"); 
        if (s1 == s2) 
            System.out.println("s1 == s2"); 
        else 
            System.out.println("s1 != s2"); 
        if (s1.equals(s2)) 
            System.out.println("s1 equals s2"); 
        else 
            System.out.println("s1 not equals s2"); 
    } 

我们将 s2 用 new 操作符创建 
程序输出: 
s1 != s2 
s1 equals s2 
嗯,很明显嘛 
s1 s2分别引用了两个"Monday"String对象 
可是为什么两段程序不一样呢? 

3. 在 String 的游泳池中游泳 
哈哈,翻了翻书终于找到了答案: 
原来,程序在运行的时候会创建一个字符串缓冲池 
当使用 s2 = "Monday" 这样的表达是创建字符串的时候,程序首先会 
在这个String缓冲池中寻找相同值的对象,在第一个程序中,s1先被 
放到了池中,所以在s2被创建的时候,程序找到了具有相同值的 s1 
将 s2 引用 s1 所引用的对象"Monday" 

第二段程序中,使用了 new 操作符,他明白的告诉程序: 
“我要一个新的!不要旧的!”与是一个新的"Monday"Sting对象被创 
建在内存中。他们的值相同,但是位置不同,一个在池中游泳 
一个在岸边休息。哎呀,真是资源浪费,明明是一样的非要分开做什么呢? 

4. 继续潜水 
再次更改程序: 
public class TestString { 
    public static void main(String[] args) { 
        String s1 = "Monday"; 
        String s2 = new String("Monday"); 
        s2 = s2.intern(); 
        if (s1 == s2) 
            System.out.println("s1 == s2"); 
        else 
            System.out.println("s1 != s2"); 
        if (s1.equals(s2)) 
            System.out.println("s1 equals s2"); 
        else 
            System.out.println("s1 not equals s2"); 
    } 

这次加入:s2 = s2.intern(); 
哇!程序输出: 
s1 == s2 
s1 equals s2 
原来,程序新建了 s2 之后,又用intern()把他打翻在了池里 
哈哈,这次 s2 和 s1 有引用了同样的对象了 
我们成功的减少了内存的占用 

5. == 与 equals() 的争斗 
String 是个对象,要对比两个不同的String对象的值是否相同 
明显的要用到 equals() 这个方法 
可是如果程序里面有那么多的String对象,有那么多次的要用到 equals , 
哦,天哪,真慢啊 
更好的办法: 
把所有的String都intern()到缓冲池去吧 
最好在用到new的时候就进行这个操作 
String s2 = new String("Monday").intern(); 
这样大家都在水池里泡着了 。

 

 

import java.*;
import java.io.*;

public class hashcode
    {
    public static void main(String args [])
        {
        String o1 = new String("Hello");

        String o2 = new String("Hello");

        if (o1.equals(o2))
            System.out.println("equal");                 //equal

        System.out.println(o1.hashCode());               //69609650
        System.out.println(System.identityHashCode(o1)); //18581223
        System.out.println(o2.hashCode());               //69609650
        System.out.println(System.identityHashCode(o2)); //3526198

        o2 = "hello";

        if (!o1.equals(o2))
            System.out.println("not equal");             //not equal

        System.out.println(o1.hashCode());               //69609650
        System.out.println(System.identityHashCode(o1)); //18581223
        System.out.println(o2.hashCode());               //99162322
        System.out.println(System.identityHashCode(o2)); //7699183

        o1 = "Hello";
        o2 = "hello";

        if (!o1.equals(o2))
            System.out.println("not equal");             //not equal

        System.out.println(o1.hashCode());               //69609650
        System.out.println(System.identityHashCode(o1)); //14285251
        System.out.println(o2.hashCode());               //99162322
        System.out.println(System.identityHashCode(o2)); //7699183

        o1 = "Hello";
        o2 = "Hello";

        if (o1.equals(o2))
            System.out.println("equal");                      //equal

        System.out.println(o1.hashCode());                    //69609650
        System.out.println(System.identityHashCode(o1));      //14285251
        System.out.println(o2.hashCode());                    //69609650
        System.out.println(System.identityHashCode(o2));      //14285251

        System.out.println("hello".hashCode());               //99162322
        System.out.println("hello".hashCode());               //99162322
        System.out.println(System.identityHashCode("hello")); //7699183
        System.out.println("Hello".hashCode());               //69609650
        System.out.println("Hello".hashCode());               //69609650
        System.out.println(System.identityHashCode("Hello")); //14285251

        java.lang.System.exit(0);
        }
    }
原创粉丝点击