11

I am learning Enums in java I like to know what are the major differences of Enum in java and C++. Thanks

zengr
  • 38,346
  • 37
  • 130
  • 192
giri
  • 26,773
  • 63
  • 143
  • 176

6 Answers6

19

In c++ an enum is just a list of integer values. In java an enum is a class which extends Enum and is more a nice way to write:

class MyEnum extends Enum<MyEnum>
{
public final static MyEnum VE01 = new MyEnum();
public final static MyEnum VE02 = new MyEnum();
}

as enum:

enum MyEnum
{
 VE01,VE02;
}

For the Methods of enum see this. Since a java enum is an object it supports everything a normal java object does. as giving them values or functions:

enum Binary{
 ZERO(0),ONE(1);
 Binary(int i)
 {
   value = i;
 }
 public final int value;
}

a nice one is anonymous classes:

enum State{
StateA{
   public State doSomething()
   {
     //state code here
     return StateB;
    }
},
StateB{
    public State doSomething()
    {
       return StateA;
    }
};
public abstract State doSomething();
}
josefx
  • 15,506
  • 6
  • 38
  • 63
13

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.

More information available on Wikipedia.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • Note: C++ will only implicitly convert enum values to their integral equivalent when there is no class declaration preceding it. – Moshe Rabaev Aug 11 '16 at 03:02
4

Enums in C/C++ are plain Integers.

Enums in Java are objects - they can have methods (with different behavior from one enum instance to the other). Moreoever, the enum class supplies methods which allows iteration over all enum instances and lookup of an enum instance.

C++:

typedef enum { Red, Yellow, Green } Colors;

void canCrossIntersection(Colors c) {
   return c == Green;
}

Java:

public enum Colors {
  RED(false),
  Yellow(false),
  Green(true) 
  ;

  Color(boolean b) { this.b = b; }
  private boolean b;

  public boolean canCrossIntersection() { return b; }
}
Itay Maman
  • 30,277
  • 10
  • 88
  • 118
  • 1
    An important effect of enums in Java being objects is that they are truely type-safe: You can only pass valid values to methods expecting an enum value. In C you can easily pass `253`, which may or may not map to a valid enum value. – Joachim Sauer Jan 17 '10 at 10:44
  • 1
    Joachim Sauer, your point is correct but irrelevant, the question is about C++ where enums *are* type-safe. – avakar Jan 17 '10 at 16:02
1

In Java you can even emulated extensible enums by letting them implement the same interface and then add all of their values to some sort of collection by calling their values() method.

helpermethod
  • 59,493
  • 71
  • 188
  • 276
1

For a semi-answer... C++0x Strongly Typed Enums bring many of the benefits of Java Enums to C++: strong typing, scope and so forth. If your compiler supports C++0x Strongly Typed Enums, you should consider using them.

Reference: http://www2.research.att.com/~bs/C++0xFAQ.html#enum

Dragontamer5788
  • 1,957
  • 1
  • 12
  • 20
0

A great feature of Java Enums which lacks in C++ is the name() method. This way you can get the Enum value name (as written in the enum definition) without any extra line of code in definition. For example:

enum Type {T1, T2, T3}
Type t = Type.T1;   
System.out.println(t.name()); // prints T1
Amir Moghimi
  • 1,391
  • 1
  • 11
  • 19