Reputation: 199
Can someone explain that why do arithmetic operations on integral types in Java always result in "int" or "long" results?
Upvotes: 0
Views: 2966
Reputation: 78364
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.
Upvotes: 1
Reputation: 200296
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.
Upvotes: 0
Reputation: 23265
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.
Upvotes: 0
Reputation: 6120
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:
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.
Upvotes: 1
Reputation: 340993
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
orlong
:
Upvotes: 0
Reputation: 15683
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.
Upvotes: 0