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

165 views Asked by At

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"); 
    } 
} 
3

There are 3 answers

0
Arun B On

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.

0
Lajos Arpad On

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/

0
ControlAltDel On

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