Chinaunix首页 | 论坛 | 博客
  • 博客访问: 403570
  • 博文数量: 79
  • 博客积分: 4010
  • 博客等级: 上校
  • 技术积分: 897
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-10 23:22
文章分类

全部博文(79)

文章存档

2011年(1)

2010年(6)

2009年(39)

2008年(33)

我的朋友

分类: Java

2009-12-10 12:26:52

package com.test;

public class StringTest {

    public static void main(String[] args) {
        String s = new String("abc");//生成2个对象 一个在堆里,一个在String 池中

        
        String s1 = "abc";//生成0个对象,因为String 池中中存在

        
        String s2 = new String("abc");//生成1个对象,存于堆中

        
        System.out.println(s == s1);//false 前者在对中,后者在池中

        
        System.out.println(s == s2);//false 前者在对中,后者在String 池中

        
        System.out.println(s1 == s2);//false 两者存于池中的不同对象

        
        System.out.println(s == s.intern());//false 前者指向对中对象, 后者指向String 池中对象

        
        System.out.println(s1 == s1.intern());//true 两个都指向String 池中的对象

        
        System.out.println(s1 == s2.intern());//true 两个都指向String 池中的对象

        
        String hello = "hello";
        
        String hel = "hel";
        
        String lo = "lo";
        
        System.out.println(hello == "hel" + "lo");//true 常量+常量=String 池中原有的对象

        
        System.out.println(hello == "hel" + lo);//false 常量+变量 =在堆中生成新的对象

        
    }
}


阅读(724) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-01-28 13:24:52

你怎么可以做到各种语言都那么懂啊。。。

chinaunix网友2010-07-16 18:42:52

KK娱乐视频网,快乐齐分享 www.yulekk.com 搞笑视频,动漫视频,美女写真,靓丽车模,美女翻唱,精彩MV,经典DV