深度比较是什么意思,成为一个对象类的对象意味着什么?

huangapple go评论78阅读模式
英文:

Hey what is deep Comparison and what does it mean to be an object of object class?

问题

这是我在Geeks for Geeks上找到的代码,我对"Object"类和"deep comparison"深度比较有些困惑,我在Java方面还很新,正试图尽可能深入地了解许多事情。请不要因为我这些愚蠢的疑问而感到烦恼。

import java.util.Arrays;
class Test 
{ 
    public static void main (String[] args)  
    { 
        // inarr1和inarr2具有相同的值
        int inarr1[] = {1, 2, 3}; 
        int inarr2[] = {1, 2, 3};    
        Object[] arr1 = {inarr1};  // arr1只包含一个元素 
        Object[] arr2 = {inarr2};  // arr2也只包含一个元素 
        if (Arrays.equals(arr1, arr2)) 
            System.out.println("相同"); 
        else
            System.out.println("不相同"); 
    } 
}
英文:

This is a code I found on geeks for geeks I am confused about class Object and what is deep comparison I am quite new to Java and I am trying to get to know as much thing in depth as I can. Please don't get irritated by my silly doubts

import java.util.Arrays;
class Test 
{ 
    public static void main (String[] args)  
    { 
        // inarr1 and inarr2 have same values 
        int inarr1[] = {1, 2, 3}; 
        int inarr2[] = {1, 2, 3};    
        Object[] arr1 = {inarr1};  // arr1 contains only one element 
        Object[] arr2 = {inarr2};  // arr2 also contains only one element 
        if (Arrays.equals(arr1, arr2)) 
            System.out.println("Same"); 
        else
            System.out.println("Not same"); 
    } 
} 

答案1

得分: 3

在你添加 Arrays.deepEquals 比较的情况下进行结果比较:

如果 (Arrays.equals(arr1, arr2))
System.out.println("相同");
else
System.out.println("不相同");
如果 (Arrays.deepEquals(arr1, arr2))
System.out.println("相同");
else
System.out.println("不相同");

在第二种情况下应该是 "相同"。这是因为虽然 Object[] 和 int[] 本身是不同的对象,它们的 int 内容是相同的。

英文:

Compare the results if you add a Arrays.deepEquals comparison

    if (Arrays.equals(arr1, arr2)) 
        System.out.println("Same"); 
    else
        System.out.println("Not same"); 
    if (Arrays.deepEquals(arr1, arr2)) 
        System.out.println("Same"); 
    else
        System.out.println("Not same"); 

In the second case it should be "Same". This is because while the Object[]'s and int[]'s themselves are different objects, their int contents are the same

答案2

得分: 1

Arrays.equal()适用于比较单维数组。它会遍历每个值并检查它们是否相等。如果我们在多维数组上使用它,该方法将失效。

Arrays.deepEquals()允许我们通过比较每对元素(在2D数组情况下)来检查多维数组是否相等,以查看它们是否相等。当你进行这样的赋值Object[] arr1 = {inarr1};时,你实际上创建了一个数组的数组,因为inarr1是一个int数组。

至于第二个问题,Object类是Java中所有类的父类,描述了Java类应包含/遵循的常见方法/行为。换句话说,所有类都必须并且确实是从Object类继承的。大多数集成开发环境(IDE)中都隐含了这一点。

因此,创建Object类的对象就是简单地创建一个Object类型的对象。你可以实例化为任何具体的子类。因此,它相当不明确,很少被使用。在我个人看来,只有在覆盖Comparator的compare方法时才会见到有人这样做。

英文:

Arrays.equal() works for comparing single dimension arrays. It'll go through every value and check to see if they're equal. This method fails if we use it on multi-dimensional arrays.

Arrays.deepEquals() allows us to check whether multi-dimensional arrays are equal by comparing every pair (in a 2D array case) to one another to see if they are equal. When you make this assignment Object[] arr1 = {inarr1};, you are making an array of an array since inarr1 is an int array.

To the second question, the object class is the parent class of all classes in Java and describes common methods/behaviors classes in Java should contain/follow. In other words, all classes must and do extend from the Object class. It's implied in most IDE's.

So, to create an object of the Object class is to simply create an object of type Object. You can instantiate to any concrete child class. As such, it's rather undescriptive and is rarely used. I personally have only seen someone do this when overriding a Comparator's compare method.

答案3

得分: 1

Object是一个class,就像任何其他class一样,但不同之处在于每个class都是它的后代。这意味着您可以将任何对象转换为Object。这就是Object对象的含义。

关于深度比较:

> 在Java中有两种类型的对象比较 - 浅比较和深比较。
>
> 当您使用'=='运算符时,您正在比较引用是否相等。这意味着您正在比较对象的内存地址。这被称为浅比较。
>
> 当您使用.equals()方法时,您正在比较对象的实际值是否相等。这意味着您正在检查“两个对象的相应实例变量是否具有相同的值”。

来源:https://blog.webnersolutions.com/java-shallow-and-deep-comparison-of-objects/

英文:

Object is a class, just like any other class, but with the difference that every class is its descendant. Which means that you can convert any object to an Object. This is what an object of Object means.

As about deep comparison:

> There are two types of object comparison in java – shallow and deep.
>
> when you use ‘==’ operator, you are comparing the references for
> equality. It means you are comparing memory addresses of the objects.
> It is called shallow comparison.
>
> When you use .equals() you are comparing the object values themselves
> for equality. It means you are checking ‘do both objects have same
> value for corresponding instance variables or not ‘.

Source: https://blog.webnersolutions.com/java-shallow-and-deep-comparison-of-objects/

huangapple
  • 本文由 发表于 2020年9月3日 02:04:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63711225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定