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

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

  1. 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
     
    Thomas Hawtin, May 15, 2006
    #1
    1. Advertisements

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

  3. Thomas Hawtin

    Chris Uppal Guest

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

    dingbat Guest

    Simple. You write code like that in my projects and I'll break your
    fingers.
     
    dingbat, May 15, 2006
    #4
  5. 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

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

    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
     
    Roedy Green, May 20, 2006
    #8
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.