String aa = new String("aa");
  System.out.println(aa==aa.substring(0,2));
  是true还是false啊?

解决方案 »

  1.   

     public String substring(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if (endIndex > count) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    if (beginIndex > endIndex) {
        throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
    }
    return ((beginIndex == 0) && (endIndex == count)) ? this :
        new String(offset + beginIndex, endIndex - beginIndex, value);
        }在这种情况下直接返回this,当然是true,不懂得问题可以参看源代码
      

  2.   

    substring()方法是子字符串从指定的 beginIndex 处开始,一直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。如:
     "hamburger".substring(4, 8) returns "urge"
     "smiles".substring(1, 5) returns "mile"
      

  3.   

    肯定是true了
    你用aa和aa.subString比较,都是对对象本身的操作
    无论在内存地址还是值都是相同的
      

  4.   

    对了,你要明白substring的用途,多点看看api
      

  5.   


    ==怎么比的是值了?
    最关键的原因是在((beginIndex == 0) && (endIndex == count))这种情况下,直接返回当前的字符串本身,其他的情况才会new一个String.
    jdk的源码已经很清楚了
      

  6.   

    结果是true
        public String substring(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if (endIndex > count) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    if (beginIndex > endIndex) {
        throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
    }
    return ((beginIndex == 0) && (endIndex == count)) ? this :
        new String(offset + beginIndex, endIndex - beginIndex, value);
        }return ((beginIndex == 0) && (endIndex == count)) ? this :
        new String(offset + beginIndex, endIndex - beginIndex, value);
      

  7.   

    当然是true了。==比较引用时是比较的是引用是否相同。
      

  8.   

    [code=Java]
    //下面源代码就了解了:
     public String substring(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if (endIndex > count) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    if (beginIndex > endIndex) {
        throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
    }
          // ((beginIndex == 0) && (endIndex == count)) 时返回自身的引用this     
    return ((beginIndex == 0) && (endIndex == count)) ? this :    new String(offset + beginIndex, endIndex - beginIndex, value);
        }
    [code]
      

  9.   

    public String substring(int beginIndex, int endIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        if (endIndex > count) {
            throw new StringIndexOutOfBoundsException(endIndex);
        }
        if (beginIndex > endIndex) {
            throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
        }
        return ((beginIndex == 0) && (endIndex == count)) ? this :
            new String(offset + beginIndex, endIndex - beginIndex, value);

        }
        
      

  10.   

    [code]
        public String substring(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if (endIndex > count) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    if (beginIndex > endIndex) {
        throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
    }
                    //这句返回了对象的本身。因为开始从0和结束又是它本身的长度。所以
    return ((beginIndex == 0) && (endIndex == count)) ? this :
        new String(offset + beginIndex, endIndex - beginIndex, value);
        }
    [/code]
      

  11.   


        public String substring(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if (endIndex > count) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    if (beginIndex > endIndex) {
        throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
    }
                    //这句返回了对象的本身。因为开始从0和结束又是它本身的长度。所以
    return ((beginIndex == 0) && (endIndex == count)) ? this :
        new String(offset + beginIndex, endIndex - beginIndex, value);
        }
      

  12.   


    原来substring返回Stirng本身的时候没有new一个新的String对象,学习了
      

  13.   

    String的subString方法生成的String与原来的String使用的是同一个char数组的引用
      

  14.   

    比较的还是内存地址,不光是进行值的比较.他们占的同一块内存地址,所以返回true
      

  15.   

    楼主是不是被类似于下面的代码干扰了?
    String aa = new String("aa");
    String bb = new String("aa"); 
            System.out.println(aa==bb);
    这样返回是 false;因为 == 比较的不仅仅是字符串的值,还有它的内在地址,所以虽然值相同,但由于内在地址不同,返回的仍然是 false.同样,楼主所问的这一题,你是不是也是这样理解的?
    String aa = new String("aa"); 
    System.out.println(aa==aa.substring(0,2)); 
    是不是你认为 aa.substrnig(0,2) 产生了一个新的字符串对象(相当于上面的bb)?所以被上面的结果所干扰了?呵呵,其实不是这样的, aa.substring(0,2) 它返回的是字符串 aa 本身!首先内存地址是没有改变的,继而值也没改变,所以结果就是 true.
    明白了吗?
      

  16.   

    str.substring(int index,int i)方法 它是在 str 这块内存地址上操作的,所以如果值也一样,即内存地址和值都一样,所以就 == ,初学就这么理解吧
      

  17.   

    public String substring(int beginIndex,
                            int endIndex)返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,一直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。 
    示例:  "hamburger".substring(4, 8) returns "urge"
     "smiles".substring(1, 5) returns "mile"
     
    参数:
    beginIndex - 开始处的索引(包括)。
    endIndex - 结束处的索引(不包括)。 
    返回:
    指定的子字符串。 
    抛出: 
    IndexOutOfBoundsException - 如果 beginIndex 为负,或 endIndex 大于此 String 对象的长度,或 beginIndex 大于 endIndex。所以你print的语句自然就是正确的 打印出来的就是正确的
      

  18.   

    结果为true的原因是return ((beginIndex == 0) && (endIndex == count)) ? this : new String(offset + beginIndex, endIndex - beginIndex, value);
    不是因为String的值相同,==在比较两个对象的时候比较的是hashCode码。
    String ss = new String("abc");
    System.out.println(ss.substring(0, 2)==ss.substring(0, 2));
    结果为false。
    因为已经不是同一个对象了,new String(offset + beginIndex, endIndex - beginIndex, value);
      

  19.   

    肯定是true,因为你aa.substring(0,2)中的2已经查过了他的最大的下标了,所以默认去到最后,而且你用aa和aa.subString比较,都是对对象本身的操作无论在内存地址还是值都是相同的
      

  20.   

    很清楚
    只有在begin=0,endindex等于0的情况下是true
      

  21.   

    这个当然就是true了,看一下API就知道了
      

  22.   

    return ((beginIndex == 0) && (endIndex == count)) ? this : 
            new String(offset + beginIndex, endIndex - beginIndex, value); 
    return this了
      

  23.   

    public String substring(int beginIndex, int endIndex) { 
    if (beginIndex < 0) { 
        throw new StringIndexOutOfBoundsException(beginIndex); 

    if (endIndex > count) { 
        throw new StringIndexOutOfBoundsException(endIndex); 

    if (beginIndex > endIndex) { 
        throw new StringIndexOutOfBoundsException(endIndex - beginIndex); 

    return ((beginIndex == 0) && (endIndex == count)) ? this : 
        new String(offset + beginIndex, endIndex - beginIndex, value); 
        } 在这种情况下直接返回this,当然是true,不懂得问题可以参看源代码
      

  24.   

    完全赞同,==比较的就是内存地址(对对象来说),由于aa.subString(0,2)和aa值相等,都指向的"字符串池里面的常量",所以内存也是相等的.
      

  25.   

    true
    substring(0,2)取0,1两位
      

  26.   

    原来substring返回Stirng本身的时候没有new一个新的String对象,学习了
      

  27.   

    当然是true啦,如果是>2的就会报错 java.lang.StringIndexOutOfBoundsException: 
      

  28.   

    ==是判断两个变量或实例是不是指向同一个内存空间
    equals是判断两个变量或实例所指向的内存空间的值是不是相同