Does the first enumerator have default value of 0 like in C++?

Discussion in 'Java' started by Thomas Hawtin, May 15, 2006.

  1. iherage wrote:
    > In C++, by default, the first enumerator has a value of 0, and each
    > successive enumerator is one larger than the value of the previous one,
    > unless you explicitly specify a value for a particular enumerator.
    >
    > What about in Java ( JDK 5.0)? Can I assign a value to the particular
    > enumerator?
    > I have googled for it and found nothing. Does anyone know?


    http://download.java.net/jdk6/docs/api/java/lang/Enum.html#ordinal()

    You can also, of course, add methods to your enum to return whatever
    value you like.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, May 15, 2006
    #1
    1. Advertising

  2. Thomas Hawtin

    iherage Guest

    In C++, by default, the first enumerator has a value of 0, and each
    successive enumerator is one larger than the value of the previous one,
    unless you explicitly specify a value for a particular enumerator.

    What about in Java ( JDK 5.0)? Can I assign a value to the particular
    enumerator?
    I have googled for it and found nothing. Does anyone know?
    Thank you.
    iherage, May 15, 2006
    #2
    1. Advertising

  3. Thomas Hawtin

    Chris Uppal Guest

    Chris Uppal, May 15, 2006
    #3
  4. Thomas Hawtin

    Guest

    iherage wrote:
    > In C++, by default, the first enumerator has a value of 0, and each
    > successive enumerator is one larger than the value of the previous one,
    > unless you explicitly specify a value for a particular enumerator.


    > What about in Java ( JDK 5.0)?


    Simple. You write code like that in my projects and I'll break your
    fingers.
    , May 15, 2006
    #4
  5. wrote:
    > iherage wrote:
    >> In C++, by default, the first enumerator has a value of 0, and each
    >> successive enumerator is one larger than the value of the previous one,
    >> unless you explicitly specify a value for a particular enumerator.

    >
    >> What about in Java ( JDK 5.0)?

    >
    > Simple. You write code like that in my projects and I'll break your
    > fingers.


    Seems a little harsh. Problems at home?

    Seriously, you have a beef with sequences of named, consecutive integers?
    Jeffrey Schwab, May 15, 2006
    #5
  6. Thomas Hawtin

    Chris Uppal Guest

    Jeffrey Schwab wrote:

    > Seriously, you have a beef with sequences of named, consecutive integers?


    I doubt it, but where is the connection between integers and instances of
    (subtypes of) java.lang.Enum ?

    Unlike the case in C and C++, Java's enums are not fancy syntax for a list of
    integers -- they are /objects/. They can have integers /associated/ with them,
    sure. And floats. And Strings...

    I suspect the OP hasn't realised this, and that dingbat's post was a gentle
    hint to the same effect ;-)

    -- chris
    Chris Uppal, May 15, 2006
    #6
  7. Chris Uppal wrote:
    > Jeffrey Schwab wrote:
    >
    >> Seriously, you have a beef with sequences of named, consecutive integers?

    >
    > I doubt it, but where is the connection between integers and instances of
    > (subtypes of) java.lang.Enum ?
    >
    > Unlike the case in C and C++, Java's enums are not fancy syntax for a list of
    > integers -- they are /objects/. They can have integers /associated/ with them,
    > sure. And floats. And Strings...
    >
    > I suspect the OP hasn't realised this, and that dingbat's post was a gentle
    > hint to the same effect ;-)


    That makes sense. It also explains the exasperation. Thanks.

    I have not had much occasion to use Java Enums yet, but they do look
    promising. Just for the record, though:

    - C++ enums are first-class types. They're not just integers.
    This is in contrast to C.

    - Mr. Bat's issue may have been with the implicit default value
    of zero. It seems appropriate, though, since several other
    Java types' default values are also zero.
    Jeffrey Schwab, May 15, 2006
    #7
  8. Thomas Hawtin

    Roedy Green Guest

    On 15 May 2006 04:54:09 -0700, "iherage" <> wrote,
    quoted or indirectly quoted someone who said :

    >What about in Java ( JDK 5.0)? Can I assign a value to the particular
    >enumerator?
    > I have googled for it and found nothing. Does anyone know?
    > Thank you.


    A Java automatically assigns integers starting at 0. You of course are
    free to invent some other method of your own that assigns something
    else, e.g. a non-consecutive it, a char, a short string, a
    description...

    See http://mindprod.com/jgloss/enum.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, May 20, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmltIEhlYXZleQ==?=

    Char Enumerator

    =?Utf-8?B?SmltIEhlYXZleQ==?=, Jul 16, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    383
    bruce barker
    Jul 16, 2004
  2. Angelos Karantzalis

    DataGrid: Bind to an Enumerator of object[]'s

    Angelos Karantzalis, Nov 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    441
  3. Oliver Hopton

    Collection class bound to DataGrid doesn't use my custom enumerator!

    Oliver Hopton, Oct 1, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    123
    Alan Green
    Oct 29, 2003
  4. Kristof Bastiaensen

    RCR: More enumerator functionality

    Kristof Bastiaensen, May 17, 2004, in forum: Ruby
    Replies:
    10
    Views:
    294
    Kristof Bastiaensen
    May 19, 2004
  5. Christopher Dicely

    Enumerator#each return value is surprising

    Christopher Dicely, Sep 3, 2010, in forum: Ruby
    Replies:
    7
    Views:
    217
    Christopher Dicely
    Sep 7, 2010
Loading...

Share This Page