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

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Using Enumerated Types as Array Indexes
Date 2011-08-20 00:26 +0200
Message-ID <9b8687Fa7tU1@mid.individual.net> (permalink)
References (11 earlier) <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>

Show all headers | 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