Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx01.iad01.newshosting.com!newshosting.com!69.16.185.11.MISMATCH!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe06.iad.POSTED!8ad76e89!not-for-mail From: Arved Sandstrom User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.18) Gecko/20110617 Lightning/1.0b2 Thunderbird/3.1.11 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Using Enumerated Types as Array Indexes References: <4e4b0d81$0$314$14726298@news.sunsite.dk> <4e4b1fe4$0$314$14726298@news.sunsite.dk> <6d418cda-dab3-43df-a9ec-293b43f2bbd8@glegroupsg2000goo.googlegroups.com> In-Reply-To: <6d418cda-dab3-43df-a9ec-293b43f2bbd8@glegroupsg2000goo.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Lines: 27 Message-ID: X-Complaints-To: abuse@newsgroups-download.com NNTP-Posting-Date: Wed, 17 Aug 2011 09:31:07 UTC Organization: Public Usenet Newsgroup Access Date: Wed, 17 Aug 2011 06:31:06 -0300 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7156 On 11-08-17 12:45 AM, Lew wrote: > On Tuesday, August 16, 2011 8:20:05 PM UTC-7, markspace wrote: >> On 8/16/2011 6:56 PM, Arne Vajh�j wrote: >>> byte/short/int/long integer types >> >> OK, you mean for indexes? char works too, and long really doesn't -- it >> has to be cast to an int. For longs, Java recognizes the loss of >> precision using long and requires that you acknowledge that the upper >> bits will simply be truncated. > > JLS 10: > "The variables contained in an array have no names; instead they are referenced by array access expressions that use nonnegative integer index values." > > IOW, you never index arrays with a byte, char or short. If you try, you get a widening conversion first, and if that results in a negative int value, you got problems. > I'm feeling slow this morning: how do we get problems with byte->int, short->int, or char->int again? These are all integral widening primitive conversions, respectively byte->int: 8-bit signed 2's complement (2C) -> 32-bit signed 2C, short->int: 16-bit signed 2C -> 32-bit signed 2C char->int: 16-bit unsigned -> 32-bit signed 2C According to common sense and the JLS, all three of these conversions _exactly_ preserve the original value. How could they not? AHS