黑马程序员技术交流社区

标题: System.out.println(strValue)的输出结果不是应该是DEFG123吗?求解 [打印本页]

作者: 梁秋瑜    时间: 2013-5-25 22:07
标题: System.out.println(strValue)的输出结果不是应该是DEFG123吗?求解
public class Foo {
        public static void main(String[] args) {
                String strValue = "ABCDEFG";
                strValue.substring(3);
                strValue.concat("123");
                System.out.println(strValue);       
                String value = new String ("ABCDEFG");
                System.out.println(strValue == value);
        }
}

作者: 武志红    时间: 2013-5-25 22:19
strValue.substring(3);只是截取后三位,并没有修改 应写为strValue =strValue.substring(3);
下面的语句同理
作者: 棉/mg花/x糖    时间: 2013-5-25 22:21
本帖最后由 棉/mg花/x糖 于 2013-5-25 22:25 编辑

楼主,你好,我来帮你解答:
strValue.substring(3);
strValue.concat("123");

操作完成后,得到的是一个新字符串,必须存储起来,这里并没有存储,也没存到strValue中。
因此相当于无用操作,即可以把这两行当成被注释了的语句。

故最后结果一定是false。

因为strValue是编译时就加载进内存的字符串,而value是在运行是才创建的字符串。
二者引用不一样。

作者: 王靖远    时间: 2013-5-26 00:08
你没有接收处理过的字符串。strValue = strValue.substring(3);
strValue = strValue.concat("123");




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2