Are Java enums considered primitive or reference types?

It’s a reference type. Java primitives are boolean byte short char int long float double.

You can get the enumeration constant’s value by calling ordinal(), which is used by EnumSet and EnumMap iterator and “traverses the elements in their natural order (the order in which the enum constants are declared)”

You can even add your own members to the enum class, like this:

public enum Operation {
  PLUS   { double eval(double x, double y) { return x + y; } },
  MINUS  { double eval(double x, double y) { return x - y; } },
  TIMES  { double eval(double x, double y) { return x * y; } },
  DIVIDE { double eval(double x, double y) { return x / y; } };

  // Do arithmetic op represented by this constant
  abstract double eval(double x, double y);
}
//Elsewhere:
Operation op = Operation.PLUS;
double two = op.eval(1, 1);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)