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.
-
Include a [MCVE] of the enum in question. – t0mm13b May 20 '16 at 11:43
-
2Possible duplicate of [Difference of Enum between java and C++?](http://stackoverflow.com/questions/2080681/difference-of-enum-between-java-and-c) – Harshad Pansuriya May 20 '16 at 11:45
-
2C enums are more like integers ... in your shoes, i would use `final static int` instead enum on the java side – Selvin May 20 '16 at 11:47
-
@Ironman the question has C as the tag. – t0mm13b May 20 '16 at 11:47
-
Asking for difference between **enum** in C and Java is almost like difference between C and Java, they really do not have anything in common except the keyword and curly braces. But yes, use `(public) final static int`s instead. – Antti Haapala -- Слава Україні May 20 '16 at 11:53
-
I wouldn't use `static final int` in many cases. Enums are a typed group of constants, static fields aren't. http://stackoverflow.com/questions/9969690/whats-the-advantage-of-a-java-enum-versus-a-class-with-public-static-final-fiel/9969723#9969723 – zapl May 20 '16 at 12:12
-
@zapl it's for C<=>Java comunication ... so it would be prolly serialized to integer ... [there is no guard on the C side](http://ideone.com/hGMrkw) so why bother about java side ... if you need groupinig you can always use interface ... `public interface b { static final a = 1, static final b = 2, static final c = 4, static final d = 8 }` – Selvin May 20 '16 at 15:02
-
@Selvin Depends... I'm not saying you should never use them but what do you do with that invalid value in Java? You probably got to write code to detect that and act somehow. So you might as well deserialize it to a useful type (maybe have an enum element for invalid values) which also prevents that you send nonsense back. One side that can't have nice things is enough :) – zapl May 20 '16 at 15:21
3 Answers
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

- 1,153
- 13
- 32
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.

- 617
- 4
- 15
-
Your answer is only really an answer when you have access to the book. – Andreas Wilkes May 20 '16 at 12:06
-
Well, that could be easily fixed by using a URL that points to one of the many many sites that offer the complete content of that book. – GhostCat May 20 '16 at 12:14
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).

- 1,479
- 1
- 15
- 23