Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
AbstractList equals() method in Java with Examples
Next article icon

Arrays.equals() in Java with Examples

Last Updated : 25 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Arrays.equals() method comes under the Arrays class in Java. It is used to check two arrays, whether single-dimensional or multi-dimensional array are equal or not.

Example:

Below is a simple example that uses Arrays.equals() method to check if two arrays of integers are equal or not.

Java
// Java program to demonstrate  // working of Arrays.equals() import java.util.Arrays;  public class ArraysEquals {     public static void main(String[] args) {                // create different integers arrays         int[] arr1 = {1, 2, 3, 4};         int[] arr2 = {1, 2, 3, 4};         int[] arr3 = {1, 2, 4, 3};                  System.out.println("arr1 equals to arr2: " +                                 Arrays.equals(arr1, arr2));         System.out.println("arr1 equals to arr3: " +                                 Arrays.equals(arr1, arr3));     } } 

Output
arr1 equals to arr2: true arr1 equals to arr3: false 

Table of Content

  • Syntax of Arrays.equals() method
  • Examples of Arrays.equals() in Java
    • Compare Arrays of User-Defined Objects Using Arrays.equals()
    • Compare Multidimensional Arrays with Arrays.equals()

Syntax of Arrays.equals() method

public static boolean equals(int[] a, int[] a2)

Parameters:

  • a: The first int[] array to be compared.
  • a2: The second int[] array to be compared.

Returns Type: boolean: Returns true if the two arrays are equal, otherwise false.

Examples of Arrays.equals() in Java

Compare Arrays of User-Defined Objects Using Arrays.equals()

In this example, we will compare arrays of Student objects for equality by overriding the equals() method to define how students are considered equal based on their attributes.

Java
// Java program to demonstrate  // working of Arrays.equals() // for user defined objects import java.util.Arrays;  public class ArraysEquals {     public static void main (String[] args) {                Student [] arr1 = {new Student(1, "a", "MP"),                            new Student(2, "b", "UP"),                            new Student(3, "c", "Delhi")};                  Student [] arr2 = {new Student(1, "a", "MP"),                            new Student(2, "b", "UP"),                            new Student(3, "c", "Delhi")};                  Student [] arr3 = {new Student(1, "a", "MP"),                            new Student(2, "b", "UP"),                            new Student(3, "c", "Jaipur"),                         };                  System.out.println("arr1 equals to arr2: " +                                     Arrays.equals(arr1, arr2));         System.out.println("arr1 equals to arr3: " +                                     Arrays.equals(arr1, arr3));         }     }  // class to represent a student class Student {     int r;     String n, a;      // Constructor     public Student(int r, String n,                             String a)     {         this.r = r;         this.n = n;         this.a = a;     }          @Override     public boolean equals(Object o) {                  // typecast o to Student so that we can compare students         Student s = (Student) o;                  return this.r == s.r && this.n.equals(s.n)                                 && this.a.equals(s.a);     } } 

Output
arr1 equals to arr2: true arr1 equals to arr3: false 


Compare Multidimensional Arrays with Arrays.equals()

In this example, we will use both Arrays.equals() and Arrays.deepEquals() method to compare two multidimensional arrays. It returns true, if the two specified arrays are deeply equal to each other, otherwise it will return false.

Java
import java.util.Arrays;  public class ArrayEqual2 {     public static void main(String[] args) {                // create array of arrays         int[][] arr1 = { { 0, 1 }, { 1, 0 } };         int[][] arr2 = { { 0, 1 }, { 1, 0 } };          System.out.println("is arr1 equals to arr2: "                            + Arrays.equals(arr1, arr2));         System.out.println("is arr1 deepequals to arr2: "                            + Arrays.deepEquals(arr1, arr2));     } } 

Output
is arr1 equals to arr2: false is arr1 deepequals to arr2: true 

Note: Arrays.equals() method can be only performed to 1-D arrays and hence it doesn’t work for multidimensional arrays.

Array.equals() vs. Arrays.deepEquals()

  • Arrays.equals() method compares arrays element by element, but this method only works for single-dimensional arrays or arrays of objects.
  • Arrays.deepEquals() method compares arrays recursively by making it suitable for multi-dimensional arrays.


Next Article
AbstractList equals() method in Java with Examples

G

Gaurav Miglani
Improve
Article Tags :
  • Java
  • Java-Arrays
Practice Tags :
  • Java

Similar Reads

  • Arrays.deepEquals() in Java with Examples
    Arrays.deepEquals() method comes under the Arrays class in Java. It is used to check two arrays, whether single-dimensional or multi-dimensional, are equal or not. It compares arrays element by element, even when dealing with nested arrays. Example: Below is a simple example that uses Arrays.deepEqu
    4 min read
  • Charset equals() method in Java with Examples
    The equals() method is a built-in method of the java.nio.charset checks if a given object of charset is equal to another given object of the charset. Two charsets are considered equal if, and only if, they have the same canonical names. A charset is never equal to any other type of object. Syntax: p
    2 min read
  • Arrays.fill() in Java with Examples
    The Arrays.fill() is a method in the java.util.Arrays class. This method assigns a specified value to each element of an entire array or a specified range within the specified array. Example: Now let's understand this with the below simple example to fill an entire array with a specified value: [GFG
    3 min read
  • AbstractList equals() method in Java with Examples
    The equals() method of java.util.AbstractList class is used to compare the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e
    3 min read
  • Character.equals() method in Java with examples
    The java.lang.Character.equals() is a function in Java which compares this object against the specified object. If the argument is not null then the result is true and is a Character object that represents the same char value as this object. Syntax: public boolean equals(Object obj) Parameters: The
    2 min read
  • Byte equals() method in Java with examples
    The equals() method of Byte class is a built in method in Java which is used to compare the equality given Object with the instance of Byte invoking the equals() method. Syntax ByteObject.equals(Object a) Parameters: It takes an Object type object a as input which is to be compared with the instance
    2 min read
  • Date equals() method in Java with Examples
    The equals() method of Java Date class checks if two Dates are equal, based on millisecond difference. Syntax: public boolean equals(Object obj) Parameters: The function accepts a single parameter obj which specifies the object to be compared with. Return Value: The function gives 2 return values sp
    2 min read
  • DateFormat equals() Method in Java with Examples
    The equals() Method of the DateFormat class is used to compare two DateFormat objects. The method returns True if this DateFormat is equal to the passed object else returns False. Syntax: public boolean equals(Object obj) Parameters: The method takes one parameter obj of Object type and refers to th
    2 min read
  • AbstractMap equals() Method in Java with Examples
    The equals() method of the Java AbstractMap class is used to check equality between two maps. It compares the key-value pair in the current map with the key-value pair of the specified map. Syntax of AbstarctMap equals() Methodpublic boolean equals(Object o) Parameter: The Object to be compared with
    2 min read
  • BitSet equals() Method in Java with Examples
    The equals() method of Java BitSet class is used to check for equality between two bitsets. It verifies whether the elements of one set passed as a parameter is equal to the elements of this set or not. The method returns true if the bitsets match else false. Syntax: Bit_Set1.equals(Bit_Set2) Parame
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences