首页 > 代码库 > 变量的比较

变量的比较

/*变量的比较
 * 1.==用于比较两个基础数据类型时只比较字节组合
 *   ==用于比较对象类型时只比较两个对象是否指向同一对象
 * 2.equals()方法用于比较两个对象的实际意义是否相等*/

 1 class XDog{ 2     private int size; 3     private String name; 4     XDog(String name,int size){ 5         this.name = name; 6         this.size = size; 7     } 8     XDog dog1 = new XDog("旺财",100); 9     XDog dog2 = new XDog("大王",100);10     public void bark(){11         if(dog1.equals(dog2)){12             System.out.println("dog1与dog2意义相等");13         }14     }15     //dog1.equals(dog2)true;16     17     18 }19 public class Bianliangbijiao {20     public static void main(String[]  args){    21           int a = 3;22           byte b = 3;23           if(a == b){24               System.out.println("a与b相等");}25           26           27           28           29           Bianliangbijiao foo1 = new Bianliangbijiao();30           Bianliangbijiao foo2 = new Bianliangbijiao();31           Bianliangbijiao foo3 =foo1;32           33           if(foo1 == foo2){34               System.out.println("foo1和foo2是指向同一个对象的!");35           }else{36               System.out.println("foo1和foo2是指向不同的对象");37           }38           39           if(foo1.equals(foo2)){40               System.out.println("foo1和foo2是真的相同!");41           }else{42               System.out.println("foo1和foo2是真的不相同");43           }44           if(foo1 == foo3){45               System.out.println("foo1和foo3是指向同一个对象的!");46           }else{47               System.out.println("foo1和foo3是指向不同的对象");48           }49       }50 }

技术分享

变量的比较