String s = null;
System.out.println(s);为什么会输出null,null不是字符串啊,不是空吗

解决方案 »

  1.   

    自动转换了
    System.out.println(s)
    会把s转化成字符串
      

  2.   

    正解,空和null是不一样的
      

  3.   

    自己去看println方法的实现即可public void print(String s) {
    if (s == null) {
        s = "null";
    }
    write(s);
        
      

  4.   


    public void print(String s) {
        if (s == null) {
            s = "null";
        }
        write(s);
        高
      

  5.   

    String比较特殊,它本身也是个对象。lz可以查查源码 /**
         * Print a string.  If the argument is <code>null</code> then the string
         * <code>"null"</code> is printed.  Otherwise, the string's characters are
         * converted into bytes according to the platform's default character
         * encoding, and these bytes are written in exactly the manner of the
         * <code>{@link #write(int)}</code> method.
         *
         * @param      s   The <code>String</code> to be printed
         */
        public void print(String s) {
    if (s == null) {
        s = "null";
    }
    write(s);
        }
      

  6.   


    而且s的初始化的值是null;所以直觉输出s也就是一个null,
    再加上s是指向一个空对象。那么它输入的还是null
      

  7.   

    好像SYSTEM.OUT.PRINTLN时,使用了String.valueOf().因此不会报异常,并把"null"打出
      

  8.   

    String s=null;
    boolean answer=false;
    if(s=="null"){
    answer=true;
    }
    System.out.print(answer);楼主可以看看这个思考思考,,