简体   繁体   English

Java中的算术运算

[英]Arithmetic operations in Java

有人可以解释为什么对Java中的整数类型的算术运算总是导致“int”或“long”结果?

Check this out: http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter02/operators.html#ArithOps 看看这个: http//www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter02/operators.html#ArithOps

It explains how the type of the return value is determined by the types of the operands. 它解释了返回值的类型如何由操作数的类型决定。 Essentially: 实质上:

  • the arithmetic operators require a numeric type 算术运算符需要数字类型
  • if the type of either operand is an integral type, the return value will be the widest type included (so int + long = long) 如果任一操作数的类型是整数类型,则返回值将是包含的最宽类型(因此int + long = long)
  • if the type of either operand is a floating-point number then a floating-point number will be returned 如果任一操作数的类型是浮点数,则返回浮点数
  • if both operands are floating-point, then a double will be returned if either operand is a double 如果两个操作数都是浮点数,那么如果任一操作数是双精度数,则返回double

If you need to control the types, then you'll need to cast the operands to the appropriate types. 如果需要控制类型,则需要将操作数转换为适当的类型。 For example, int * int could be too long for an int, so you may need to do: 例如,对于int,int * int可能太长,因此您可能需要执行以下操作:

long result = myInt * (long) anotherInt

Likewise for really large or really tiny floats resulting from arithmetic operations. 同样,对于算术运算产生的非常大或非常小的浮点数。

I think it's worth pointing out that this (arithmetic operations on integers producing integers) is a feature of many many programming languages, not only Java. 我认为值得指出的是(对整数生成整数的算术运算)是许多编程语言的一个特征,而不仅仅是Java。

Many of those programming languages were invented before Java, many after Java, so I think that arguments that it is a hang-over from the days when hardware was less capable are wide of the mark. 其中许多编程语言都是在Java之前发明的,很多都是在Java之后发生的,所以我认为从硬件能力不强的那些时代开始,它的争论就是广泛的。 This feature of language design is about making languages type-safe . 语言设计的这一特点是使语言类型安全 There are very good reasons for separating integers and floating-point numbers in programming languages, and for making the programmer responsible for identifying when and how conversions from type to type take place. 在编程语言中分离整数和浮点数有很好的理由,并使程序员负责识别何时以及如何进行从类型到类型的转换。

Because the basic integer arithmetic operators are only defined either between int and int or between long and long . 因为基本整数算术运算符仅在intint之间或longlong之间定义。 In all other cases types are automatically widened to suit. 在所有其他情况下,类型会自动加宽以适应。 There are no doubt some abstruse paragraphs in the Java Language Specification explaining exactly what happens. 毫无疑问, Java语言规范中有一些深奥的段落可以解释究竟发生了什么。

Dummy answer: because this is how the Java Language Specification defines them : 虚拟答案:因为这是Java语言规范定义它们的方式

4.2.2. 4.2.2。 Integer Operations 整数运算

The Java programming language provides a number of operators that act on integral values: Java编程语言提供了许多作用于整数值的运算符:

[...] [...]

  • The numerical operators, which result in a value of type int or long : 数值运算符,其结果为intlong类型的值:

Do you mean why you don't get a double or BigInteger result? 你的意思是为什么你没有得到doubleBigInteger结果? Historical accident and efficiency reasons, mostly. 历史事故和效率的原因,大多数。 Detecting overflow from + or * and handing the result (from Integer.MAX_VALUE * Integer.MAX_VALUE , say) means generating lots of exception detection code that will almost never get triggered, but always needs to get executed. 检测来自+*溢出并处理结果(来自Integer.MAX_VALUE * Integer.MAX_VALUE ,比方说)意味着生成大量异常检测代码,这些代码几乎永远不会被触发,但总是需要执行。 Much easier to define addition or multiplication modulo 2^32 (or 2^64) and not worry about it. 更容易定义加法或乘法模2 ^ 32(或2 ^ 64)而不用担心它。 Same for division with a fractional remainder. 对于具有小数余数的除法也是如此。

This was certainly the case long ago with C. It is less of an issue today with superscalar processors and lots of bits to play with. 很久以前C就是这种情况。今天的超标量处理器和很多比特都不是问题。 But people got used to it, so it remains in Java today. 但是人们习惯了它,所以今天它仍然存在于Java中。 Use Python 3 if you want your arithmetic autoconverted to a type that can hold the result. 如果您希望算术自动转换为可以保存结果的类型,请使用Python 3。

The reason is kind of the same as why we have primitive types in Java at all -- it allows writing efficient code. 原因与我们在Java中拥有原始类型的原因相同 - 它允许编写有效的代码。 You may argue that it also makes less efficient but correct code much uglier; 您可能会认为它也会使效率更低但代码更正确更加丑陋; you'd be about right. 你是对的。 Keep in mind that the design choice was made around 1995. 请记住,设计选择是在1995年左右完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM