有一点解释
String a = "aaa";
那么a完全可以当作基本类型来用

解决方案 »

  1.   

    这个对新人学习java面向对象很有帮助
      

  2.   

    String a="abc";
        String b=new String("abc");
        System.out.println(a=="abc");//true 因为a和"abc"指向同一个内存引用
        System.out.println(b=="abc");//false 因为b 和"abc"不同引用
        System.out.println(a==b);    //false 同上
        System.out.println(a.equals("abc"));    //true 因为是判断内容,不是引用
        System.out.println(b.equals("abc"));    //true 同上2.  String aaa=null;
        String aaaa="";
        
        System.out.println(aaa==null);    //true
        System.out.println(aaaa==null);   //false 明显就不是null!
        String abc=aaa+"ttt";      
        System.out.println(abc);          //nullttt 因为在字符串加时,null被当成"null"的3.public class a1{
      public int aa=0;
      class a2 {
        public int aa=1;
      }
      public void init() {
        class a3 {
          public int aa=2;
        }
      }
    }编译结果a1.class(全局) a1$a2.class(inner class) a1$1a3.class(method内部).
    4. int[][] a1={{110,120},{210,220}};
       int[][] a2={{11,12,13}{21,22,23},{31,32,33}}; 
       System.arraycopy(a1,0,a2,0,a1.length);输出a2会是
    {{110,120},{210,220},{31,32,33}};因为第三个元素没有被覆盖
      

  3.   

    这些问题就算SCT也未必能搞清楚
      

  4.   

    这种问题就不用问
    我回答一个吧:
    1。false
       false
       true
       true
       true 怎么样,全对吧,呵呵
      

  5.   

    to  ChDw(米):
    为何literal型变量例如true,false,null
    会被当作string处理?
    java规范中好像没有提到这种特例吧?毕竟他们没有toString()函数啊
      

  6.   

    我是菜鸟,我认为是:
    1.
    true //这个不用说了吧
    false //我认为是b是指向新建的字符串对象的指针,所以不能直接==
    false //同上
    true  //这是对比字符串对象的内容,都有是"abc",所以相等
    true  //同上
    2.
    true  //都是null值
    false //字符串''不等于null(这是常识了)
    nullttt //println会自动用toString方法转打印值为字符串,所以结果是转换后字符串相加的结果
    3.三个,因有两个内部类
    a1是public的
    a2只能在a1中调用
    a3只能在a2中调用
    4.
    ={{110,120},{210,220},{31,32,33}}; 
    这得看arraycopy说明了
      

  7.   

    to  ChDw(米):
    二维数组覆盖,进行的是‘行Reference’覆盖。
    JAVA为啥设计成这样?
      

  8.   

    to ChDw(米):
    String a="abc";
        String b=new String("abc");
        System.out.println(a=="abc");//true 因为a和"abc"指向同一个内存引用
        System.out.println(b=="abc");//false 因为b 和"abc"不同引用
    ======================================================================你说System.out.println(b=="abc");//false 因为b 和"abc"不同引用,那是为什么呢?分析一下 String b = new String("abc")发生的情况好吗?我觉得如果照你的理论,"abc"这个 静态内存分配的区域应该全局一样,这样才会出现a到哪里和"abc"比较地址都一样的情况.那 new String("abc");这里面的"abc"是不是和a="abc"那个 /abc/一样的地址呢,这样的话,new String(...)那难道不是把"abc"的地址给引用下来,再付给b吗?除非new String(...)触发的是拷贝构造函数....
    但是为何=号触发的又是引用赋值而不是拷贝构造函数呢?(难道未被重载操作符?)
      

  9.   

    to liuyxit(深蓝(编程知识恶补中...)) :你有几处回答错误.
    nullttt //println会自动用toString方法转打印值为字符串,所以结果是转换后字符串相加的结果
    ------------------
    null是literal型,不是class,因此不会继承Object.toString().,何来toString之说
    a2只能在a1中调用
    ----------------------------
    错,a2是可以在别的class中也调用的,只有a3最可怜
      

  10.   

    a = "abc",这样并没有创建新的对象,只是使得a指向了"abc"
    b = new String("abc")这样不同,这里明确的调用了String的构造函数,新建了一个新的对象,b正是指向了这个对象。所以a != b
    to  ChDw(米):
    为何literal型变量例如true,false,null
    会被当作string处理?
    java规范中好像没有提到这种特例吧?毕竟他们没有toString()函数啊这里调用的是String.valueOf(variant);而不是toString方法
    如果这个variant是Object则会在valueOf方法内部return variant.toString();或者varaint == null时返回"null",这个是Java对变成String方法的做法
      

  11.   

    to ChDw(米):
    能解释一下 String的奇怪现象吗?
    String在java规范中并不是一个primitive类型,
    但是
    String ="abc"的时候,String就具有primitive的特性
    在任何地方宣称的"abc"甚至"ab" + "c"等等都是相等的!也就是说
    "abc"== ("ab" +"c")
    也是ture!?  如何解释?
      

  12.   

    to  ChDw(米):
    二维数组覆盖,进行的是‘行Reference’覆盖。
    JAVA为啥设计成这样?java中二维数组中a[0] a[1]是不需要等长的,可以每个都不一样
    其实arrayCopy你可以这样认为
    a2[0] = a1[0];
    a2[1] = a1[1];所以结果就是上面的写了
      

  13.   

    结果是
    true//“abc”是常量,String a = "abc";因此为true
    false//String b=new String("abc")是new出来的,和"abc"常量不是同一个对象
    false//对句柄的比较,也就是比较是否为同一个对象
    true
    true
      

  14.   

    能解释一下 String的奇怪现象吗?
    String在java规范中并不是一个primitive类型,
    但是
    String ="abc"的时候,String就具有primitive的特性
    在任何地方宣称的"abc"甚至"ab" + "c"等等都是相等的!也就是说
    "abc"== ("ab" +"c")
    也是ture!?  如何解释?-----------------------------
    因为
    String abc = "abc";
    这个"abc"是放在一个公用的数据段里面的。这个数据是可以复用的
    所以
    String cba = "abc";
    String cba = abc;
    也是复用了同一个"abc",所以他们的==操作是true
    但是当例如
    cba改变值的时候,例如
    cba+"c"=="abcd"这是false
    因为这时候会重新生成一个"abcd",和编译时候分配的"abcd"是不同的,因此两个是不同的hashcode至于
    "abc"== ("ab" +"c")
    abc==("ab"+"c")
    是因为编译内就把"ab"+"c"编译成"abc"了,这是符合java标准和一般的编译标准的
    你可以反编译一下就明白了至于第一点,这是为了提高java的性能,因为,大量无效的字符串往往是占用资源最多的地方,所以,一般是能复用就复用
      

  15.   

    个人认为 ChDw(米) 的答案是对的,
     
    补充:
    1、String a="abc";时虚拟机为"abc"分配空间,再把a引用过去,再次调用a=="abc"为两个相同引用的比较。2、public class a1{
      public int aa=0;
      class a2 {
        public int aa=1;
      }
      public void init() {
        class a3 {
          public int aa=2;
        }
      }
    }
    中aa为的命名范围为各个类,所以互不影响,在这个类的对象中都存在,值为该类中初始的值。
      

  16.   

    to  zxbest(.bin)补充:
    1、String a="abc";时虚拟机为"abc"分配空间,再把a引用过去,再次调用a=="abc"为两个相同引用的比较。
    ——————————————————
    PS:在java中, =运算符,对primitive type是传值,对object type是传Reference(既内存地址);
    如你的解释,=="abc" 则"abc"也是一个新的Object. 匿名构造的。
    这一点,我明白,相信你也清楚。
    但在这里不是考虑的重点,看看楼上其它的回答。
    2、public class a1{
      public int aa=0;
      class a2 {
        public int aa=1;
      }
      public void init() {
        class a3 {
          public int aa=2;
        }
      }
    }
    中aa为的命名范围为各个类,所以互不影响,在这个类的对象中都存在,值为该类中初始的值。
    ————————————————————————
    你在说什么?????
      

  17.   

    to  ChDw(米):
    为何literal型变量例如true,false,null
    会被当作string处理?
    java规范中好像没有提到这种特例吧?毕竟他们没有toString()函数啊这里调用的是String.valueOf(variant);而不是toString方法
    如果这个variant是Object则会在valueOf方法内部return variant.toString();或者varaint == null时返回"null",这个是Java对变成String方法的做法
    ====================================================================== 
    String.valueOf有boolean型参数,不过对于null...却没有valueOf这种操作不信你试试编译
    System.out.println(String.valueOf(null));
      

  18.   

    to  zxbest(.bin)补充:
    1、String a="abc";时虚拟机为"abc"分配空间,再把a引用过去,再次调用a=="abc"为两个相同引用的比较。
    ——————————————————
    PS:在java中, =运算符,对primitive type是传值,对object type是传Reference(既内存地址);
    如你的解释,=="abc" 则"abc"也是一个新的Object. 匿名构造的。
    这一点,我明白,相信你也清楚。
    但在这里不是考虑的重点,看看楼上其它的回答。
    2、public class a1{
      public int aa=0;
      class a2 {
        public int aa=1;
      }
      public void init() {
        class a3 {
          public int aa=2;
        }
      }
    }
    中aa为的命名范围为各个类,所以互不影响,在这个类的对象中都存在,值为该类中初始的值。
    ————————————————————————
    你在说什么?????
      

  19.   

    to  ChDw(米):
    为何literal型变量例如true,false,null
    会被当作string处理?
    java规范中好像没有提到这种特例吧?毕竟他们没有toString()函数啊这里调用的是String.valueOf(variant);而不是toString方法
    如果这个variant是Object则会在valueOf方法内部return variant.toString();或者varaint == null时返回"null",这个是Java对变成String方法的做法
    ====================================================================== 
    String.valueOf有boolean型参数,不过对于null...却没有valueOf这种操作不信你试试编译
    System.out.println(String.valueOf(null));
      

  20.   

    楼上的,没你想的那么复杂
    只不过是做了个判断而已:
    if (s == null) {
        s = "null";
    }
    true和false同样是:)
      

  21.   

    因为 equals()比较的是内容和地址,String 是一个对象它定义的变量不会有地址,
    实例化就会有地址所以输出的结果是不一样的
      

  22.   

    这个问题会有难度!!!!!!!!!!!看一看JAVA2编程思想,你会发现这个问题很简单.通过这个小问题,就容易让我们想到,学习编程,其中的一块"微机原理"是很重要的.建议看高等教育出版社出版的那本教材比较好.请给分!
      

  23.   

    to  wdx1632(lg):
    我们只是想探究一下编写java的人是怎么处理这个问题细节的,
    跟你研究微机原理有屁的关系啊!!
      

  24.   

    to wdx1632(lg)
    傻比鸟人屁都不懂还来混分!向人家 jackjingsg(飞翔的精灵) 学习学习!不懂就不要乱发言。
      

  25.   

    根据分析,我们来总结一下吧1.java处理字符串相加,String 的加其实调用的是StringBuffer.Append,看下面The Java language provides special support for the string
    concatenation operator ( + ), and for conversion of
    other objects to strings. String concatenation is implemented
    through the <code>StringBuffer</code> class and its
    append method.
    String conversions are implemented through the method
    toString, defined by Object and
    inherited by all classes in Java. //对null的特殊处理
    The characters of the <code>String</code> argument are inserted, in 
    order, into this string buffer at the indicated offset, moving up any 
    characters originally above that position and increasing the length 
    of this string buffer by the length of the argument. If 
    str is null, then the four characters 
    "null" are inserted into this string buffer.//下面是String相加的时候对几个literal的特殊处理!
    public synchronized StringBuffer append(boolean b) {
            if (b) {
                int newcount = count + 4;
                if (newcount > value.length)
                    expandCapacity(newcount);
                value[count++] = 't';
                value[count++] = 'r';
                value[count++] = 'u';
                value[count++] = 'e';
            } else {
                int newcount = count + 5;
                if (newcount > value.length)
                    expandCapacity(newcount);
                value[count++] = 'f';
                value[count++] = 'a';
                value[count++] = 'l';
                value[count++] = 's';
                value[count++] = 'e';
            }
    return this;
        }
    //如果是null的话,看下面
    //in java.lang.StringBuffer
        public synchronized StringBuffer append(String str) {
    if (str == null) {
        str = String.valueOf(str);
    } int len = str.length();
    int newcount = count + len;
    if (newcount > value.length)
        expandCapacity(newcount);
    str.getChars(0, len, value, count);
    count = newcount;
    return this;
        }//in java.lang.String
        public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
        }
      

  26.   

    我现在公布第一道题的标准正确答案,<<勤劳的民工>>猜测也是相对比较正确的,但还不是完全正确
    大家请仔细阅读下面的文字!The following are examples of string literals: "" // the empty string
    "\"" // a string containing " alone
    "This is a string" // a string containing 16 characters
    "This is a " + // actually a string-valued constant expression,
    "two-line string" // formed from two string literals

    Because Unicode escapes are processed very early, it is not correct to write "\u000a" for a string literal containing a single linefeed (LF); the Unicode escape \u000a is transformed into an actual linefeed in translation step 1 (§3.3) and the linefeed becomes a LineTerminator in step 2 (§3.4), and so the string literal is not valid in step 3. Instead, one should write "\n" (§3.10.6). Similarly, it is not correct to write "\u000d" for a string literal containing a single carriage return (CR). Instead use "\r". Each string literal is a reference (§4.3) to an instance (§4.3.1, §12.5) of class String (§4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (§15.28)-are "interned" so as to share unique instances, using the method String.intern.
    Thus, the test program consisting of the compilation unit (§7.3): package testPackage;
    class Test {
    public static void main(String[] args) {
    String hello = "Hello", lo = "lo";
    System.out.print((hello == "Hello") + " ");
    System.out.print((Other.hello == hello) + " ");
    System.out.print((other.Other.hello == hello) + " ");
    System.out.print((hello == ("Hel"+"lo")) + " ");
    System.out.print((hello == ("Hel"+lo)) + " ");
    System.out.println(hello == ("Hel"+lo).intern());
    }
    }
    class Other { static String hello = "Hello"; }and the compilation unit:package other;
    public class Other { static String hello = "Hello"; }produces the output:true true true true false trueThis example illustrates six points:1.Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1). 
    2.Literal strings within different classes in the same package represent references to the same String object. 
    3.Literal strings within different classes in different packages likewise represent references to the same String object. 
    4.Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals. 
    5.Strings computed at run time are newly created and therefore distinct. 
    6.The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.