基本类型和基本封装类型以及==和equals的比较。

一:基本类型和基本封装类型

    1.基本型和基本型封装型进行“==”运算符的比较,封装型将会自动拆箱变为基本型后再进行比较。 如Integer和int; Character和char。另外,基本型是不能赋值为null的。封装型可以。

    另外,对于封装型和基本型比较最好使用(封装型).equals(基本型),同时保证封装型不为空。这时候,先会进行自动装箱,基本型转换为其封装类型,若类型不同返回false。
    2.两个基本型的封装型进行equals()比较,首先equals()会比较类型,如果类型相同,则继续比较值,如果值也相同,返回true

二: ==和equals

     1.两个基本类型的只能用 ==

     2.至少有一个封装型的建议使用.equals。

     3. 用==对基本型和封装性比较必须保证封装型不为null。如果为null则不能转化为基本型就会报错。

     4.用==来比较两个封装行的话,比较的是地址。

public class CompareCharacter {
    public static void main(String args[]) {
        System.out.println("Character c1 = 'Y';\nCharacter c2 = 'Y';\nCharacter c3 = new Character('Y');\nCharacter c4 = new Character('Y');\nSystem.out.println(c1 == c2);\nSystem.out.println(c3 == c4);");
        Character c1 = 'Y';
        Character c2 = 'Y';
        Character c3 = new Character('Y');
        Character c4 = new Character('Y');
        System.out.println(c1 == c2);
        System.out.println(c3 == c4);
        
        System.out.println("String s1 = \"Y\";\nString s2 = \"Y\";\nString s3 = new String(\"Y\");\nString s4 = new String(\"Y\");\nSystem.out.println(s1 == s2);\nSystem.out.println(s3 == s4);");
        String s1 = "Y";
        String s2 = "Y";
        String s3 = new String("Y");
        String s4 = new String("Y");
        System.out.println(s1 == s2);
        System.out.println(s3 == s4);
        
        System.out.println("Integer i1 = 257;\n" + 
                "Integer i2 = 257;\n" + 
                "Integer i3 = new Integer(257);\n" + 
                "Integer i4 = new Integer(257);\n" + 
                "System.out.println(i1 == i2);\n" + 
                "System.out.println(i3 == i4);");
        Integer i1 = 257;
        Integer i2 = 257;
        Integer i3 = new Integer(257);
        Integer i4 = new Integer(257);
        System.out.println(i1 == i2);
        System.out.println(i3 == i4);
        
        System.out.println("Integer ii1 = 57;\r\n" + 
                "Integer ii2 = 57;\r\n" + 
                "Integer ii3 = new Integer(57);\r\n" + 
                "Integer ii4 = new Integer(57);\r\n" + 
                "System.out.println(ii1 == ii2);\r\n" + 
                "System.out.println(ii3 == ii4);");
        Integer ii1 = 57;
        Integer ii2 = 57;
        Integer ii3 = new Integer(57);
        Integer ii4 = new Integer(57);
        System.out.println(ii1 == ii2);
        System.out.println(ii3 == ii4);
    }
}
//    public static void main(String[] args) {
//
//        int a=257;
//        Integer b=257;
//        Integer c=257;
//        Integer d=new Integer(a);
//        Integer d1=new Integer(a);
//
//        Integer b2=57;
//        Integer c2=57;
//
//        System.out.println(a==b);//1
//        System.out.println(b==c);//2
//        System.out.println(b2==c2);//3
//        System.out.println(a==d);//4
//        System.out.println(b==d);//5
//        System.out.println(d==d1);//6
//
//        /**以上输出结果:
//         * true
//           false
//           true
//           true
//           false
//           false
//
//         */
//
//        System.out.println();
//
//        //System.out.println(a.equals(b));  编译出错,基本型不能调用equals()
//        System.out.println(b.equals(257.0));  //7
//        System.out.println(b.equals(c));       //8
//        System.out.println(b.equals(a));      //9
//        System.out.println(b.equals(d));    //10
//
//        /**
//         * 以上输出结果:
//        false
//        true
//        true
//        true
//
//         */
//        }
//        }

输出结果如下:

Character c1 = 'Y';
Character c2 = 'Y';
Character c3 = new Character('Y');
Character c4 = new Character('Y');
System.out.println(c1 == c2);
System.out.println(c3 == c4);
true
false
String s1 = "Y";
String s2 = "Y";
String s3 = new String("Y");
String s4 = new String("Y");
System.out.println(s1 == s2);
System.out.println(s3 == s4);
true
false
Integer i1 = 257;
Integer i2 = 257;
Integer i3 = new Integer(257);
Integer i4 = new Integer(257);
System.out.println(i1 == i2);
System.out.println(i3 == i4);
false
false
Integer ii1 = 57;
Integer ii2 = 57;
Integer ii3 = new Integer(57);
Integer ii4 = new Integer(57);
System.out.println(ii1 == ii2);
System.out.println(ii3 == ii4);
true
false

 

posted @ 2018-07-27 13:47  董永辉Bruno  阅读(997)  评论(0)    收藏  举报