为什么是true呀? String aa = new String("aa"); System.out.println(aa==aa.substring(0,2)); 是true还是false啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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,不懂得问题可以参看源代码 substring()方法是子字符串从指定的 beginIndex 处开始,一直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。如: "hamburger".substring(4, 8) returns "urge" "smiles".substring(1, 5) returns "mile" 肯定是true了你用aa和aa.subString比较,都是对对象本身的操作无论在内存地址还是值都是相同的 对了,你要明白substring的用途,多点看看api ==怎么比的是值了?最关键的原因是在((beginIndex == 0) && (endIndex == count))这种情况下,直接返回当前的字符串本身,其他的情况才会new一个String.jdk的源码已经很清楚了 结果是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); 当然是true了。==比较引用时是比较的是引用是否相同。 [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] 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); } [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] 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); } 原来substring返回Stirng本身的时候没有new一个新的String对象,学习了 String的subString方法生成的String与原来的String使用的是同一个char数组的引用 比较的还是内存地址,不光是进行值的比较.他们占的同一块内存地址,所以返回true 楼主是不是被类似于下面的代码干扰了? 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.明白了吗? str.substring(int index,int i)方法 它是在 str 这块内存地址上操作的,所以如果值也一样,即内存地址和值都一样,所以就 == ,初学就这么理解吧 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的语句自然就是正确的 打印出来的就是正确的 结果为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); 肯定是true,因为你aa.substring(0,2)中的2已经查过了他的最大的下标了,所以默认去到最后,而且你用aa和aa.subString比较,都是对对象本身的操作无论在内存地址还是值都是相同的 很清楚只有在begin=0,endindex等于0的情况下是true 这个当然就是true了,看一下API就知道了 return ((beginIndex == 0) && (endIndex == count)) ? this : new String(offset + beginIndex, endIndex - beginIndex, value); return this了 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,不懂得问题可以参看源代码 完全赞同,==比较的就是内存地址(对对象来说),由于aa.subString(0,2)和aa值相等,都指向的"字符串池里面的常量",所以内存也是相等的. truesubstring(0,2)取0,1两位 原来substring返回Stirng本身的时候没有new一个新的String对象,学习了 当然是true啦,如果是>2的就会报错 java.lang.StringIndexOutOfBoundsException: ==是判断两个变量或实例是不是指向同一个内存空间equals是判断两个变量或实例所指向的内存空间的值是不是相同 Eclipse 中 能不能 根据cvs中 文件的提交时间来进行查询? Thread小問題 TfIdf算法实现 in java IP 正则表达式问题 射日 求算法一个 table 里面如何加JButton ,并且button可以响应事件? 应聘java程序员一般会被问到那些问题?请指点! 怎样用java实现一个动态时钟,谁有源程序?? 有大连理工大学软件学院的吗??? [面试题] javascript 如何校验数字型? 在做图书管理系统中遇到的问题!
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,不懂得问题可以参看源代码
"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"
你用aa和aa.subString比较,都是对对象本身的操作
无论在内存地址还是值都是相同的
==怎么比的是值了?
最关键的原因是在((beginIndex == 0) && (endIndex == count))这种情况下,直接返回当前的字符串本身,其他的情况才会new一个String.
jdk的源码已经很清楚了
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);
//下面源代码就了解了:
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]
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);
}
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]
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);
}
原来substring返回Stirng本身的时候没有new一个新的String对象,学习了
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.
明白了吗?
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的语句自然就是正确的 打印出来的就是正确的
不是因为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);
只有在begin=0,endindex等于0的情况下是true
new String(offset + beginIndex, endIndex - beginIndex, value);
return this了
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,不懂得问题可以参看源代码
substring(0,2)取0,1两位
equals是判断两个变量或实例所指向的内存空间的值是不是相同