为什么Java的部门破裂了? [英] Why is Java's division broken?

查看:55
本文介绍了为什么Java的部门破裂了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一位经验丰富的php开发人员,刚开始学习Java.我目前正在学习Lynda的一些课程,但我仍处于初期阶段.我正在编写要求用户输入并进行简单计算和操作的示例程序.

I am an experienced php developer just starting to learn Java. I am following some Lynda courses at the moment and I'm still really early stages. I'm writing sample programs that ask for user input and do simple calculation and stuff.

昨天我遇到了这种情况:

Yesterday I came across this situation:

double result = 1 / 2;

用穴居人的大脑,我会想到result == 0.5,但不是,不是Java.显然是1 / 2 == 0.0.是的,我知道如果将其中一个操作数更改为双精度,结果也将是双精度.

With my caveman brain I would think result == 0.5, but no, not in Java. Apparantly 1 / 2 == 0.0. Yes, I know that if I change one of the operands to a double the result would also be a double.

这实际上使我感到恐惧.我禁不住认为这是很糟糕的.认为整数除法会导致整数是非常幼稚的.我认为这种情况很少见.

This scares me actually. I can't help but think that this is very broken. It is very naive to think that an integer division results in an integer. I think it is even rarely the case.

但是,由于Java的使用非常广泛,并且正在搜索为什么Java的划分被破坏了?"没有产生任何结果,我可能是错误的.

But, as Java is very widely used and searching for 'why is java's division broken?' doesn't yield any results, I am probably wrong.

我的问题是:

  1. 为什么除法行为会如此?
  2. 我还能在哪里找到这样的魔术/巫毒/意外行为?

推荐答案

您正在像PHP开发人员一样思考; PHP是动态键入的语言.这意味着类型是在运行时推导的,因此小数在逻辑上不能产生整数,因此从除法运算中隐含一个double(或float).

You are thinking like a PHP developer; PHP is dynamically typed language. This means that types are deduced at run-time, so a fraction cannot logically produce a whole number, thus a double (or float) is implied from the division operation.

Java,C,C ++,C#和许多其他语言是严格键入的语言,因此当一个整数除以一个整数时,您会得到一个整数,100/50会给我返回2,就像100/45会给我2一样,因为100/45实际上是2.2222...,因此请截断小数以得到整数(整数除法),您会得到2.

Java, C, C++, C# and many other languages are strongly typed languages, so when an integer is divided by an integer you get an integer back, 100/50 gives me back 2, just like 100/45 gives me 2, because 100/45 is actually 2.2222..., truncate the decimal to get a whole number (integer division) and you get 2.

在强类型语言中,如果要使结果符合期望,则需要显式(或隐式),这就是为什么将除法运算中的参数之一设为double或float会导致浮点除法(返回分数).

In a strongly typed language, if you want a result to be what you expect, you need to be explicit (or implicit), which is why having one of your parameters in your division operation be a double or float will result in floating point division (which gives back fractions).

因此,在Java中,您可以执行以下一项操作来获取小数:

So in Java, you could do one of the following to get a fractional number:

double result = 1.0 / 2;
double result = 1f / 2;
double result = (float)1 / 2;

从松散类型的动态语言变为强类型的静态语言可能会令人讨厌,但不必害怕.只是要了解,除了输入之外,您还必须格外小心进行验证,还必须验证类型.

Going from a loosely typed, dynamic language to a strongly typed, static language can be jarring, but there's no need to be scared. Just understand that you have to take extra care with validation beyond input, you also have to validate types.

从PHP到Java,您应该知道自己不能做这样的事情:

Going from PHP to Java, you should know you can not do something like this:

$result = "2.0";
$result = "1.0" / $result;
echo $result * 3;

在PHP中,这将产生输出1.5(自(1/2)*3 == 1.5起),但是在Java中,

In PHP, this would produce the output 1.5 (since (1/2)*3 == 1.5), but in Java,

String result = "2.0";
result = "1.0" / result;
System.out.println(result * 1.5);

这将导致错误,因为您不能分割字符串(不是数字).

This will result in an error because you cannot divide a string (it's not a number).

希望可以提供帮助.

这篇关于为什么Java的部门破裂了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆