Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Using Enumerated Types as Array Indexes Date: Wed, 17 Aug 2011 21:34:03 -0400 Organization: A noiseless patient Spider Lines: 46 Message-ID: References: <4e4b0d81$0$314$14726298@news.sunsite.dk> <4e4b1fe4$0$314$14726298@news.sunsite.dk> <6d418cda-dab3-43df-a9ec-293b43f2bbd8@glegroupsg2000goo.googlegroups.com> <9b2aglFuj7U1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 18 Aug 2011 01:35:25 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="f8igmItKsWs6nM5YanFxAA"; logging-data="22934"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/x20umT7TCaGEgqWfj4CWN" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: <9b2aglFuj7U1@mid.individual.net> Cancel-Lock: sha1:zfWroS7dB1oQ7z9OloS3S56LPRA= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7188 On 8/17/2011 1:02 PM, Robert Klemme wrote: > On 17.08.2011 16:17, Lew wrote: >> Andreas Leitgeb wrote: >>> Arved Sandstrom wrote: >>>> Lew wrote: >>>>> JLS 10: [...] IOW, you never index arrays with a byte, char or >>>>> short. [...] >>>> I'm feeling slow this morning: how do we get problems with >>>> byte->int, short->int, or char->int again? >> >> 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. >> >> One gets into trouble generally in programming when one thinks one >> thing is going on ("index takes a byte") whilst ignoring what's >> really going on (there's a widening conversion involved). You might >> get away with it most of the time, but occasionally such things trip >> you up. > > Why had a nice issue recently which exactly fits this bill: after over > two months of uninterrupted, completely error free production usage the > application suddenly stopped working giving weird error messages (there > was an issue with the error reporting as well, but let's ignore that for > the moment). Turns out this was the setup: > > 1. There was an AtomicInteger initialized at startup with 0 which for > every request coming into that system was incremented in a thread safe > manner (incrementAndGet()). > > 2. The result was used to index into an array with the quite obvious > "arr[n % arr.length]". > > 3. (left as exercise for the reader) > > Anybody who now thinks all is fine should stop coding Java immediately > and go reading the language spec. Yes, but what has this error to do with widening conversions, or conversions of any kind? It's GIGO, pure and simple. -- Eric Sosman esosman@ieee-dot-org.invalid