public class C04 {
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
               String a = "abc";
               a = new String("abc");
}}
上面的代码中,内存中有1个abc 还是2个abc,
请大家看下, 并且给出证明的办法。

解决方案 »

  1.   

    String a = "abc";
    System.out.println("abc".hashCode());
    System.out.println(a.hashCode());
    a = new String("abc");
    System.out.println(a.hashCode());
    a = new String("d");
    System.out.println(a.hashCode());
    前面3行输出都是一样的,说明只创建了一个对象"abc",a只是指向该对象"abc"的引用.
    内存中只有一个abc
      

  2.   

    内存中有2个abc
    1个在字符串常量池中;
    1个在heap中;证明方法:public class TEST {     
        public static void main(String[] args) {
            // TODO Auto-generated method stub
                   String a = "abc";
                   String b = new String("abc");
                   System.out.println(a==b);
        }
    }
    //输出结果
    false这里的==是比较a,b指向的物理地址是否相同,不同即为false,因此a和b指向不同的物理地址,即有2个abc另:楼主的代码是
    String a = "abc";
    a = new String("abc");即原来a 指向字符串常量池中的abc,后来a又指向heap中的abc,java里有垃圾回收机制,
    原常量池中的abc不知道会不会被回收掉,如果会的话,内存中就又剩1个abc了,当然这得在程序运行过了一段时间之后才会发生;
      

  3.   


    所有的字符串,只要其字面值一样,它的hashcode就是一样的……
    这里的问题与hashcode无关;
      

  4.   

    只要了解字符串常量池,==和EQUALS的区别.这个问题一点不难.
      

  5.   

    1个String很特殊的只要内容相同就是在同一个堆内存中
      

  6.   

    是一个 
    a==b 打印false 是因为a b是两个不同的对象呀 楼主问的是 内存中有几个"abc"
      

  7.   

    两个,常量池中一个,new的时候在堆中创建了一个
      

  8.   

    我的理解是1个,其他内存空间被回收
    首先第一句String a = "abc";开辟堆内存匿名对象把地址给了a
    其次第二句a = new String("abc");使用a重新指向新的堆内存空间,第一句的堆内存成为垃圾
    第二句里面的过程我认为是第一步在堆内存中新开辟"abc"匿名对象,第二步使用String的构造方法在堆内存中重新开辟一个空间,并且把“abc”这个值复制到新空间,此时第二句第一步开辟的内存空间作废,同时把第二步的新空间的地址给了a总之我觉得String a="a";
             String b="a";
             a==b因为String是使用了共享资源设计模式,指向同一资源
    而用String a=new String("a");
        String b=new String("a");
        a不等于b,内容相同,但是在堆上的地址不同
      

  9.   

    String a = "abc";
    a = new String("abc");第一个a在stack中,他的值是"abc";new String("abc")的意思是你在heap里又整出个"abc";两个"abc"的位置不同,前一个是在stack(栈)里面,第二个是在heap(堆)里面
      

  10.   

    2个,证明:C:\java>javap -c C04
    Compiled from "C04.java"
    public class C04 extends java.lang.Object{
    public C04();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   returnpublic static void main(java.lang.String[]);
      Code:
       0:   ldc     #2; //String abc
       2:   astore_1
       3:   new     #3; //class java/lang/String
       6:   dup
       7:   ldc     #2; //String abc
       9:   invokespecial   #4; //Method java/lang/String."<init>":(Ljava/lang/Strin
    g;)V
       12:  astore_1
       13:  return}
      

  11.   

    那 为什么 a和b有相同的hashCode?
    (我也不是 十分了解hashCode到底是 根据什么, 返回的是什么?)
      

  12.   

    那 为什么 a和b有相同的hashCode? 
    (我也不是 十分了解hashCode到底是 根据什么, 返回的是什么?)
      

  13.   

    2个,如果是:
    String a = "abc";
    String b = "abc";
    是指创建1个
      

  14.   

    两个
    String a="abc";是在栈里
    a=new String("abc"); 是在堆里
     都不是在一个容器里,当然是两个了
      

  15.   

                    String s1 = "You are hired!";
    String s2 = new String("You are hired!"); System.out.println("s1内存地址:"+System.identityHashCode(s1));
    System.out.println("s2内存地址:"+System.identityHashCode(s2)); if (s1==s2) {
    System.out.println("一个内存空间");
    } else {
    System.out.println("不是一个内存空间");
    }
      

  16.   

    1个,直接在eclipse里面用debug方式看就晓得了!!!String很特殊哈
      

  17.   

    在main方法里测试的代码:
    public static void main(String[] args){
        String s1 = "You are hired!"; 
        int a = System.identityHashCode(s1);
        System.out.println("s1内存地址:"+a); 

        s1 = new String("You are hired!"); 
        int b = System.identityHashCode(s1); 
        System.out.println("s1内存地址:"+b);     if (a==b) { 
             System.out.println("一个内存空间"); 
        } else { 
             System.out.println("不是一个内存空间"); 
        }
    }
    在我的一次运行中的输出:
    s1内存地址:14576877
    s1内存地址:12677476
    不是一个内存空间
      

  18.   

    String a = "abc"; 
    System.out.println(a.hashCode()); 
    a = new String("abc"); 
    System.out.println(a.hashCode()); 这个hashCode()输出一样的结果是因为String的hostCode()不是输出内存地址。Object.hashCode方法运行:
      public native int hashCode();而String方法运行:
        public int hashCode() {
    int h = hash;
    if (h == 0) {
        int off = offset;
        char val[] = value;
        int len = count;            for (int i = 0; i < len; i++) {
                    h = 31*h + val[off++];
                }
                hash = h;
            }
            return h;
        }
      

  19.   

    是两个......
    String a = "abc";
    时创建了一个"abc"放到了字符串池中
    a = new String("abc");
    在内存中开辟了一个新的空间 又放了一个"abc"
    而a是指向新的空间......
    用==可以比较出两个字符的地址是否相同 得到的结果为false也就说明了两个不在同一地方 
    所以我支持两个.
      

  20.   

    2个string对象
    HashCode一样是因为String类的HashCode方法是根据字符串的字面值计算的
    public int hashCode()返回此字符串的哈希码。String 对象的哈希码根据以下公式计算: 
     s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
    使用 int 算法,这里 s[i] 是字符串的第 i 个字符,n 是字符串的长度,^ 表示求幂。(空字符串的哈希值为 0。) 
    所以虽然两个字符串对象在不同的地方,值确实一样的,HashCode当然一样!
      

  21.   

    public class Text1 
    {        
            public static void main(String[] args) {
                // TODO Auto-generated method stub
                       String a = "abc";
                       String b = new String("abc");
                       System.out.println(a==b);//false
       System.out.println(a.equals(b));//true
         }
    }
    说明只创建了一个对象
    故内存中只存在一个"abc"