Java枚举是否被视为原始类型或引用类型?

时间:2022-02-16 16:27:35

If I have an enum object, is it considered a primitive or a reference?

如果我有一个枚举对象,它被认为是原语还是引用?

4 个解决方案

#1


54  

It's a reference type.

这是一种参考类型。

Unlike many languages, in which an enum is a set of integral constansts, Java enums are immutable object instances. You can get the numeric value of an enum instance by calling ordinal().

与许多语言不同,其中枚举是一组完整的constansts,Java枚举是不可变的对象实例。您可以通过调用ordinal()来获取枚举实例的数值。

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);

#2


11  

The way enums work is actually not too different from how they were used before their introduction with Java 5:

枚举的工作方式实际上与它们在引入Java 5之前的使用方式并没有太大差别:

public final class Suit {

public static final Suit CLUBS = new Suit();
public static final Suit DIAMONDS = new Suit();
public static final Suit HEARTS = new Suit();
public static final Suit SPADES = new Suit();

/**
 * Prevent external instantiation.
 */
private Suit() {
    // No implementation
}}

By instantiating the different suits on class loading it is ensured that these will be mutually exclusive and the private constructor ensures that no further instances will be created.

通过在类加载上实例化不同的套装,确保它们是互斥的,私有构造函数确保不会创建更多的实例。

These would be comparable either through == or equals.

这些可以通过==或等于进行比较。

The Java 5 enum works pretty much the same way, but with some necessary features to support serialization etc.

Java 5枚举的工作方式大致相同,但有一些必要的功能可以支持序列化等。

I hope this background sheds some further light.

我希望这个背景能够进一步阐明。

#3


0  

This article essentially shows you how enums are implemented, and as SLaks says, they are references.

本文主要向您展示如何实现枚举,并且正如SLaks所说,它们是引用。

#4


0  

Enums are reference types, in that they can have methods and can be executed from command line as well , if they have main method.

枚举是引用类型,因为它们可以有方法,并且如果它们具有main方法,也可以从命令行执行。

See following "Planet" example from Sun/Oracle

请参阅Sun / Oracle中的以下“Planet”示例

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

#1


54  

It's a reference type.

这是一种参考类型。

Unlike many languages, in which an enum is a set of integral constansts, Java enums are immutable object instances. You can get the numeric value of an enum instance by calling ordinal().

与许多语言不同,其中枚举是一组完整的constansts,Java枚举是不可变的对象实例。您可以通过调用ordinal()来获取枚举实例的数值。

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);

#2


11  

The way enums work is actually not too different from how they were used before their introduction with Java 5:

枚举的工作方式实际上与它们在引入Java 5之前的使用方式并没有太大差别:

public final class Suit {

public static final Suit CLUBS = new Suit();
public static final Suit DIAMONDS = new Suit();
public static final Suit HEARTS = new Suit();
public static final Suit SPADES = new Suit();

/**
 * Prevent external instantiation.
 */
private Suit() {
    // No implementation
}}

By instantiating the different suits on class loading it is ensured that these will be mutually exclusive and the private constructor ensures that no further instances will be created.

通过在类加载上实例化不同的套装,确保它们是互斥的,私有构造函数确保不会创建更多的实例。

These would be comparable either through == or equals.

这些可以通过==或等于进行比较。

The Java 5 enum works pretty much the same way, but with some necessary features to support serialization etc.

Java 5枚举的工作方式大致相同,但有一些必要的功能可以支持序列化等。

I hope this background sheds some further light.

我希望这个背景能够进一步阐明。

#3


0  

This article essentially shows you how enums are implemented, and as SLaks says, they are references.

本文主要向您展示如何实现枚举,并且正如SLaks所说,它们是引用。

#4


0  

Enums are reference types, in that they can have methods and can be executed from command line as well , if they have main method.

枚举是引用类型,因为它们可以有方法,并且如果它们具有main方法,也可以从命令行执行。

See following "Planet" example from Sun/Oracle

请参阅Sun / Oracle中的以下“Planet”示例

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html