Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Using Enumerated Types as Array Indexes

Reply
Thread Tools

Using Enumerated Types as Array Indexes

 
 
Arne Vajh°j
Guest
Posts: n/a
 
      08-18-2011
On 8/17/2011 9:34 PM, Arne Vajh°j wrote:
> On 8/16/2011 11:20 PM, markspace wrote:
>> On 8/16/2011 6:56 PM, Arne Vajh°j wrote:
>>> byte/short/int/long integer types

>>
>> OK, you mean for indexes?

>
> No, in general.


The original post was:

# In Java array indexes are
# int and you have byte/short/int/long types and that is it.

an it should have been:

# In Java array indexes are
# int and you have byte/short/int/long integer types and that is it.

Arne



 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      08-18-2011
On 8/17/2011 7:09 PM, markspace wrote:
> On 8/17/2011 10:02 AM, Robert Klemme wrote:
>> 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()).

>
>
> I'd be shocked if an int actually overflowed when counting requests like
> this, but that's all I can think of. 2^31 is such a huge number, I
> didn't think you could get there in practical cases.


One of the most important rules in computing are:

http://en.wikipedia.org/wiki/Murphys_law

Arne
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-18-2011
On 8/17/2011 7:09 PM, markspace wrote:
> On 8/17/2011 10:02 AM, Robert Klemme wrote:
>
>> 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()).

>
>
> I'd be shocked if an int actually overflowed when counting requests like
> this, but that's all I can think of. 2^31 is such a huge number, I
> didn't think you could get there in practical cases.


He *did* write "two months of uninterrupted [...] usage." Two
months ~= 60 days ~= 5 megaseconds, so a 2G counter overflows if it
increments at ~400 Hz or more, give or take a few backs of envelopes.

> Assuming that the AtomicInteger did overflow, then n % y produces
> negative numbers if n is negative, a clear AIOOB.
>
> Try : arr[ Math.abs( n ) % arr.length ];


Bzzzzt! Thank you for playing, go back to the rear of the line.
While awaiting your next opportunity, ponder: What is the sign of
Math.abs(Integer.MIN_VALUE)?

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      08-18-2011
On 8/17/2011 8:29 AM, David Lamb wrote:
> On 16/08/2011 8:38 PM, Arne Vajh°j wrote:
>> On 8/16/2011 10:53 AM, KevinSimonson wrote:
>>> Java is a pretty handy language in its own right. But in Ada one
>>> could define arrays to be indexed by enumerated types. Can Java do
>>> that? If not, why not?

>>
>> 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.

>
> Sure -- but if Enums had been in the language from the beginning, then
> it might have occurred to people that array indexes could be "any type
> with a method 'int ordinal()'" (with the appropriate caveat about being
> in range 0..length-1)


Non int array indexes exists in many languages before Java, so they
knew, but decided to make it simple.

Arne

 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      08-18-2011
On 8/17/2011 4:24 AM, Niklas Holsti wrote:
> KevinSimonson wrote:
>> Ada, the programming language mandated for the military for a time,
>> was a wonderful language that didn't deserve to die. But it did,

>
> No it didn't. The Ada language has been extended and updated regularly;
> a new standard is due to appear in 2012. Several compilers are
> available, one of the best being gnat, which is a part of gcc and is
> freely available on many platforms. See libre.adacore.com and
> http://en.wikipedia.org/wiki/Ada_(programming_language).
>
>> and now Java is alive and well

>
> Agreed.
>
>> and nobody seems to be doing anything at all with Ada.

>
> The number of people working with Ada is certainly smaller than for
> Java, but far from zero. Even the USENET group comp.lang.ada is quite
> active.


Ada is not completely dead.

But its usage has declined to a small fraction of what it once was.

If you look for Ada jobs at job sites there are very few.

A shame that it ended up so, but it did.

Arne


 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      08-18-2011
On 8/17/2011 6:46 PM, Arne Vajh°j wrote:
> an it should have been:
>
> # In Java array indexes are
> # int and you have byte/short/int/long integer types and that is it.



OK, you still left char out. char is an integer type. And I personally
don't feel that long is correct -- it's an integer type but it has
potential loss of precision. Normally you should use int, since that
has all the precision you will need.

That's all I'm saying.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-18-2011
On Tue, 16 Aug 2011 07:53:38 -0700 (PDT), KevinSimonson
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>Java is a pretty handy language in its own right. But in Ada one
>could define arrays to be indexed by enumerated types. Can Java do
>that? If not, why not?


because you can use a[e.ordinal()]

The general rule has been any language feature that can be kludged
with sufficient verbiage is not considered for inclusion.

The focus was in the early days completely on the JVM. The language
itself was considered a kludge for generating byte code.

I got a chance to talk at length with Bill Joy one of the designers of
Java. One of the thing he explained that various licensees of the JVM
would revolt if there were too many changes made for a new version of
Java. This lead the designers to use implementations that could be
handled with whatever was already present in the JVM.

In Java 7 where was some sugar: underscores in literals, binary
literals, string case labels, so perhaps we could see enumerated type
arrays and hash arrays some day.
--
Roedy Green Canadian Mind Products
http://mindprod.com
Most of computer code is for telling the computer
what do if some very particular thing goes wrong.
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-18-2011
On 18.08.2011 03:34, Eric Sosman wrote:
> On 8/17/2011 1:02 PM, Robert Klemme wrote:
>> On 17.08.2011 16:17, Lew wrote:
>>> Andreas Leitgeb wrote:
>>>> Arved Sandstrom<(E-Mail Removed)> 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.


It has to do with "One gets into trouble generally in programming when
one thinks one thing is going on [...] whilst ignoring what's really
going on [...]." And of course it has to do with a specialty of int
math in Java which is probably considered too rarely.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
Niklas Holsti
Guest
Posts: n/a
 
      08-18-2011
Arne Vajh°j wrote:
> On 8/17/2011 4:24 AM, Niklas Holsti wrote:
>> KevinSimonson wrote:
>>> Ada, the programming language mandated for the military for a time,
>>> was a wonderful language that didn't deserve to die. But it did,

>>
>> No it didn't. The Ada language has been extended and updated regularly;
>> a new standard is due to appear in 2012. Several compilers are
>> available, one of the best being gnat, which is a part of gcc and is
>> freely available on many platforms. See libre.adacore.com and
>> http://en.wikipedia.org/wiki/Ada_(programming_language).
>>
>>> and now Java is alive and well

>>
>> Agreed.
>>
>>> and nobody seems to be doing anything at all with Ada.

>>
>> The number of people working with Ada is certainly smaller than for
>> Java, but far from zero. Even the USENET group comp.lang.ada is quite
>> active.

>
> Ada is not completely dead.
>
> But its usage has declined to a small fraction of what it once was.


True when considering the proportion of Ada usage versus all other
programming languages, because of the huge expansion in internet and web
applications that rarely use Ada. But I doubt that Ada usage in absolute
terms has declined so radically.

It is difficult to find reliable figures on language usage, but Ada has
been in the "top 20" category on the TIOBE Programming Community index
for several years, with ups and downs in its rating. See
http://www.tiobe.com/index.php/conte...pci/index.html.

Whatever the current level of usage, my main point is that Ada is
*alive* in the sense that good compilers are readily available and the
language is steadily evolving to provide modern features while remaining
true to its original design goals.

(Apologies for promoting Ada in a Java group, but I felt I had to
protest against the OP's claims of deadness, even if these claims were
incidental to the OP's question.)

--
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
. @ .
 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      08-18-2011
On 11-08-17 11:17 AM, Lew wrote:
> Andreas Leitgeb wrote:
>> Arved Sandstrom <(E-Mail Removed)> 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 think about it imprecisely? Tell us the advantage of that, please.
>
>> array-indexing takes integers. That you can also throw byte/short/char
>> values at them without cast doesn't mean that arrays "take" those, but
>> only that such values get automatically converted to int before being
>> taken by the array for indexing.
>>
>> An example for something really "taking" byte-values:
>> foo(byte b) { ... }

>

I'll try my answer again, it seems not to have hit the airwaves.

I don't see my imprecision: the JLS very clearly says that integral
widening primitive conversions result in *no* loss of information; this
is just common sense when you consider what's going on with char->int,
byte-int and short->int.

I reiterate: the _JLS_ says that there is *no* loss of information. I
don't see why you believe the JLS is wrong. Exactly how are you getting
into trouble with integral widening primitive conversions?

Furthermore, your assertion about indexing into an array with (byte)0x0A
(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.

AHS
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Name Enumerated Types and Structs Immortal Nephi C++ 4 08-24-2010 05:20 PM
Assigning Values to Enumerated Types Kevin Neilson VHDL 9 05-09-2008 05:19 PM
size of a class having enumerated data types? ypjofficial@indiatimes.com C++ 8 01-23-2006 12:29 PM
Progressive restriction on enumerated types in Owl David R. Throop XML 0 05-05-2005 11:39 PM
strange recursivion with enumerated types - how is this possible? rfractal30 Java 6 02-25-2005 02:38 AM



Advertisments