La verificación de dos enteros iguales o no en Java se realiza mediante varios enfoques.
Input: FirstNumber = 15 SecondNumber= 15 Output: Numbers are same Input: FirstNumber = 15 SecondNumber= 25 Output: Numbers are not same
Si dos números son iguales entonces su resta es igual a 0.
Java
// Check Two Integers are Equal or Not in Java // using arithmetic operator import 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"); } }
Producción
Numbers are equal
Si dos números son iguales, entonces el operador igual en si la condición devuelve verdadero, de lo contrario devuelve falso.
Java
// Check Two Integers are Equal or Not in Java // using Comparison Operators import 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"); } }
Producción
Numbers are equal
Convierta Números a string y use el método compareTo() en la clase de string. El método compareTo() devuelve 0 si ambas strings son iguales; de lo contrario, devuelve 1 o -1.
Java
// Check Two Integers are Equal or Not in Java // using String functions import 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"); } }
Producción
Numbers are equal
La propiedad XOR establece que XOR de dos números iguales es cero.
Java
// Check Two Integers are Equal or Not in Java // using XOR Operation import 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"); } }
Producción
Numbers are equal
Enfoque #5:
Java
// Check Two Integers are Equal or Not in Java import 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"); } }
Producción
Numbers are equal
Publicación traducida automáticamente
Artículo escrito por mehtarashita18 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA