简体   繁体   中英

Arithmetic operations in Java

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

Check this out: 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)
  • 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

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:

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.

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. 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 . 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.

Dummy answer: because this is how the Java Language Specification defines them :

4.2.2. Integer Operations

The Java programming language provides a number of operators that act on integral values:

[...]

  • The numerical operators, which result in a value of type int or long :

Do you mean why you don't get a double or BigInteger result? 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. Much easier to define addition or multiplication modulo 2^32 (or 2^64) and not worry about it. 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. But people got used to it, so it remains in Java today. Use Python 3 if you want your arithmetic autoconverted to a type that can hold the result.

The reason is kind of the same as why we have primitive types in Java at all -- it allows writing efficient code. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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