Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
iherage - 15 May 2006 12:54 GMT
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.
Thomas Hawtin - 15 May 2006 12:22 GMT
> 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,
[quoted text clipped - 3 lines]
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Chris Uppal - 15 May 2006 13:23 GMT
> http://download.java.net/jdk6/docs/api/java/lang/Enum.html#ordinal()

And pay particular attention to the sentence:

  "Most programmers will have no use for this method."

   -- chris
dingbat@codesmiths.com - 15 May 2006 14:03 GMT
> 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.
Jeffrey Schwab - 15 May 2006 14:05 GMT
>> 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,
[quoted text clipped - 4 lines]
> 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?
Chris Uppal - 15 May 2006 14:39 GMT
> 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
Jeffrey Schwab - 15 May 2006 14:47 GMT
>> Seriously, you have a beef with sequences of named, consecutive integers?
>
[quoted text clipped - 7 lines]
> 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.
Roedy Green - 20 May 2006 19:26 GMT
>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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.