Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7257

Re: Using Enumerated Types as Array Indexes

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Using Enumerated Types as Array Indexes
Date Sat, 20 Aug 2011 00:26:04 +0200
Lines 62
Message-ID <9b8687Fa7tU1@mid.individual.net> (permalink)
References <b9ad6662-3240-4754-86a0-b715e8cdbc48@z7g2000vbp.googlegroups.com> <4e4b0d81$0$314$14726298@news.sunsite.dk> <j2f3ci$1lh$1@dont-email.me> <4e4b1fe4$0$314$14726298@news.sunsite.dk> <j2fc1g$b9j$1@dont-email.me> <6d418cda-dab3-43df-a9ec-293b43f2bbd8@glegroupsg2000goo.googlegroups.com> <vTL2q.38278$tp.27319@newsfe06.iad> <slrnj4ngfi.6gl.avl@gamma.logic.tuwien.ac.at> <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> <9b74tiFf06U1@mid.individual.net> <slrnj4tgp9.6gl.avl@gamma.logic.tuwien.ac.at> <bdB3q.135776$8G4.24004@newsfe17.iad>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net yHD1NqJj+uEoPddhbu/q+wKzLt2OKj32MRxnn7x9isQhZd2as=
Cancel-Lock sha1:NoeJT6+m1HqgXEEJ64l+fZfwdnY=
User-Agent Mozilla/5.0 (Windows NT 6.0; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0
In-Reply-To <bdB3q.135776$8G4.24004@newsfe17.iad>
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7257

Show key headers only | View raw


On 20.08.2011 00:12, Arved Sandstrom wrote:
> On 11-08-19 05:10 PM, Andreas Leitgeb wrote:
>> Robert Klemme<shortcutter@googlemail.com>  wrote:
>>> byte b = 0xA0; // won't compile
>>
>> Actually, it *does* compile due to some special rule in
>> the JLS w.r.t initializers.

Andreas, which rule and which compiler?

>> That, however doesn't impact the the point you made.
>> Had you just written it thusly:
>>    byte b; b = 0xA0; // won't compile
>> it would have been correct altogether. :-)
>>
> Not with JDK 1.6 it doesn't compile. I usually test these things if I'm
> talking about them, mainly to avoid looking like a complete dilettante
> in the case that I am overlooking something basic. This scenario, and
> many permutations, I tried.

Same here: Eclipse's compiler refuses both with "Type mismatch: cannot 
convert from int to byte".

> "possible loss of precision" errors are reported if you try to compile
> with outside range integer literals assigned to byte, short or char
> variables, without casts. It doesn't matter if you try
>
> byte b = 128;
>
> or
>
> byte b; b = 0x80;
>
> either will error with a "possible loss of precision" message.

Also, I cannot see any reason why these two situations should be treated 
differently: the loss of precision is there regardless.

> Since it's routine and OK to create values of type byte, short, int and
> long from int literals (and longs from long literals also), and a cast
> is redundant for such an integer literal assignment, _and_ because a
> cast masks the error, I would suggest that assignments with casts such as
>
> byte b = (byte)0xA0;
>
> or
>
> byte b = (byte)intVariable;
>
> be strenuously avoided.

That's certainly a good rule of thumb which helps keeping one out of 
trouble.  Although it sometimes seems reasonable to use values in the 
range [0x80, 0xFF] e.g. when defining bit masks.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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