By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,330 Members | 1,572 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,330 IT Pros & Developers. It's quick & easy.

enumeration

P: n/a
Hi,

Java doe not have the data type enum. I am starting to learn it in Java and
find it confusing.

1) Whats the point in having enum? Java does have it, so probably there is a
way around it.
2) Can someone show a SIMPLE code of how enum works?
WD

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Web Developer" <no****@hotmail.com> wrote...
Java doe not have the data type enum. I am starting to learn it in Java and find it confusing.
Find what confusing? Java? No doubt.
1) Whats the point in having enum? Java does have it, so probably there is a way around it.
Enums are "named constants". Each enum is its own type. Using enums
promotes strong typing (no, I don't mean hitting keys with your fingers).
2) Can someone show a SIMPLE code of how enum works?


#include <iostream>

enum Protocol { FTP = 21, HTTP = 80, HTTPS = 143 };

void foo(int)
{
std::cout << "foo(int) called\n";
}

void foo(Protocol)
{
std::cout << "foo(Protocol) called\n";
}

int main()
{
foo(42);
foo(HTTPS); // _not_ the same as foo(143)
}

Victor
Jul 19 '05 #2

P: n/a

Web Developer wrote:

Hi,

Java doe not have the data type enum. I am starting to learn it in Java and
find it confusing.

1) Whats the point in having enum? Java does have it, so probably there is a
way around it.
No.
2) Can someone show a SIMPLE code of how enum works?


Yes.

#include <iostream>

enum vote { no, yes };

std::ostream& operator <<(std::ostream& os, vote v) {
return os << (!v ? "no" : "yes");
}

vote operator or(vote lhs, vote rhs) {
return vote(lhs | rhs);
}

int main() {
std::cout << (yes or no) << std::endl;
}

Well, try also this:

#include <cstdio>
#include <climits>
//#include <iostream>

int main() {
enum { iMIN = LONG_MIN, uMAX = ULONG_MAX } ld = iMIN, lu = uMAX;
printf("%ld ** %lu\n", LONG_MIN, ULONG_MAX);
printf("%ld ** %lu\n", static_cast<signed long>(ld),
static_cast<unsigned long>(lu));
// std::cout << ld << " ** " << lu << std::endl;
}

regards,
alexander.
Jul 19 '05 #3

P: n/a
Web Developer wrote:
Hi,

Java doe not have the data type enum. I am starting to learn it in Java and
find it confusing.
Well, this is a C++ 'group, not a Java 'group.
1) Whats the point in having enum? Java does have it, so probably there is a
way around it.
But you just said "Java doe not have" it.
2) Can someone show a SIMPLE code of how enum works?


If there's any C++ question buried in this, please clarify what you're
asking for. Otherwise, try asking in comp.lang.java.help.

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

Jul 19 '05 #4

P: n/a
Dear mate,

There's a little survey about the general uses of 'enum's started by
me in comp.lang.c++ under the title "C++ programmers! How do you use
your 'enum's ?", and in comp.lang.c under the title "C programmers!
How do you use your 'enum's ?".
You may find these two threads useful.

Regards,
//rk
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.