📜  在Java中检查两个整数是否相等

📅  最后修改于: 2022-05-13 01:55:46.906000             🧑  作者: Mango

在Java中检查两个整数是否相等

在Java中检查两个整数是否相等是通过各种方法完成的。

  1. 算术运算运算符
  2. 比较运算符
  3. 字符串函数
  4. 异或运算符
  5. 补码 (~) 和按位 (&)运算符

例子

Input:    FirstNumber = 15
          SecondNumber= 15
Output: Numbers are same

Input:    FirstNumber = 15
          SecondNumber= 25
Output: Numbers are not same

方法#1:算术运算符

如果两个数相等,则它们的减法等于 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");
    }
}


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");
    }
}


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");
    }
}


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");
    }
}


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");
    }
}



输出
Numbers are equal

方法#2:比较运算符

如果两个数字相等,则 if 条件中的相等运算符返回 true,否则返回 false。

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");
    }
}


输出
Numbers are equal

方法#3:字符串函数

将数字转换为字符串并在字符串类中使用 compareTo() 方法。如果两个字符串相同,compareTo() 方法返回 0,否则返回 1 或 -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");
    }
}


输出
Numbers are equal

方法#4:异或运算

XOR 属性表明两个相同数字的 XOR 为零。

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");
    }
}


输出
Numbers are equal

方法 #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");
    }
}


输出
Numbers are equal