String == 与equals 区别

来源:互联网 发布:公司注销,淘宝企业店铺 编辑:程序博客网 时间:2024/06/17 13:39
public static void main(String[] args) {

String str1="ABCD";
String str2="AB"+new String("CD");
String str3="A"+"B"+"C"+"D";
String str4=new String("ABCD");

System.out.println("1.str1==str2 :"+(str1==str2));
System.out.println("2.str1==str3 :"+(str1==str3));
System.out.println("3.str1==str4 :"+(str1==str4));
System.out.println("4.str1.equals(str2) :"+str1.equals(str2));
System.out.println("5.str1.equals(str3) :"+str1.equals(str3));
System.out.println("6.str1.equals(str4) :"+str1.equals(str4));

     System.out.println("###################################");
     short s1=1;
      s1 =(short) (s1+1);
      short s2=1;
            s2+=1;
         System.out.println("7. x1: "+s1);
       System.out.println("8. x2: "+s2);
         System.out.println("#####################################");
       byte b1=123;
         byte b2='a';             
     System.out.println(" 9.byte b1: "+b1);
     System.out.println(" 10.byte b2: "+b2);
     System.out.println(" ========================= ");
     System.out.println(" 11.2<<3 :"+(2<<3));//记住是左移动是 << 
     System.out.println(" 12. 8>>2 :"+(8>>2));
     System.out.println(" 13.-20>>>2 :"+(-20>>>2));
     System.out.println(" 14.20>>>2 :"+(20>>>2));
}

---------------------------------------------------------------------------------------------------------

1.str1==str2 :false
2.str1==str3 :true
3.str1==str4 :false
4.str1.equals(str2) :true
5.str1.equals(str3) :true
6.str1.equals(str4) :true
###################################
7. x1: 2
8. x2: 2
#####################################
9.byte b1: 123
10.byte b2: 97
 ========================= 
11.2<<3 :16
12. 8>>2 :2
13.-20>>>2 :1073741819
14.20>>>2 :5

0 0