==和equals的理解
1 package testEquals; 2 /** 3 * 测试By AsceticJ 4 * 本类是用来测试“==”和equals()方法 5 * 结果如下: 6 * 1、== 和equals方法默认一只,均表示比较的是对象本身 7 * 2、基本数据类型的包装类和String类的equals()的方法已经被默认重写了 表示比较对象的内容 8 * 3、String 类的"==",在不是用new关键字时,比较的其实还是对象,只是他们指向的是同一块地址,在使用new关键字时正常 比较的是对象本身; 9 */ 10 public class Equals { 11 public static void main(String[] args) { 12 //前面了瞅一眼就行了,布尔值的第二种构造方式可以了解一下 13 //通过基本数据类型的包装类获取对象格式:基本数据类型的包装类类名 对象名 = new 基本数据类型包装类的构造方法 14 Character ch = new Character('a'); //Character直接继承自 Object类 只有这种构造方法; 等价于Char ch = 'a'; 15 Character ch1 = new Character('a'); 16 17 Byte bt = new Byte((byte) 2); //Byte 继承自Number类, 有前面两种构造方法; 作用等价于Byte bt =2; 后面的一样 18 Byte bts = new Byte("2"); 19 20 Short sh = new Short((short) 10); //Short 继承自Number类, 有前面两种构造方法; 21 Short shs = new Short("10"); 22 23 Integer it = new Integer(1000); //Integer 继承自Number类, 有前面2种构造方法; 24 Integer its = new Integer("1000"); 25 26 Long lg = new Long(10000L); //Long 继承自Number类, 有前面2种构造方法; 27 Long lgs = new Long("10000"); 28 29 Float fl = new Float(1.23f); //Float 继承自Number类, 有前面3种构造方法; 30 Float fld = new Float(1.23); //这个参数里面没有后面没有加F,表示数据类型为默认的Double 31 Float fls = new Float("1.23"); 32 33 Double db = new Double(1.234); //Double 继承自Number类, 有前面2种构造方法; 34 Double dbs = new Double("1.234"); 35 36 Boolean bl = new Boolean(true); //Boolean直接继承自 Object类 只有这种构造方法; 37 Boolean bls = new Boolean("true"); //如果 String 参数不为 null 且在忽略大小写时等于 "true",则分配一个表示 true 值的 Boolean 对象; 38 39 // 开始测试基本数据类型的==和equals()方法。 40 System.out.print("Character:"); 41 System.out.println(ch == ch1); 42 System.out.println(ch.equals(ch1)); 43 System.out.print("Byte:"); 44 System.out.println(bt==bts); 45 System.out.println(bt.equals(bts)); 46 System.out.print("Short:"); 47 System.out.println(sh==shs); 48 System.out.println(sh.equals(shs)); 49 System.out.print("Integer:"); 50 System.out.println(it==its); 51 System.out.println(it.equals(its)); 52 System.out.print("Long:"); 53 System.out.println(lg==lgs); 54 System.out.println(lg.equals(lgs)); 55 System.out.print("Float:"); 56 System.out.println(fl==fls); 57 System.out.println(fl.equals(fls)); 58 System.out.print("Double:"); 59 System.out.println(db==dbs); 60 System.out.println(db.equals(dbs)); 61 System.out.print("Boolean:"); 62 System.out.println(bl==bls); 63 System.out.println(bl.equals(bls)); 64 // 结果输出均为:false和true 说明基本数据类型的包装类的equals()方法和"=="不同,equals比较的是对象的“内容”,而等号比较的是对象的实例 65 66 // 下面测试String类和我们创造的类 67 String str = "abc"; 68 String str1 = "abc"; 69 System.out.print("String1:"); 70 System.out.println(str==str1); 71 System.out.println(str.equals(str1)); 72 73 String str2 = new String("abc"); 74 String str3 = new String("abc"); 75 System.out.print("String2:"); 76 System.out.println(str2==str3); 77 System.out.println(str2.equals(str3)); 78 // 说明 :返回结果为true true 和false true, 79 // 说明当String类型不用new关键字进行初始化的时候,==和equals相同,都是比较对象的内容 80 // 当String类型 通过new关键字初始化,== 比较的是对象,equals比较的是内容 81 82 System.out.println("我们自己定义的类,绝对没有重写Object的任何方法"); 83 ForTest ft = new ForTest(2, "2"); 84 ForTest ft1 = new ForTest(2, "2"); 85 System.err.println("自定义类:"); 86 System.out.println(ft==ft1); 87 System.out.println(fl.equals(ft1)); 88 // 说明:返回false和false 说明默认的==和equals()方法相同,比较的都是ForTest类的对象本身 89 } 90 } 91 92 class ForTest 93 { 94 int a; 95 String strs; 96 public ForTest(int a,String b) { 97 this.a = a; 98 this.strs = b; 99 } 100 }