Sech
Sech

Reputation: 167

Difference of enum in C and Java

I would like to know what is the difference of Enum between C and Java. And is there a way to "translate" a C enum into a Java enum ? That's because I have an enum written in C on a robot containing types messages and I need to have the same in Java so that it could read data i send from an app.

Upvotes: 2

Views: 2122

Answers (3)

Maxim G
Maxim G

Reputation: 1489

Opposed to C, which uses enum as a set of named constants, Java implements it as a class. And if you are concerned about performance do not use it in Android (The Price of enums).

Upvotes: 0

Alex
Alex

Reputation: 616

Java Enum is different from C Enum, because in Java Enum is a kind of class, when C\C++ enums is constant, that can be used in indexed expressions and as operands. Also, C\C++ enumerations provide an alternative to the #define preprocessor directive. P.S.: To get more information, read Effective Java (http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683) 6th chapter (item 30-37) about it, the usage and the differences with C\C++ enums.

Upvotes: 0

Sanjit Kumar Mishra
Sanjit Kumar Mishra

Reputation: 1209

In C, an enumeration is just a set of named, integral constants. In Java, an enumeration is more like a named instance of a class. You have the ability to customize the members available on the enumeration.

Also, C will implicitly convert enum values to their integral equivalent, whereas the conversion must be explicit in Java.

For More Information Visit: Enum In C & Enum In Java

Upvotes: 3

Related Questions