针对字符串是特殊的引用类型我们来看下例子:
程序代码:
结果:
不相同
相同
不相同
不相同

String st1 = new String("hello,world!"); String st2 = new String("hello,world!"); System.out.println("---------==比较结果--------"); if(st1==st2){ System.out.println("相同"); }else{ System.out.println("不相同"); } System.out.println("---------equals比较结果--------"); if(st1.equals(st2)){ System.out.println("相同"); }else{ System.out.println("不相同"); } StringBuffer sb=new StringBuffer("Hello");//真正的字符串引用类型. StringBuffer sb1=new StringBuffer("Hello"); System.out.println("---------==比较结果--------"); if(sb==sb1){ System.out.println("相同"); }else{ System.out.println("不相同"); } System.out.println("---------equals比较结果--------"); if(sb.equals(sb1)){ System.out.println("相同"); }else{ System.out.println("不相同"); }
结果:
不相同
相同
不相同
不相同

C#超级群 74862681,欢迎大家的到来!