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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 209920266 中级黑马   /  2015-1-11 22:05  /  676 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class StringTest {  
    public static void main(String[] args) {  
        String a=new String("hello lol");  
        String b=new String("hello lol");  
        System.out.println(a==b);//false  
        System.out.println(a.equals(b));//true  
         
        String c="hello league of legends";  
        String d="hello league of legends";  
        System.out.println(c==d);//true  
        System.out.println(c.equals(d));//true  
    }  
  
}  

“==”操作符专门用来比较两个变量是否相等。

equals用来比较两个独立对象的内容是否相等。

String a=new String("hello lol");

String b=new String("hello lol");

a,b两个变量分别指向两个不同的String对象,但他们的内容相同。

String c="hello league of legends";

String d="hello league of legends";

字符串直接用这种方式赋值,java会把"hello league of legends"放在同一个内存空间,即在数据缓冲局中c ,d变量所指向的是同一个String对象。


评分

参与人数 1技术分 +2 收起 理由
lwj123 + 2 a,b的话equals为true,==为false,c,dequal.

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马