zoukankan      html  css  js  c++  java
  • 【Java概念每日一题002】== 和 equals的区别

    == 解读

    对于基本类型和引用类型 == 的作用效果是不同的。

    基本类型:比较值是否相同;

    引用类型:比较引用是否相同。

    代码示例:

    1 String x = "string";
    2 String y = "string";
    3 String z = new String("string");
    4 System.out.println(x == y); //true
    5 System.out.println(x == z); //flase
    6 System.out.println(x.equals(y)); //true
    7 System.out.println(x.equals(z)); //true

    根据对基本类型(int,short等)以及引用类型(String等)的理解,x 和 y指向的是同一个引用,所以 == 是true,而new String()方法new了新的内存空间,所以 == 结果为false,而equals比较值,所以返回为true

    equals解读

    equals的本质是 == ,但是String和Integer(Int)等重写了equals方法,把equals方法变成了值的比较。

    默认情况下equals方法比较值

     1 class Cat(){
     2     public Cat(String name){
     3         this.name = name;
     4     }
     5     
     6     private String name;
     7 
     8     public String getName(){
     9         return name;
    10     }
    11 
    12     public void setName(String name){
    13         this.name = name;
    14     }
    15 }
    16 
    17 
    18 Cat c1= new Cat("提莫”)
    19 Cat c2= new Cat("提莫”)
    20 System.out.println(c1.equals(c2)): // false

    equals源码如下:

    public boolean equals(object obj){
        return (this == obj);
    }

    equals本质上就是==那,两个相同值的 String对象,为什么返回的是true?代码如下

    String s1= new String("阿莫");
    String s2= new String("阿莫");
    System.out.printin(s1.equals(s2));//true

    String的equals方法如下:

    public boolean equals(object anObject){
         if(this == anObject){
             return true;
         }
     
         if (anObject instanceof String){
             String anotherString = (String)anObject;
             int n = value.length;
             if (n == anotherString.value.length){
                     char v1[] = value;
                     char v2[] = anotherString.value;
                     int i=0;
                     while(n-- != 0){
                             if (v1[i] != v2[i])
                                     return false;
                             i++;
                     }
                 return true;
                 }
             }
         return false;
    }
     1 public boolean equals(object anObject){
     2     if(this == anObject){
     3         return true;
     4     }
     5 
     6     if (anObject instanceof String){
     7         (String anotherString = (String)anObject;
     8         int n = value.length;
     9         if (n == anotherString.value.length){
    10                 char v1[] = value;
    11                 char v2[] = anotherString.value;
    12                 int i=0;
    13                 while(n-- != 0){
    14                         if (v1[i] != v2[i]){
    15                                 return false;
    16                         i++;
    17                 }
    18             return true;
    19             }
    20         }
    21     return false;
    22 }

    由此可见,String类型的equals方法被重写,将引用比较改成了值比较。

    总结:对于基本类型来说是值比较,对于引用类型来说是引用比较;而equals默认情况下是引用比较,只是很多类重写了equals方法,比如 String,Integer等把它变成了值比较,所以一情况下equals比较的是值是否相等。

  • 相关阅读:
    RE
    【LeetCode】198. House Robber
    【LeetCode】053. Maximum Subarray
    【LeetCode】152. Maximum Product Subarray
    【LeetCode】238.Product of Array Except Self
    【LeetCode】042 Trapping Rain Water
    【LeetCode】011 Container With Most Water
    【LeetCode】004. Median of Two Sorted Arrays
    【LeetCode】454 4Sum II
    【LeetCode】259 3Sum Smaller
  • 原文地址:https://www.cnblogs.com/Neil-J/p/13547170.html
Copyright © 2011-2022 走看看