以下代码共创建了几个对象String s1=new  String("hello");
String s2=new  String("hello");
String s3=s1;
String s4=s2;请给出详细的解答,谢谢。

解决方案 »

  1.   

    String s1=new  String("hello");
    //创建2个对象,一个“hello",因为字符串是常量,一个s1,因为new 
    String s2=new  String("hello"); 
    //创建1个,因为'hello"已存在,所以不创建,创建一个s2,因为new
    String s3=s1; 
    //对象地址赋值,不创建对象,因为没有new
    String s4=s2; 
    //同上总计创建3个对象
      

  2.   

    String s1=new  String("hello"); 
    //创建2个对象,一个“hello",
    //hello 是在内存中因为字符串是常量
    //,一个s1,在堆栈中创建了个引用对象  
    String s2=new  String("hello");  
    //创建2个,因为有new 就会创建对象'hello",再在内存中创建hello,在堆栈中创建一个s2,也是对hello的引用
    String s3=s1;  
    //对象地址赋值,创建对象,因为没有new 
    String s4=s2;  
    //同上 总共有几个你加一下,
    有空去看一下 孙鑫的 java无难事 的视频 满有帮助的
      

  3.   


    三个,就是这样的,看看java虚拟机体系结构就知道了
      

  4.   

    一共只有一个String对象被创建,因为对于String类型来说,是有一个字符池,也就是说当你创建一个String对象的时候,首先会去字符池看有没有匹配的String 
      所以,s1,s2,s3,s4是指向同一个地址!
      

  5.   

    String[] a=new String[9];
    创建多少个对象?
      

  6.   


    同意. 也就是说说S1是一个对象, S2是一个对象, 然后S3和S4只是对象的引用(出现在Stack中).