答案是两个,为什么啊?

解决方案 »

  1.   

    创建了一个对象,一个引用.对象new String("xyz")呆在堆里,那个引用s在栈中
      

  2.   

    一个是String值对象“xyz”,一个是指向“xyz”的String引用对象s。  
      

  3.   

    创建了一个对象,一个引用.对象new String("xyz")呆在堆里,那个引用s在栈中 ymfhcn(这痞子真帅) 的回答很具体了!!
      

  4.   

    我不太同意:
    创建了1个引用1个对象,不是2个对象!
    相当于
    String s;//引用
    s = new String("xyz");//用NEW创建的叫对象,引用指向对象了!
      

  5.   

    跟一个问题:
    String s = new String("xyz");和String s = "xyz";有区别吗?
      

  6.   

    楼上,这个问题在 hsnh1981(黑色男孩) 其实已经回答了。
    String s = "xyz"只创建了一个对象,而String s = new String("xyz")则创建了一个引用和一个对象。
      

  7.   

    我也记得这个问题的答案是两个String对象。
    如果是普通类的话是一个对象一个引用,不过String类好像有点特殊。
    String s = new String("xyz");和String s = "xyz"肯定是有区别的。
    顶一把,高手来解释一下阿。
      

  8.   

    刚刚看了一下jdk的api文档,String s=new String("xyz");的确是两个对象,问题不是出在引用s上面,而是构造函数上!jdk里对以String为参数的构造函数的相关的解释如下
    String(String original) 
              Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.这样就很清楚了"xyz"作为参数是一个String对象,而s是它的一个copy,第二个String对象,所以答案是两个String对象。
    btw看文档还是很有用的。
      

  9.   

    回答错误的人八成是因为编译原理没学好
    "xyz"本身作为字符常量,在汇编语言中应该作为常量放在数据段,Java有一个类似数据段的constant pool保存这个常量,在classloader加载这个类的时候就把"xyz"和这个类的其他一些信息放在constant pool
    new String("xyz")根据常量"xyz"在heap上创建String对象
    s只不过是stack上的一个引用,指向heap上的String对象
    所以,一共两个对象
      

  10.   

    是滴,引用不算一个对象吧,如果是对象应该从Object类继承,但是如果是这样的话,这么重要的理论应该在教科书里摆明了说
    所以,是一个引用,一个对象
      

  11.   

    回复人: ogmios() ( ) 信誉:100  2006-02-08 09:29:00  得分: 0  
    楼上,这个问题在 hsnh1981(黑色男孩) 其实已经回答了。
    String s = "xyz"只创建了一个对象,而String s = new String("xyz")则创建了一个引用和一个对象。String s = "xyz"这句代码中的s就不是引用了?你的回答完全不贴边.
    还有,一个引用和一个对象也算是两个对象?引用怎么可能也是一个对象,这种说法就更说不过去了.
      

  12.   

    活力四射的回答是一种可能的正确答案.
    绿色毒汁的解释也有点问题,没说清楚是哪两个对象,heap上的肯定是一个对象,另一个是哪个?在constant pool中?
    还有,JAVA编程思想中也明确说过,对于每个字符串常量,编译器会将其自动生成一个String对象.至于这个自动生成的对象是存放在哪里的则没有说.
    因此还有另一种可能的正确答案,就是String s = "xyz"和String s = new String("xyz")是完全相同的,写法不同而已.
      

  13.   

    基础--两个对象
    1."xyz"--常量,存在于常量内存区
    2.new String("xyz")--变量,存在于堆栈中
      

  14.   

    java里面的对象都是放在heap中,引用是可以放到stack中的,所以说引用s是对象肯定是错误的说法,String s=new String("xyz");在编绎时会把"xyz"作为常量放到类的常量池中,运行时调用new指令时会再在堆中产生一个String类型的对象,把常量池中的"xyz"的值复制过来,然后让s指向堆中的String对象,所以应该是两个对象,一个是String类的Class对象中的常量字符串对象,一个是new出来的堆中的字符串对象.而String s="abc";则只产生了一个对象,就是String类的Class对象中的常量字符串对象,它只是让s指向了常量池中的那个常量字符串对象.
      

  15.   

    个人感觉应该是:一个或者两个  :o"xyz"可能是新创建,也可能是堆栈中已有的。
      

  16.   

    同意 mengxiaoyong ,两个对象
      

  17.   

    两个对象.因为没有字符串都是常量,这个代码的作用是首先创建一个常量"xyz",然后把该常量复制到对象s占用的空间中.
      

  18.   

    同意yonghar(ohno)如果
    在执行
    String s = new String("xyz");之前有String s1 = "xyz"或String s1 = new String("xzy")呢那
    String s = new String("xyz");还是两个吗?
      

  19.   

    mengxiaoyong() ( ) 的回答有一定的道理, 是一种可能的正确答案, 但有一点还是有争议的, 就是常量池中的是否算是一个对象, 可以参见<JAVA编程思想>第二章里面关于存储位置的解释关于静态存储的解释的最后一句话: JAVA对象本身是决不会放在静态存储中去的.
      

  20.   

    >>mengxiaoyong() ( ) 的回答有一定的道理, 是一种可能的正确答案, 但有一点还是有争议的, 就是常量池中的是否算是一个对象, 可以参见<JAVA编程思想>第二章里面关于存储位置的解释关于静态存储的解释的最后一句话: JAVA对象本身是决不会放在静态存储中去的.你曲解了TIJ,那本书说的是静态存储,不是literal. literal是可以放在constant pool,你可以参看JVM Spec 三个章节4.4, 3.5.4, 3.5.5
    一般来说heap中的一块特殊区域保存着method area,method区类似汇编的代码段,包含了方法(函数)和constant pool和其他信息等
    constant pool内又包含了编译器词法/语法分析阶段找到和预计算出来的所有常量,不光是primitive numeric literal比如,还包含String literal. 还有类的方法属性的引用名等。>>绿色毒汁的解释也有点问题,没说清楚是哪两个对象,heap上的肯定是一个对象,另一个是哪个?在constant pool中?
    我说了阿,当然是literal在constant pool,new 在 heap阿
      

  21.   

    给一段程序验证一下
    String s1 = "test";//编译期放入constant pool
    String s2 = new String("test");

    System.out.println(s1 == "test");
    System.out.println(s2 == "test");
    System.out.println(s1 == s2);

    s2 = s1.intern();//把constant pool内的"test"给s2
    System.out.println(s1 == "test");
    System.out.println(s2 == "test");
    System.out.println(s1 == s2);
      

  22.   

    上面程序的运行结果,我做的是:
    s1 == "test" true
    s2 == "test" false
    s1 == s2 false
    s1 == "test" true
    s2 == "test" true
    s1 == s2 true
    对s2 == "test" false感到吃惊。我水平太菜了
      

  23.   

    Because the compiler automatically creates a new String object for every literal string it encounters, you can use a literal string to initialize a String.
    String s = "Test";
    The above construct is equivalent to, but more efficient than, this one, which ends up creating two Strings instead of one:
    String s = new String("Test");
    The compiler creates the first string when it encounters the literal string "Test", and the second one when it encounters new String.
    source: http://java.sun.com/docs/books/tutorial/java/data/stringsAndJavac.htmlAdvice: If you are C/C++ programmer be careful with shortcut assignment operator "+=" in Java when you work with Strings!Why: The shortcut assignment operator += when used with Strings may confuse C and C++ programmers at first. Recall that a += b is equivalent to a = a + b. Let's look at two codesamples written in C++ and the Java programming language:
    //C++ code
    string* s1 = new string("hello");
    string* s2 = s1;
    (*s1) += " world";
    cout<<*s1<<endl<<*s2<<endl;
    return 0;
    //s1 = s2 = "hello world"//Java programming language code
    String s1 = "hello";
    String s2 = s1;
    s1 += " world";
    System.out.println(s1 + "\n" + s2);
    //s1 = "hello world" and s2 = "hello"In the C++ example, the strings s1 and s2 print the same result because they both point to the same address. In the Java programming language, Strings can't be modified, so the + operator must create a new String when "world" is appended to s1.source: http://java.sun.com/docs/books/tutorial/java/data/stringsAndJavac.html