String str = "abc" ;和 
String str = new String(); str="abc";
有什么区别,讲得详细一点。

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【feptest】截止到2008-07-17 14:24:29的历史汇总数据(不包括此帖):
    发帖的总数量:1                        发帖的总分数:20                       每贴平均分数:20                       
    回帖的总数量:8                        得分贴总数量:7                        回帖的得分率:87%                      
    结贴的总数量:0                        结贴的总分数:0                        
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:1                        未结的总分数:20                       
    结贴的百分比:0.00  %               结分的百分比:0.00  %                  
    无满意结贴率:---------------------无满意结分率:---------------------
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
      

  2.   

    区别就一点,第二个先生成了一个值为空“”的String对象
    然后做和第一个一样的操作貌似没说,本来也就这么回事
      

  3.   

    http://topic.csdn.net/u/20080716/11/2dbbb5e6-31a4-4e04-8ff1-0c72167ef24f.html
      

  4.   

    String s1="hello";
    String s2="hello";
    如果你去比较 s1==s2   会告诉你是true.
    这是字符串常量,s1和s2是 匿名String对象  把="hello"赋值给s2的时候不会再创建匿名对象。String s1=new String();
    s1="hello";
    String s2=new String();
    s2="hello";这时你再去比较 s1==s2    系统会告诉你是false
    因为s1和s2是两个不同的对象
      

  5.   

    这不还是true吗?
    你new了一个新对象就放弃引用了
      

  6.   


    String s1 = new String("hello");
    String s2 = new String("hello");
    这样执行s1 == s2会返回false
      

  7.   

    这不是TURE了,你可以自己编程调试一下,用s1==s2这样去比较的话,是比较地址,s1和s2是两个不同的对象,地址不一样。这个c语言不一样,c语言用这样的等式比较是比较的内容。而java如果用s1.compareTo(s2),返回的结果才是TRUE,这个比较的是内容
      

  8.   

    ZangXT 说的是对的
    String s1=new String(); 
    s1="hello"; 
    String s2=new String(); 
    s2="hello"; 还是true
      

  9.   

    String str = "abc" 
    这里面的abc放在常量池中
    String str = new String(); str="abc";
    而这的abc放在堆内存中
      

  10.   

    怎么都这么晕呢?
    注意下面两行的区别:
    String str=new String("abc");String str=new String();str="abc"
      

  11.   

    String s1="hello"; 
    String s2="hello"; 
    如果你去比较 s1==s2  会告诉你是true. 
    这是字符串常量,s1和s2是 匿名String对象  把="hello"赋值给s2的时候不会再创建匿名对象。 
    String s1=new String("hello"); 
    String s2=new String("hello"); 
    这时你再去比较 s1==s2    系统会告诉你是false 
    因为s1和s2是两个不同的对象 
    应该是这样。
    写错了。ZangXT 是对的。
      

  12.   

    String s1="hello"; 
    String s2="hello"; 
    如果你去比较 s1==s2  会告诉你是true. 
    这是字符串常量,s1和s2是 匿名String对象  把="hello"赋值给s2的时候不会再创建匿名对象。 String s1=new String(); 
    s1="hello"; 
    String s2=new String(); 
    s2="hello"; 这时你再去比较 s1==s2    系统会告诉你是false 
    因为s1和s2是两个不同的对象 
      

  13.   

    String str="abc"这个abc放在String池里
    而String str = new String(); 
    str是一个对象 对象引用了堆里的"abc"数据
    首先存放的位置就不相同
    使用的时候也不一样的
      

  14.   

    说的相当详细:
    http://zangweiren.javaeye.com/blog/209895
      

  15.   

    String str = "abc"  
    等价于 
    String str = new String("abc");
    -------------------------------------
    String str = new String(); str="abc"; 
    等价于
    String str="";
    str="abc"; ---------
    运行结果都是一样的
      

  16.   


    应该是并不严格等价,考虑常量池问题
    String str = "abc"是一个“abc”对象在常量池(只有一个String对象)
    String str = new String("abc");是一个“abc”对象在常量池,一个“abc”对象在heap(有两个String对象)String str = new String(); str="abc";是一个空串“”对象在heap,一个“abc”对象在常量池
    String str="";str="abc";是一个空串“”对象在常量池,一个“abc”对象在常量池只是使用来讲,并没什么不同。