You can't directly compare an int[] to an int. You need to access a value in the array and then compare.

int[] myArray = {1, 2, 3};
int value = 1;
if (myArray[0] == value) {/*do something*/}
Answer from Grice on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-arrays-compare-method-with-examples
Java Arrays compare() Method with Examples - GeeksforGeeks
To handle null values safely, use Arrays.compare() with a Comparator like Comparator.nullsFirst() or nullsLast(). ... This behavior is similar to how words are compared in a dictionary. Example 1: Compare Two Integer Arrays (Equal Arrays) ... import java.util.Arrays; public class CompareExample{ public static void main(String[] args) { //Initialized two integer array int[] arr1 ={6, 7, 8, 11, 18, 8, 2, 5}; int[] arr2 ={3, 5, 9, 13, 28, 6, 8, 9}; //compare both integer array using compare method and finally print result System.out.println(""+ Arrays.compare(arr1,arr2)); } }
Published: April 14, 2026
🌐
GeeksforGeeks
geeksforgeeks.org › java › compare-two-arrays-java
Compare Two Arrays in Java - GeeksforGeeks
July 23, 2025 - So, when we compare arr1 and arr2, two reference variables are compared, therefore, we get the output as "Not Same". ... Java provides a direct method Arrays.equals() to compare two arrays.
🌐
Medium
medium.com › @AlexanderObregon › javas-arrays-compare-method-explained-7989daf18608
Java’s Arrays.compare() Method Explained | Medium
February 6, 2025 - The compare() method in the java.util.Arrays class provides a way to compare two arrays lexicographically. This means it determines which array is greater, smaller, or if they are equal by checking the elements in order.
🌐
DataCamp
datacamp.com › doc › java › compare
Java Array compare()
While Java does not have a built-in compare() method specifically for arrays, it provides utility methods in the java.util.Arrays class to facilitate array comparison.
🌐
Quora
quora.com › How-do-you-compare-elements-inside-an-array-in-Java
How to compare elements inside an array in Java - Quora
Answer (1 of 5): By compare do you mean compare elements in an array to a number? If so I’ve included the following(working) code for you. //Alex shen //3/12/34 //ArrayExample.java //This program is about basic arrays. import java.util.Scanner; public class ArrayExample { public static vo...
🌐
Baeldung
baeldung.com › home › java › java array › comparing arrays in java
Comparing Arrays in Java | Baeldung
January 8, 2024 - Otherwise, it returns a value less than zero. Furthermore, a null is always lexicographically less than a non-null array. Please note that the Arrays class offers several overloaded compare() versions for comparing arrays holding primitive data such as boolean, byte, char, and int.
Find elsewhere
🌐
Javatpoint
javatpoint.com › how-to-compare-two-arrays-in-java
How to Compare Two Arrays in Java - Javatpoint
How to Compare Two Arrays in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Java67
java67.com › 2014 › 05 › how-to-compare-two-arrays-in-java-string-int-example.html
How to compare two Arrays in Java to check if they are equal - [String & Integer Array Example] | Java67
As I said in the previous section that java.util.Arrays class has overloaded equals() and deepEquals() to accept object[]. Since arrays are co-variant, you can pass String[] to a method that accepts an Object[], which is used to compare String ...
🌐
CodeJava
codejava.net › java-core › collections › comparing-arrays-in-java-examples
Comparing Arrays in Java Examples
April 11, 2024 - The Arrays utility provides the following methods for comparing two arrays of same type: ... boolean equals(Object[] a, Object[] a2)Here, X denotes a primitive type (byte, short, char, int, float, and double). Two arrays are considered equal if they contain the same elements in the same order.
🌐
TutorialsPoint
tutorialspoint.com › how-to-compare-two-arrays-in-java
How to compare two arrays in Java
import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { int[] ary = {1,2,3,4,5,6}; int[] ary1 = {1,2,3,4,5,6}; int[] ary2 = {1,2,3,4}; System.out.println("Is array 1 equal to array 2?? " +Arrays.equals(ary, ary1)); System.out.println("Is array 1 equal to array 3??
🌐
Coderanch
coderanch.com › t › 477547 › java › Comparing-Array-Elements
Comparing Array Elements (Beginning Java forum at Coderanch)
October 10, 2016 - I tried to take advantage of knowing that the array lists are from least to greatest. If any element values matched, the element value from the second list would decrease the first match by 1. Then all other matching elements would get the decreased first match value. Therefore, duplicates cannot be matched because it is from least to greatest. ... Boost this thread! ... Compare the values in 2 array.
🌐
YouTube
youtube.com › watch
How to Compare Arrays in Java - Comparing 2 Arrays by Example - Learn Java Programming - Appficial - YouTube
When comparing two arrays is Java, you cannot use the == operator to compare arrays because iIt will only tell you if the two arrays point to the same object...
Published: March 1, 2018
🌐
Oracle
blogs.oracle.com › javamagazine › java-quiz-arrays-compare-mismatch
Quiz yourself: Comparing Java arrays and interpreting the mismatch method | javamagazine
July 10, 2023 - The mismatch() method returns the index value of the first point in the arrays where the values of the elements differ. If the arrays are identical in contents and length, the method returns -1. Also, if either array reference is null, a ...
🌐
TutorialsPoint
tutorialspoint.com › java › util › arrays_compare_char.htm
Java Arrays compare() Method
This method returns the value 0 if, over the specified ranges, the first and second array are equal and contain the same elements in the same order; a value less than 0 if, over the specified ranges, the first array is lexicographically less ...
🌐
W3Schools
w3schools.com › java › ref_arrays_compare.asp
Java Arrays compare() Method
The compare() method compares two arrays lexicographically. ... Coding fundamentals as bite-sized lessons and challenges. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: ...
🌐
Medium
medium.com › @AlexanderObregon › finding-matching-items-in-two-java-arrays-43751484730f
Finding Matching Items in Two Java Arrays | Medium
July 17, 2025 - Here’s the core idea. You take one array and compare every element in it to every element in the second array. If there’s a match, that value goes into a new structure like a list or another array.