zoukankan      html  css  js  c++  java
  • Java中的equals和==的差别 以及Java中等价性和同一性的讨论

    ==对基本数据类型比较的是值,对引用类型比较的是地址

    equals()比较的是对象的数据的引用

    等价性原理:

    • 自反性    x.equals(x)为true
    • 对称性    x.equals(y) 为true时,y.equals(x) 也为true
    • 传递性    若x.equals(y) 为true , y.equals(z) 为true, 则x.equals(z) 为true
    • 一致性   在未修改x,y的值的情况下x.equals(y)始终是相同的值
    • 非空性   x.equals(null) 总是 false
    public class Test {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            int n=3;
            int m=3;
            String str1 = "hello";//"hello"存在于常量池,str1,str2都指向该常量池
            String str2 = "hello";
    
          
            System.out.println("基本数据类型的 ==  判定:"+(n==m));  //判断数据
            System.out.println("引用数据类型的 == 判定"+(str1==str2));//判断地址
    
    
    
            String str3 = new String("hello");//"hello"存在于堆的对象内存区,非常量区
            String str4 = new String("hello");
           
            System.out.println("对象的 == 判定:"+(str3 == str4)); //判断地址
            System.out.println("对象的 equals() 判定"+str3.equals(str4));//判断数据
    
            str4 = str3;
            System.out.println("对象赋值后的 == 判定"+( str3 == str4 ) );
            System.out.println("对象的 equals() 判定"+str3.equals(str4));
        }
    
    }

    结果如下:

    基本数据类型的 ==  判定:true
    引用数据类型的 == 判定true
    对象的 == 判定:false
    对象的 equals() 判定true
    对象赋值后的 == 判定true
    对象的 equals() 判定true
    
    public class practice5 {
    
    	public static void main(String[] args) {
    		Dog spot = new Dog();
    		Dog scruffy = new Dog();
    		Dog spot1 = new Dog();
    		Dog spot2 = new Dog();
    		
    		spot.setNameString("spot");
    	   scruffy.setNameString("scruffy");
    	   spot.setSayString("Ruff!");
    	   scruffy.setSayString("Wurf!");
    	   spot2.setNameString("spot");
    	   spot2.setSayString("Ruff!");
    	   
    	   spot1=spot;
    	   System.out.println(spot.getNameString()+"	"+spot.getSayString());
    	   System.out.println(scruffy.getNameString()+"	"+scruffy.getSayString());
    	   
    	   System.out.println("spot1==spot	"+(spot1==spot));
    	   System.out.println("spot1.equals(spot)	"+spot1.equals(spot));
    	   
    	   System.out.println("spot2==spot	"+(spot2==spot));
    	   System.out.println("spot2.equals(spot)	"+spot2.equals(spot));
    	   
    	   System.out.println("spot2.nameString==spot.nameString	"+(spot2.nameString==spot.nameString));
    	   System.out.println("spot2.nameString.equals(spot.nameString)	"+spot2.nameString.equals(spot.nameString));
    	   
    	   
    	}
    
    }
    class Dog{
    	
    	String nameString;
    	String sayString;
    	
    	public String getNameString() {
    		return nameString;
    	}
    	public void setNameString(String nameString) {
    		this.nameString = nameString;
    	}
    	public String getSayString() {
    		return sayString;
    	}
    	public void setSayString(String sayString) {
    		this.sayString = sayString;
    	}
    	
    	
    	
    }
    

    结果

    spot	Ruff!
    scruffy	Wurf!
    spot1==spot	true
    spot1.equals(spot)	true
    spot2==spot	false
    spot2.equals(spot)	false
    spot2.nameString==spot.nameString	true
    spot2.nameString.equals(spot.nameString)	true
  • 相关阅读:
    缓存算法之LRU与LFU
    银行家算法
    死锁,死锁的四个必要条件以及处理策略
    找出无序数组中位数的方法
    HTTP状态码
    进程调度算法
    宽字节wchar_t和窄字节char的相互转换
    胜天半子
    ? 题目 一道超难的奥数题,猜生日. A告诉B他生日的月份,告诉C他生日的日期 B说:“如果我不知道A的生日,那C肯定也不知道." C说:”本来我不知道,现在我知道了.“ B说:”哦,那我也知道了.
    有对夫妇生有一男一女,一天晚上,成员中的一个杀了另一个,剩下2个成员,1个是帮凶1个是目击者
  • 原文地址:https://www.cnblogs.com/jeasion/p/10758374.html
Copyright © 2011-2022 走看看