当用String str1="hello";语句时,在栈创建一个String对象;并具下次在用相类似的语句时无需再新创建值相同的对象,而直接利用已存在的.例如:                         String str2="hello";//此时用str2引用指向str1已创建的对象;当用String str3=new String("hello");语句时,创建一个新对象时在堆(heap)中;
请问此时是否也在栈(stack)中创建同样的对象呢?!(最好能给出个例程)

解决方案 »

  1.   

    String str1 = "hello";
    String str2 = "hello";
            String str3 = new String("hello") ;
            
            System.out.println(str1 == str2); // true
            
            System.out.println(str1 == str3); // false
    ----------        String str1 = "hello";
    String str2 = "hello";
    一个内容,两个引用        String str1 = "hello";
    String str2 = "hello";
    String str3 = new String("hello");
    两个内容,三个引用
      

  2.   

    自己写个程序试试不就知道了 String str1=new String("hello");
    String str2="hello";
    String str3="hello";
    if(str1==str2){
    System.out.println("str1==str2");
    }
    if(str2==str3){
    System.out.println("str2==str3");
    }
      

  3.   

    String str1 = "hello";
    String str2 = "hello";
    System.out.println(str1 == str2); // true
    //这个我知道这个结果是true.
    //我想知道一下,下面这个的结果是真还是假
    String str1 = new String("hello");
    String str2 = "hello";
    System.out.println(str1 == str2); // ????
      

  4.   

    呵呵,分不多,可问题也不复杂啊,明白人说那么一两句就明白了!
    -----------------------------------------------------------
    最佳答案:
    -----------------------------------------------------------
    public class HelloWorldApp
    {
    public static void main (String[] args)
    {
    String str1 = new String("hello");
    String str2 = "hello";
    String str3 = "hello";
    System.out.println(str1 == str2);//false 
    System.out.println(str2 == str3);//true
    }
    }
    ********************************************************************************
    可知:用new String("hello");语句时,只在堆(heap)中创建对象,不在栈(stack)中创建对象
         用"hello";语句时在栈(stack)中创建对象
    *********************************************************************************