Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 775437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:17:19+00:00 2026-05-14T19:17:19+00:00

I was just curious to know – why does Arrays.equals(double[][], double[][]) return false? when

  • 0

I was just curious to know – why does Arrays.equals(double[][], double[][]) return false? when in fact the arrays have the same number of elements and each element is the same?

For example I performed the following test.

double[][] a,  b;
int size =5;

a=new double[size][size];
b=new double[size][size];

for( int i = 0; i < size; i++ )
    for( int j = 0; j < size; j++ ) {
        a[i][j]=1.0;
        b[i][j]=1.0;
    }

if(Arrays.equals(a, b))
    System.out.println("Equal");
else
    System.out.println("Not-equal");

Returns false and prints “Not-equal”.

on the other hand, if I have something like this:

double[] a,  b;
int size =5;

a=new double[size];
b=new double[size];

for( int i = 0; i < size; i++ ){
    a[i]=1.0;
    b[i]=1.0;
} 

if(Arrays.equals(a, b))
    System.out.println("Equal");
else
    System.out.println("Not-equal");

returns true and prints “Equal”. Does the method only work with single dimensions? if so, is there something similar for multi-dimensional arrays in Java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:17:20+00:00Added an answer on May 14, 2026 at 7:17 pm

    Use deepEquals(Object[], Object[]).

    Returns true if the two specified arrays are deeply equal to one another.

    Since an int[] is an instanceof Object, an int[][] is an instanceof Object[].


    As to why Arrays.equals doesn’t “work” for two dimensional arrays, it can be explained step by step as follows:

    For arrays, equals is defined in terms of object identity

    System.out.println(
        (new int[] {1,2}).equals(new int[] {1,2})
    ); // prints "false"
    

    This is because arrays inherit their equals from their common superclass, Object.

    Often we really want value equality for arrays, of course, which is why java.util.Arrays provides the static utility method equals(int[], int[]).

    System.out.println(
        java.util.Arrays.equals(
            new int[] {1,2},
            new int[] {1,2}
        )
    ); // prints "true"
    

    Array of arrays in Java

    • An int[] is an instanceof Object
    • An int[][] is an instanceof Object[]
    • An int[][] is NOT an instanceof int[]

    Java doesn’t really have two dimensional arrays. It doesn’t even really have multidimensional arrays. Java has array of arrays.

    java.util.Arrays.equals is “shallow”

    Now consider this snippet:

    System.out.println(
        java.util.Arrays.equals(
            new int[][] {
                { 1 },
                { 2, 3 },
            },
            new int[][] {
                { 1 },
                { 2, 3 },
            }
        )
    ); // prints "false"
    

    Here are the facts:

    • Each argument is an Object[]
      • The element at index 0 is an int[] { 1 }
      • The element at index 1 is an int[] { 2, 3 }.
    • There are two Object[] instances
    • There are four int[] instances

    It should be clear from the previous point that this invokes Arrays.equals(Object[], Object[]) overload. From the API:

    Returns true if the two specified arrays of Objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. Two objects e1 and e2 are considered equal if (e1==null ? e2==null : e1.equals(e2)).

    Now it should be clear why the above snippet prints "false"; it’s because the elements of the Object[] arrays are not equal by the above definition (since an int[] has its equals defined by object identity).

    java.util.Arrays.deepEquals is “deep”

    In contrast, here’s what Arrays.deepEquals(Object[], Object[]) does:

    Returns true if the two specified arrays are deeply equal to one another. Unlike the equals(Object[],Object[]) method, this method is appropriate for use with nested arrays of arbitrary depth.

    System.out.println(
        java.util.Arrays.deepEquals(
            new int[][] {
                { 1 },
                { 2, 3 },
            },
            new int[][] {
                { 1 },
                { 2, 3 },
            }
        )
    ); // prints "true"
    

    On Arrays.toString and Arrays.deepToString

    It’s worth noting the analogy between these two methods and what we’ve discussed so far with regards to nested arrays.

    System.out.println(
        java.util.Arrays.toString(
            new int[][] {
                { 1 },
                { 2, 3 },
            }
        )
    ); // prints "[[I@187aeca, [I@e48e1b]"
    
    System.out.println(
        java.util.Arrays.deepToString(
            new int[][] {
                { 1 },
                { 2, 3 },
            }
        )
    ); // prints "[[1], [2, 3]]"
    

    Again, the reasoning is similar: Arrays.toString(Object[]) treats each element as an Object, and just call its toString() method. Arrays inherit its toString() from their common superclass Object.

    If you want java.util.Arrays to consider nested arrays, you need to use deepToString, just like you need to use deepEquals.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 463k
  • Answers 463k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Update directly in the database and return the new revision:… May 16, 2026 at 12:49 am
  • Editorial Team
    Editorial Team added an answer Definitely go for a class here. That's what objects and… May 16, 2026 at 12:49 am
  • Editorial Team
    Editorial Team added an answer overflow will only work if it knows where to start… May 16, 2026 at 12:49 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.