Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7250
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Using Enumerated Types as Array Indexes |
| Date | 2011-08-19 14:57 +0200 |
| Message-ID | <9b74tiFf06U1@mid.individual.net> (permalink) |
| References | (8 earlier) <f08d98e8-e04d-4d4e-b518-44bba735dba6@glegroupsg2000goo.googlegroups.com> <rY43q.61720$7K4.15412@newsfe14.iad> <4e4cdfab$0$2524$da0feed9@news.zen.co.uk> <a45df926-b010-4f9f-8697-f66a3b85812a@glegroupsg2000goo.googlegroups.com> <Dvf3q.591613$SG4.295041@newsfe03.iad> |
On 18.08.2011 23:30, Arved Sandstrom wrote: > On 11-08-18 03:35 PM, Lew wrote: >> RedGrittyBrick wrote: >>> Arved Sandstrom wrote: > [ SNIP ] > >>>> (or any other legal byte value for that matter) is just plain >>>> incorrect, and can be disproved in a minute or two. Indexing >>>> into any array with (byte)0x0A returns the 11th element of a >>>> (sufficiently large) array. >>> >>> I think you misread Lew, he said (byte)0xA0 (not your 0x0A) >>> doesn't get you the 161st element of a (sufficiently large) >>> array. > > 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Using Enumerated Types as Array Indexes KevinSimonson <kvnsmnsn@hotmail.com> - 2011-08-16 07:53 -0700
Re: Using Enumerated Types as Array Indexes Patricia Shanahan <pats@acm.org> - 2011-08-16 09:30 -0700
Re: Using Enumerated Types as Array Indexes Tom Anderson <twic@urchin.earth.li> - 2011-08-16 21:37 +0100
Re: Using Enumerated Types as Array Indexes Gene Wirchenko <genew@ocis.net> - 2011-08-16 14:54 -0700
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-16 20:47 -0400
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-16 20:38 -0400
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-16 17:52 -0700
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-16 21:56 -0400
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-16 20:20 -0700
Re: Using Enumerated Types as Array Indexes Lew <lewbloch@gmail.com> - 2011-08-16 20:45 -0700
Re: Using Enumerated Types as Array Indexes Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-17 06:31 -0300
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-17 13:28 +0000
Re: Using Enumerated Types as Array Indexes Lew <lewbloch@gmail.com> - 2011-08-17 07:17 -0700
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-17 19:02 +0200
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-17 16:09 -0700
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-17 23:48 +0000
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-17 17:34 -0700
Re: Using Enumerated Types as Array Indexes Patricia Shanahan <pats@acm.org> - 2011-08-17 18:07 -0700
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-18 08:22 -0700
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-18 18:37 +0000
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-18 11:50 -0700
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-18 20:49 +0000
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-18 14:48 -0700
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-18 22:50 +0000
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-17 21:49 -0400
Re: Using Enumerated Types as Array Indexes Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-17 21:50 -0400
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-19 14:13 +0200
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-19 20:05 +0000
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-20 00:20 +0200
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-19 23:11 +0000
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-20 11:21 +0200
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-20 11:45 +0000
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-20 15:23 +0200
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-20 16:15 +0000
Re: Using Enumerated Types as Array Indexes Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-17 21:34 -0400
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-18 07:58 +0200
Re: Using Enumerated Types as Array Indexes Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-18 06:29 -0300
Re: Using Enumerated Types as Array Indexes RedGrittyBrick <RedGrittyBrick@spamweary.invalid> - 2011-08-18 10:47 +0100
Re: Using Enumerated Types as Array Indexes Lew <lewbloch@gmail.com> - 2011-08-18 11:35 -0700
Re: Using Enumerated Types as Array Indexes Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-18 18:30 -0300
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-19 14:57 +0200
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-19 20:10 +0000
Re: Using Enumerated Types as Array Indexes Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-19 19:12 -0300
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-20 00:26 +0200
Re: Using Enumerated Types as Array Indexes Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-19 22:59 +0000
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-17 21:34 -0400
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-17 21:46 -0400
Re: Using Enumerated Types as Array Indexes markspace <-@.> - 2011-08-17 19:51 -0700
Re: Using Enumerated Types as Array Indexes David Lamb <dalamb@cs.queensu.ca> - 2011-08-17 08:29 -0400
Re: Using Enumerated Types as Array Indexes Robert Klemme <shortcutter@googlemail.com> - 2011-08-17 16:04 +0200
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-17 21:51 -0400
Re: Using Enumerated Types as Array Indexes Niklas Holsti <niklas.holsti@tidorum.invalid> - 2011-08-17 11:24 +0300
Re: Using Enumerated Types as Array Indexes Arne Vajhøj <arne@vajhoej.dk> - 2011-08-17 21:55 -0400
Re: Using Enumerated Types as Array Indexes Niklas Holsti <niklas.holsti@tidorum.invalid> - 2011-08-18 10:59 +0300
Re: Using Enumerated Types as Array Indexes Roedy Green <see_website@mindprod.com.invalid> - 2011-08-17 22:09 -0700
csiph-web