A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 2528870651 高级黑马   /  2014-4-3 14:19  /  972 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 2528870651 于 2014-4-4 18:45 编辑
  1. class Test2
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 String str1 = "Hello";
  6.                 String str2 = "World";
  7.                 String str3 = "HelloWorld";
  8.                 System.out.println(str3 == str1+ str2);                                //false
  9.                 System.out.println(str3 == str1 + "World");                         //false
  10.                 System.out.println(str3 == "Hello"+"World");                 //true
  11.                 System.out.println(str1 + "World" =="Hello"+"World");//false
  12.                 //不都是HelloWorld吗???为什么结果会不同呢
  13.         }
  14. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
itpower + 1

查看全部评分

7 个回复

倒序浏览
我是这么理解的
只要是用一个引用 跟一个字符串 进行预算 必定是一个新的字符串
只有两个都是字符串相加才会有一个在内存中寻找是否有这个字符串存在,有就是同一个
回复 使用道具 举报 1 0
==判断规则:两边不但对象相等,而且存储对象的内存地址也得相同。
比如说,str1=“abc”,就是创建了一个abc对象,要存储他必须申请个内存地址,假如说这个内存地址是1,str1=“abc”就是把存在内存地址1的abc赋给了str1。在java中有个机制,就是比如,str1创建时,发现内存中没有“abc”,所以就创建个“abc”并赋给他内存地址1,而当创建str2时,发现内存中存在了“abc”,当这个对象存在时,就不用创建“abc”了,就直接把“abc”映射给了str2,所有str1和str2的“abc“是同一个”abc“,无论是对象,也就是字符串”abc“,还是地址,就是内存地址1,都是一样的,所以就是ture

评分

参与人数 1技术分 +1 收起 理由
itpower + 1

查看全部评分

回复 使用道具 举报
比如说str1的“hello”是存在内存1,str2的“world”是存在内存2,str3是存在内存3的“helloworld”所以说str1+str2是内存1的hello和内存2的world组成的字符串,而str3是内存3的“helloworld”,字符串是一样的,内存地址是不一样的
回复 使用道具 举报
肖涵 发表于 2014-4-3 16:04
比如说str1的“hello”是存在内存1,str2的“world”是存在内存2,str3是存在内存3的“helloworld”所以说s ...

你是想说str1+str2相加组成一个新的字符串,这个字符串再存储到内存中,得到一个新的字符串对象(设为str4),这个str4和str3的内存地址不一样??????
可是按照你说的那个什么缓冲池机制是吧,在创建str4的时候不是先要去检查缓冲池里面有没有“HelloWorld”这个字符串对象吗? 没有的话才创建,可是明明有了一个“HelloWorld”了啊 ,
它应该不会再创建一个str4了啊 ,他会直接使用str3的“HelloWorld”呀?????
回复 使用道具 举报
楼主看我的代码有注颜色的地方你就懂了。
class Test2
{
        public static void main(String[] args)
        {
                String str1 = "Hello";   完整写法:String str1 = new String("Hello");
                String str2 = "World";  完整写法:String str2 = new String("World");
                String str3 = "HelloWorld"; 完整写法:String str3 = new String("HelloWorld");

                System.out.println(str3 == str1+ str2);                                //false
                System.out.println(str3 == str1 + "World");                         //false
                System.out.println(str3 == "Hello"+"World");                 //true
                System.out.println(str1 + "World" =="Hello"+"World");//false
                //不都是HelloWorld吗???为什么结果会不同呢
        }
}
对象变量其实是一个引用,它们的值是指向对象所在的内存地址,而不是对象本身。都使用了new操作符,意味着将在内存中产生的内容,既然是“三个”,它们自然位于不同的内存地址。srt1、str2和str3的值其实是不同的内存地址的值。==成立条件是内存地址必须相等。
str3 == str1+ str2明显是false;
str3 == str1 + "World"  false;
str3 == "Hello"+"World" true;
str1 + "World" =="Hello"+"World" false;
回复 使用道具 举报
LYG 初级黑马 2014-4-3 18:57:59
7#
假设: 字符串1,str1 = "Hello";内存地址为0x0012
          新定义的字符串2,str2 = "World";内存地址肯定与前面的不同,要不然就覆盖了,假设为0x0032
          字符串3,str3 = "HelloWorld";设内存地址为0x0054
           因为定义好的字符串内存地址不一样,所以不能相加减运算和相比较。
           所以前两条 和最后一条为false。
           第三条因为“Hello”+“World”=HelloWorld,HelloWorld的值会直接映射到str3的地址,所以他们        相等
        /*      System.out.println(str3 == str1+ str2);                                //false
                System.out.println(str3 == str1 + "World");                         //false
                System.out.println(str3 == "Hello"+"World");                 //true
                System.out.println(str1 + "World" =="Hello"+"World");//false
         */
回复 使用道具 举报
肖涵 发表于 2014-4-3 15:55
==判断规则:两边不但对象相等,而且存储对象的内存地址也得相同。
比如说,str1=“abc”,就是创建了一个a ...

那这个怎么解释啊?
  1. String str1 = "hello";
  2.                 String str2 = new String("hello");
  3.                 System.out.println(str1==str2);//结果为false,怎么解释啊?
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马