Check if Two Integers are Equal or Not in Java - GeeksforGeeks

21 Jun, 2021
Checking two integers equal or not in Java is done by various approaches.
Arithmetic operatorComparison OperatorsString functionsXOR operatorComplement (~) and bit-wise (&) operator
Arithmetic operator
Comparison Operators
String functions
XOR operator
Complement (~) and bit-wise (&) operator
Example
Input:    FirstNumber = 15
          SecondNumber= 15
Output: Numbers are same

Input:    FirstNumber = 15
          SecondNumber= 25
Output: Numbers are not same
Approach #1: Arithmetic Operator
If two numbers are equal then their subtraction is equal to 0.
Java
// Check Two Integers are Equal or Not in Java// using arithmetic operatorimport java.io.*;class GFG {    public static void main(String[] args)    {        int firstNumber = 15;        int secondNumber = 15;        if ((firstNumber - secondNumber) == 0)            System.out.println("Numbers are equal");        else            System.out.println("Numbers are not equal");    }}
  
Numbers are equal
 
Approach #2: Comparison Operators
 
If two numbers are equal then equal operator in if condition returns true, else return false.
 
Java
// Check Two Integers are Equal or Not in Java// using Comparison Operatorsimport java.io.*;class GFG {    public static void main(String[] args)    {        int firstNumber = 15;        int secondNumber = 15;        if (firstNumber == secondNumber)            System.out.println("Numbers are equal");        else            System.out.println("Numbers are not equal");    }}
  
Numbers are equal
 
Approach #3: String functions
 
Convert Numbers to string and use compareTo() method in the string class. compareTo() method returns 0 if both strings are same, else returns 1 or -1.
 
Java
// Check Two Integers are Equal or Not in Java// using String functionsimport java.io.*;class GFG {    public static void main(String[] args)    {        String firstNumber = 15 + "";        String secondNumber = 15 + "";        if (firstNumber.compareTo(secondNumber) == 0)            System.out.println("Numbers are equal");        else            System.out.println("Numbers are not equal");    }}
  
Numbers are equal
 
Approach #4: XOR Operation
 
XOR property states that XOR of the two same numbers is zero.
 
Java
// Check Two Integers are Equal or Not in Java// using XOR Operationimport java.io.*;class GFG {    public static void main(String[] args)    {        int firstNumber = 15;        int secondNumber = 15;        if ((firstNumber^secondNumber)==0)            System.out.println("Numbers are equal");        else            System.out.println("Numbers are not equal");    }}
  
Numbers are equal
 
Approach #5: Complement (~) and Bit-wise (&) Operator
 
Java
// Check Two Integers are Equal or Not in Javaimport java.io.*; class GFG {    public static void main(String[] args)    {        int firstNumber = 15;        int secondNumber = 15;        if ((firstNumber & ~secondNumber) == 0            && (~firstNumber & secondNumber) == 0)            System.out.print("Numbers are equal");        else            System.out.print("Numbers are not equal");    }}
  
Numbers are equal
 
saurabh1990aror
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways of Reading a text file in Java
Stream In Java
Constructors in Java
Generics in Java
Exceptions in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?