Java: Why can't I declare integer types using scientific notation?
Asked Answered
J

3

16

I can easily read 2e15 as "two quadrillion" at a glance, but for 2000000000000000 I have to count the zeroes, which takes longer and can lead to errors.

Why can't I declare an int or long using a literal such as 2e9 or 1.3e6? I understand that a negative power of 10, such as 2e-3, or a power of 10 that is less than the number of decimal places, such as 1.0003e3, would produce a floating point number, but why doesn't Java allow such declarations, and simply truncate the floating-point part and issue a mild warning in cases where the resulting value is non-integral?

Is there a technical reason why this is a bad idea, or is this all about type-safety? Wouldn't it be trivial for the compiler to simply parse a statement like

long x = 2e12 as long x = 2000000000000 //OK for long

and int y = 2.1234e3 as int y = 2123.4 //warning: loss of precision

Jeremiad answered 14/7, 2013 at 5:18 Comment(4)
Eric Lippert: "I'm often asked why the compiler does not implement this feature or that feature, and of course the answer is always the same: because no one implemented it. Features start off as unimplemented and only become implemented when people spend effort implementing them: no effort, no feature. This is an unsatisfying answer of course, because usually the person asking the question has made the assumption that the feature is so obviously good that we need to have had a reason to not implement it."Arvy
@BrianRoach: I know what an integer is. I was asking if there's a specific reason as to why numbers that are expressed in floating-point form but evaluate to an integral value ex: 2.3e3 == 2300 can't be assigned without an explicit cast. Maybe the reason is simply what John Kugelman quoted. That would make sense. The reason I asked is just that I was curious if there was a technical reason why such a feature would be a bad idea.Jeremiad
No, I get the first part ... but that's not your second example; int y = 2123.4Arber
@BrianRoach In my second example I was suggesting that the compiler would issue a warning for a non-integral expression using scientific notation being assigned to an integer, in contrast to the first example, in which the expression is determined to be an integral value and can therefore be assigned to an integer type without any loss of precision. I understand that Java does not allow such assignments without an explicit cast; I was merely curious about the design decisions involved.Jeremiad
C
21

It's because when you use the scientific notation you create a floating point number (a double in your example). And you can't assign a floating point to an integer (that would be a narrowing primitive conversion, which is not a valid assignment conversion).

So this would not work either for example:

int y = 2d; //can't convert double to int

You have a few options:

  • explicitly cast the floating point to an integer: int y = (int) 2e6;
  • with Java 7+ use a thousand separator: int y = 2_000_000;
Cartelize answered 14/7, 2013 at 5:22 Comment(3)
The options you mention are easy to use and remove any need for the hypothetical feature I was talking about. Thanks!Jeremiad
@Cartelize Yes but a number without a radix point and without a d or f suffix is assumed to be an int. So I think the OP was asking why, for example, wouldn't 2e8 be interpreted as an int, and why wouldn't 2e12l be interpreted as a long (which would be consistent with how non-scientific notation numbers are interpreted). So the answer seems to be "There's no technical reason why the Java compiler designers handled sci notation in this regard. They could have done things the way the OP suggested, but they didn't--perhaps they just didn't think of doing it that way."Wormseed
@Wormseed well yes - I just pointed to the relevant parts of the specification that say that it doesn't work. Why it was done that way has to be asked to the JDK developers. See also the first comment under the question.Cartelize
E
3

Because it's a shortcoming of Java.

(Specifically, there is clearly a set of literals represented by scientific notation that are exactly represented by ints and longs, and it is reasonable to desire a way to express those literals as ints and longs. But, in Java there isn't a way to do that because all scientific notation literals are necessarily floats because of Java's language definition.)

Emprise answered 19/2, 2014 at 20:55 Comment(0)
H
1

You are asking about the rules on writing a integer literals. See this reference: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html The capability to use scientific notation as an integer literal might make things easier indeed but has not been implemented. I do not see any technical reason that would prevent such a feature from being implemented.

Hexangular answered 14/7, 2013 at 5:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.