public String makinStrings() { 
String s = "ASDASD"; 
s = s + "344"; 
s = s.substring(2, 5); 
s = s.toUpperCase(); 
return s.toString(); 

问题是:在这个过程中,有几个String对象创建 
哪位大哥告诉小弟一下,谢谢了
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
正确答案是3个。。哪个大哥帮我解释一下是哪3个

解决方案 »

  1.   

    String s = "ASDASD"; 第一个1个
    s = s + "344";    第二个
    s = s.substring(2, 5); 第三个s = s.toUpperCase();//this
    return s.toString();//this
      

  2.   

    "ASDASD"
    "344"
    s.substring(2, 5)会得到一个新字符串。
      

  3.   

    public String makinStrings() { 
    String s = "ASDASD"; //1
    s = s + "344"; //2
    s = s.substring(2, 5); //3
    s = s.toUpperCase(); //不是新对象
    return s.toString(); 、、不是新对象

      

  4.   

    "ASDASD", "344" & S?
      

  5.   

    String s = "ASDASD"; 
    s = s.substring(2, 5); 
    s = s.toUpperCase(); 
    3个
      

  6.   

    各位不好意思,第一句打错了。。
    public String makinStrings() {
    String s = "asdddddd";  //原题是小写
    s = s + "344";
    s = s.substring(2, 5);
    s = s.toUpperCase();
    return s.toString();
    }
    问题是:在这个过程中,有几个String对象创建
    哪位大哥告诉小弟一下,谢谢了
    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    正确答案是3个。。哪个大哥帮我解释一下是哪3个
      

  7.   

    s = s + "344"; //2 
    4楼的说第二个对象是"344"还是"s"啊
      

  8.   

    String s = "asdddddd";  //原题是小写 
    s = s + "344"; 
    s = s.substring(2, 5); 
      

  9.   

    String s = "asdddddd";   //这是一个
    s = s + "344";           // 这应该创建几个对象啊
    s = s.substring(2, 5);   //这是一个
    s = s.toUpperCase();     //这一句也应该创建一个新的对象了吧
      

  10.   

    String是不可改变的(final)类,改变了字面值,就会重新分配内存地址。应该是创建了四个对象。个人认为。。
      

  11.   

    第一个:ASDASD
    第二个:ASDASD344
    第三个:DASjava中的字符串是常量,都放在字符串常量池中,是可以共享的,它的值在创建之后不能更改(而C,C++中是可以改变的),所以被设计成final类型,当有一个新的字符串对象产生时,JVM会在常量池中查找是否有这个对象,如果有,将不会生成新的对象,所有堆中的引用持有相同的常量值。
    String s = "ASDASD"; 
    在常量池中生成一个对象 "ASDASD",s持有这个的引用,
    s = s + "344"; 
    JVM会在常量池中查找是否有这个字符串,如果没有,将生成一个新的字符串“ASDASD344”,将引用付给s,
    s = s.substring(2, 5);
    生成一个新的对象,将引用付给s
    s = s.toUpperCase()
    转换成大写后,值没有变,所以不会产生新对象
      

  12.   

    public String makinStrings() { 
    String s = "ASDASD"; 
    //这句不会创建对象,而是把常量池中的对象附给局部变量s
    s = s + "344"; 
    //这句等同于:(new StringBuilder(String.valueOf(s))).append("344").toString();而StringBuilder.toString会new String
    s = s.substring(2, 5);
    //substring执行的时候,除非结果正好跟原串相同,否则会new 一个新串
    s = s.toUpperCase(); 
    //跟substring类似,除非结果正好跟原串相同,否则会new 一个新串
    return s.toString(); 
    //这句不会,toString只是把s的this返回了

      

  13.   

    因此,如果String s = "ASDASD"; 只会new 2个字符串
    而如果String s = "asdddddd";  就有3个了(toUpperCase多了一个)
      

  14.   

    public String makinStrings() { 
    String s = "ASDASD";     //  这个不产生新对象, equals 跟 == 的区别应该清楚吧,去试一下,这种写法所有的对象引用的是同一个内存区域的东西,不会新开内存
    s = s + "344"; 
    s = s.substring(2, 5); 
    s = s.toUpperCase(); 
    return s.toString();    // 这个不产生新对象。
    }
      

  15.   

    s.toUpperCase()的内部逻辑记不清楚了。。如果是全都大写,也许不会创建新的对象,不过那样楼主给的答案就不对了,呵呵。
      

  16.   

    public String makinStrings() {
    String s = "asdddddd"; //一个对象s,注意不是"asdddddd",是s本身。
    s = s + "344";//第二个对象 asdddddd344
    s = s.substring(2, 5);//不创建对象
    s = s.toUpperCase();//对象DDD
    return s.toString();
    } 在类被加载的时候,"asdddddd"和"344"对象已经被创建。
    方法被调用的时候产生3个对象:s,"asdddddd344","DDD".
      

  17.   


    常量池里的对象是怎么来的,是JVM在初始化生成的吗?那第2句的"344"也是常量池里的吗?
    "ASDASD"这个是在什么时候创建的,是在String s = "ASDASD"这个时候创建的,还是JVM在初始化创建的?
      

  18.   

    常量池的对象怎么来的,具体我没研究过,不好瞎说。我只是估计是在JVM 加载类时创建的。
    我可以肯定的是String s = "ASDASD"; 没有创建对象
    跟String s = new String("ASDASD");是完全不一样的第2句的"344",毫无疑问,跟"ASDASD"是一样的,也是常量池中的
     
      

  19.   

    对字符串常量池的问题可以参考如下文章:
    String对象的理解进行时
      

  20.   

    谢谢各位的帮助。
    原题:
    public String makinStrings() {
    String s = "asdddddd";  
    s = s + "344";
    s = s.substring(2, 5);
    s = s.toUpperCase();
    return s.toString();

      我想在这个类编译时是不是实例了两个String对象,分别是 "asdddddd"、"344",然后它们被载入到文字池,
    当String s = "asdddddd"; 被执行时 java检测 "asdddddd" 在文字池中已存在,所以将相应的文字池中的
    对象付给s 这样理解对吗?
    请各位解评一下 谢谢!!
      

  21.   

    jvm规范:
    certain nonreference runtime values are derived from items found in the constant_pool table:A string literal (§2.3) is derived from a CONSTANT_String_info structure (§4.4.3) in the binary representation of a class or interface. The CONSTANT_String_info structure gives the sequence of Unicode characters constituting the string literal.The Java programming language requires that identical string literals (that is, literals that contain the same sequence of characters) must refer to the same instance of class String. In addition, if the method String.intern is called on any string, the result is a reference to the same class instance that would be returned if that string appeared as a literal. Thus, 
        ("a" + "b" + "c").intern() == "abc"must have the value true.To derive a string literal, the Java virtual machine examines the sequence of characters given by the CONSTANT_String_info structure. If the method String.intern has previously been called on an instance of class String containing a sequence of Unicode characters identical to that given by the CONSTANT_String_info structure, then the result of string literal derivation is a reference to that same instance of class String.Otherwise, a new instance of class String is created containing the sequence of Unicode characters given by the CONSTANT_String_info structure; that class instance is the result of string literal derivation. Finally, the intern method of the new String instance is invoked.
      

  22.   


    理解完全错误!你要分清楚编译和运行
    注意:我下面说的也不一定是对的,我只是根据常识做了一个猜测。
    JVM加载这个类的时候,在常量池中创建了2个对象,并给这2个对象编了号,比如第1号和第2号。
    在运行String s = "asdddddd";这句的时候,只是按指令把第1个对象附给了s。(而不是检测 "asdddddd" 在文字池中已存在)
      

  23.   


    呵呵,不过这篇文章比那篇稀烂的忽悠文章《JAVA堆和栈》晦涩难懂些,
    所以很多人还是都宁可看那篇稀烂的忽悠文章,而不是这篇从楼主的某些话里面,我还是能看到被那篇忽悠文章毒害的影子
      

  24.   

    表达能力太重要了,呵呵。
    经常看到很多人回答别人问题的时候就把那个《java堆与栈》引用过来说明问题,我很无奈。
      

  25.   

    让人郁闷的是写这个String分析的人blog上也转载了那篇,哭ing
      

  26.   


    非常感谢 !!
    那如果加载别的类的时候,文字池里已经有 "asdddddd"、"344" 这两个对象了,这时再加载当前类时难道不需要检测吗?
      

  27.   

    重点看这句:
    If the method String.intern has previously been called on an instance of class String containing a sequence of Unicode characters identical to that given by the CONSTANT_String_info structure, then the result of string literal derivation is a reference to that same instance of class String.Otherwise, a new instance of class String is created containing the sequence of Unicode characters given by the CONSTANT_String_info structure; that class instance is the result of string literal derivation. Finally, the intern method of the new String instance is invoked.
    关于String,注意6点:
        *      Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1).
        *      Literal strings within different classes in the same package represent references to the same String object.
        *      Literal strings within different classes in different packages likewise represent references to the same String object.
        *      Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.
        *      Strings computed by concatenation at run time are newly created and therefore distinct.The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
    http://blog.csdn.net/ZangXT/archive/2008/10/11/3057471.aspx
      

  28.   

    public String makinStrings() { 
    String s = "asdddddd"; //若常量池中没有"asdddddd"这个对象,会创建新的对象(相当于 new String("asdddddd")),如果已经存在就不会创建新的对象,s只是持有"asdddddd"的引用
    s = s + "344"; //产生新对象
    s = s.substring(2, 5); //产生新对象
    s = s.toUpperCase(); //产生新对象
    return s.toString(); 
    String对象是不可变的,如果修改String的值都会产生一个新的对象,改变的只是引用.