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");
}
}
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 assignmentObject[] arr1 = {inarr1};
, you are making an array of an array sinceinarr1
is anint
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.