Re: byte + byte -> int
- From: Eric Sosman <Eric.Sosman@xxxxxxx>
- Date: Fri, 20 Mar 2009 17:26:09 -0400
Lew wrote:
Eric Sosman wrote:This feature of the definition of Java is a little bit
arbitrary, to be sure. For example, in
int x = Integer.MIN_VALUE + 10;
int y = Integer.MIN_VALUE + 20;
int z = x + y;
... the arguments in the preceding paragraph still apply, with
the obvious changes. The compiler doesn't know that x and y are
constant, the compiler can't be sure that their mathematical sum
is in range for an int (in fact, it isn't), but allows the line
anyhow. Java doesn't complain about the loss of precision when
That's because Java doesn't see this as a loss of precision but as an
overflow, and the rules specifically address overflow.
an int (or long) calculation wraps around, but whines loudly when
it thinks precision might be lost in a down-conversion. It's not
consistent, it may not be defensible -- but That's The Way It Is.
It's both consistent and defensible. The operation of integer
addition always results in a 32-bit value, so there's no loss of
precision when you assign the result to an integer.
That's just defining the problem away, not solving it or even
avoiding it. Java adds a couple numbers and delivers an answer,
an answer that would earn a failing mark on a third-grade arithmetic
quiz, and proudly boasts "That's not a bug; that's a feature!" It
crosses my mind that the JLS is akin to Newspeak.
The old steam-powered computers I shovelled coal for as a lad[*]
had one nice feature that our sleek fast modern machines have lost:
They'd trap when a computation overflowed. Blasted inconvenient now
and then, but a great safety feature for programs that would otherwise
have produced results that were wrong, R-O-N-G, wrong.
[*] "Merely corroborative detail intended to lend verisimilitude
to an otherwise bald and unconvincing narrative." I never actually
shovelled coal into computers; the boilers were gas-fired.
--
Eric.Sosman@xxxxxxx
.
- Follow-Ups:
- Re: byte + byte -> int
- From: Mike Schilling
- Re: byte + byte -> int
- From: Thomas Pornin
- Re: byte + byte -> int
- From: Lew
- Re: byte + byte -> int
- References:
- byte + byte -> int
- From: Ayun ka Koun
- Re: byte + byte -> int
- From: Eric Sosman
- Re: byte + byte -> int
- From: Lew
- byte + byte -> int
- Prev by Date: Re: byte + byte -> int
- Next by Date: TimeZone change - how to use TimeZone class
- Previous by thread: Re: byte + byte -> int
- Next by thread: Re: byte + byte -> int
- Index(es):
Relevant Pages
|