String s1="this is the first string.";
String s2=new String("this is the first string.");
请问一下两者有何区别 谢谢

解决方案 »

  1.   

    String s1="this is the first string.";
    这个分配在jvm的终生代里面
    String s2=new String("this is the first string.");这个分配在jvm的新生代里面。
    所以不是同一个对象。s2.intern()之后 s1==s2
      

  2.   

    第一个创建了一个对象
    第二个创建了两个对象
    所以他们两个不相等 !
    ??????????????????????????第一个对象不一定要创建 而第二个对象是一定会创建的 
    String s2=new String("this is the first string.");这行可以怎么写:String s2=new String();
    s2="this is the first string.");也就是说它也是创建了一个对象` 不同意楼上的观点!!~
      

  3.   

    喔 打错了 是这样: s2="this is the first string.";
      

  4.   

    其实最主要的是java在处理字符串中的机制。
    在java中,有个字符串池,在这个池中存放了系统使用过的字符串,
    当用户再次使用时,java会自动引用。
    如下:
    String s="abc";
    String s2 = "abc";
    当定义s时,java会在字符串池中新生成一个对象,"abc";
    当用户定义这样(String s2 = "abc";)定义s2时,java机制会在字符串池中查找有没有"abc"对象,如果有,就将对象的引用给s2。
    所以,此时,s 和s2是同一个对象。
    但是,当用户用new来生成字符串对象时,java的处理机制和其它的对象相同。
    所以当用户如下操作时:
    String s2 = new String("abc");
    它会像生成其它对象一样,生成一个新的对象"abc",这个时,s和s2是不同的对象,不过值相同。
    并且,当用户将s2指向别的地方时,新生成的对象会自动释放.
    如:s2 = "aaaaa".
    明白了吗?
      

  5.   

    第一个在内存中查找一下有没有S1这个地址 要是有就将”this is the first string“附值给s1 如果没有新建一个名叫S1空间 将值塞给S1,
    第二个不进行查找 直接在内存中新建一个空间 然后将"this is the first string."附值给s2这里用到了字符串池的概念!!!!
      

  6.   

    第一个就是直接开辟一个内在空间,用来存放this is the first string
    第二个.是先开辟一个内在空间来放地址,名为s2,s2用来指向this is the first string