R
Robert Klemme
[ SNIP ]
Fair cop, I am semi-dyslexic I guess. I don't think it's a
particularly valid point - arguing that someone might screw up and
not know what the valid range is for bytes or shorts, for example,
and not argue that this numerically-challenged person will also have
problems with ints, is somewhat disingenuous.
Or is it just that there's so much more room with int that a sloppy
person is much less likely to have problems? That approach doesn't
fill me with confidence.
Furthermore, once this hypothetical coder starts getting negative
array indexes the odds are pretty decent that they may figure out
what the issue is.
He also failed to answer my question: What is the advantage of
thinking imprecisely in this matter?
Why did you ask the question? I wasn't thinking imprecisely. I know
that byte/short/char values will get widened when used as array
indexes. Personally I'll also ensure that these values are valid
bytes/shorts/chars...just like I would do for int.
Where's the imprecise thinking? My original reply didn't mention
anything whatsoever about array indexes _not_ being ints: in fact I
started talking about widening conversions right away. If your
original byte/short/char value is valid, then you've got no problems
at all. If you want to suppose that people are using invalid values,
well, hell, that's a bigger problem anyhow.
I tried to reconstruct how Lew and you got into this discussion but it's
hard to track it down. I think things got a bit off track when Lew wrote
One gets problems with byte->int and short->int with array indexes
the way one gets into trouble with those widening conversions
generally. The usual suspect is the lack of unsigned versions, so
widening a (byte)0xA0, for example, would result in a negative
index.
The fact that you have to use a cast demonstrates that 0xA0 is not a
literal which can be used as byte:
byte b = 0xA0; // won't compile
0xA0 is an int literal and the cast to byte already introduces the
negative sign (the value is -96 decimal). So in this case the error
would be already to assume that
byte b = (byte) 0xA0;
leaves a positive value in b. So it's not the widening conversion which
causes trouble but the cast from int to byte which looses information by
cutting off bits 8 through 31 leaving only bits 0 to 7.
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#25363
The lowest int literal directly assignable to byte is -0x80 (-128
decimal) and the highest is 0x7f (127 decimal). 0xA0 is outside this range.
It seems nobody exactly stated that you could index arrays with bytes.
Arne said
Java is a simpler language than Ada. In Java array indexes are
int and you have byte/short/int/long types and that is it.
and Mark said
OK, you mean for indexes? char works too, and long really doesn't --
it has to be cast to an int.
before Lew responded with the quote above. Of course a char "works" as
array index but the char is converted to int before indexing. However,
this conversion does not change the value of the char.
It's probably worthwhile to note that char is unsigned so in a way it's
better suited to index arrays than byte and short because all values are
legal array indexes.
Kind regards
robert