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

T

Thomas Hawtin

iherage said:
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
 
I

iherage

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

dingbat

iherage said:
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.
 
J

Jeffrey Schwab

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?
 
C

Chris Uppal

Jeffrey said:
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
 
J

Jeffrey Schwab

Chris said:
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.
 
R

Roedy Green

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top