为什么无法使用运算符“==”比较 Java 中的两个 Double 类型数字?

huangapple go评论80阅读模式
英文:

Why can't I compare two numbers Double class Java using operator "=="?

问题

我已经尝试过制作一个简单的代码,使用Double类来比较数字是否相等。这是我的简单代码:

BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
Double number1;
Double number2;
System.out.println("Ask for first number:");
number1 = Double.parseDouble(bfr.readLine());
System.out.println("Ask for second number:");
number2 = Double.parseDouble(bfr.readLine());

if (number1 == number2) {
    System.out.println("These numbers are equals!");
    System.out.println(number1);
    System.out.println(number2);
} else {
    System.out.println("They're different!");
    System.out.println(number1);
    System.out.println(number2);
}

当我使用“int”而不是Double,并且使用运算符“==”时,它运行良好,但是当我使用“Double”时,它会显示这些是不同的数字,即使输入相同的数字。我尝试过使用Object.equals()方法,它可以正常工作,但我想知道为什么“Double”类不支持运算符“==”。

英文:

I have tried to make a simple code to compare numbers if is equal using Double class. This is my simple code:

BufferedReader bfr= new BufferedReader(new InputStreamReader(System.in));
	Double number1;
	Double number2;
	System.out.println("Ask for first number:");
	number1 = Double.parseDouble(bfr.readLine());
	System.out.println("Ask for second number:");
	number2 =  Double.parseDouble(bfr.readLine());
	
	if(number1==number2) {
		System.out.println("These numbers are equals!");
		System.out.println(number1);
		System.out.println(number2);
	}else {
		System.out.println("They're differents!");
		System.out.println(number1);
		System.out.println(number2);
	}

When I use "int" instead Double and the operator "==" it works fine but when I use "Double" it shows me they are different numbers even the same numbers are entered. I've tried to use Object.equals() method it works fine but I want to know why "Double" class does not admit operator "==".

答案1

得分: 2

在这里,intDouble 之间的重要区别不是浮点数的本质(尽管这可能会使计算的行为变得出乎意料),而是int 是原始类型,而 Double 是引用类型(围绕着 double 的包装器)。

当使用 == 比较引用类型值时,您是在检查对象标识。也就是说,表达式 number1 == number2 的意思是“变量 number1number2 是否引用完全相同的对象”,这几乎肯定不是您想要的。

解决方案,就像您发现的那样,是使用 number1.equals(number2)

另一个解决方法是从包装器 Double 切换为原始类型 double。通常情况下,您可以将一个赋值给另一个,适当的自动装箱/自动拆箱将处理转换。

英文:

The important difference between int and Double here is not the floating point nature (though that can make computations behave in unexpected ways), but the fact that int is a primitive type and Double is a reference type (a wrapper around double).

When you compare reference type values using == you check for object identity. I.e. the expression number1 == number2 means "do the variables number1 and number2 reference the exact same object", which is almost certainly not what you want.

The solution, as you found is to use number1.equals(number2).

Another workaround would be to switch from Double (the wrapper) to double the primitive type. Usually you can just assign one to the other and the appropriate autoboxing/autounboxing will handle the conversion.

huangapple
  • 本文由 发表于 2020年9月26日 04:17:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64070850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定