您的位置:首页 > 其它

String a = new String(new char[] { 'a', 'b', 'c', 'd' });

2012-10-21 21:28 316 查看
public class StringInternTest2 {   
    public static void main(String[] args) {   
        // 使用char数组来初始化a,避免在a被创建之前字符串池中已经存在了值为"abcd"的对象
		String a0="abcd";
        String a = new String(new char[] { 'a', 'b', 'c', 'd' });   
        String b = a.intern(); 
        if (a0 == a) {   
            System.out.println("a0 == a");   
        } else {   
            System.out.println("a0 != a");   
        } 
		
        if (b == a) {   
            System.out.println("b被加入了字符串池中,没有新建对象");   
        } else {   
            System.out.println("b没被加入字符串池中,新建了对象");   
        }   
    }   
} 
/*
a0 != a
b没被加入字符串池中,新建了对象
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐