要在Java中检查两个数字是否相等,我们可以使用Equals()方法以及==运算符。
首先,让我们设置整数。
Integer val1 = new Integer(5);
Integer val2 = new Integer(5);
现在,要检查它们是否相等,让我们使用==运算符。
(val1 == val2)
现在让我们看完整的例子。
示例
public class Demo {
public static void main( String args[] ) {
Integer val1 = new Integer(5);
Integer val2 = new Integer(5);
Integer val3 = new Integer(10);
System.out.println("Integer 1 = "+val1);
System.out.println("Integer 2 = "+val2);
System.out.println("Integer 3 = "+val3);
System.out.println("val1 is equal to val2 = "+(val1 == val2));
System.out.println("val2 is not equal to val3 = "+(val2 != val3));
}
}
输出
Integer 1 = 5
Integer 2 = 5
Integer 3 = 10
val1 is equal to val2 = false
val2 is not equal to val3 = true