Java 的枚举...它们是在哪里创建的?
由于 C# 中的枚举位于堆栈上,我想知道 Java 中的枚举是在哪里创建的。在堆栈上?在堆上?在其他某个神秘的地方?
C# 中的枚举比 Java 中的枚举更原始,这也许可以解释为什么它们在堆栈上创建......
它们在哪里?我找不到他们!
谢谢
Since enum in C# are on the stack, I was wondering where enum, in Java, where created. On the stack? On the heap? In some mysterious other place?
Enumeration in C# are more primitive than those in Java, this might explain why they are created on the stack...
Where are they? I can't find them!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Java 中的枚举也是对象:例如,枚举可以具有实例变量/方法/构造函数并实现接口。所有这些让我觉得它们就像 jvm 处理其他对象一样。
Enums in Java are also objects: for example, enums can have instance variables/methods/constructors and implement interfaces. All this makes me think they're handled just like other objects by jvm.
由于 Java 枚举 扩展
java.lang.Enum< /code>,它们像所有其他 Java 对象一样在堆上创建。
Since Java enums extend
java.lang.Enum
, they are created on the heap like all other Java objects.枚举是Java中的对象,因此它们位于堆上。然而,对于每种类型来说,它们的数量都是固定的。 客户端代码正在处理对这些枚举对象的引用,因此实际上不会在堆上创建任何内容。从规范的角度来看:局部变量引用位于堆栈上;对象字段引用位于堆上。
Enums are objects in Java, so they are on the heap. However, for each type there is only a fixed number of them. Client code is dealing with references to these enum objects, so doesn't actually create anything on the heap. As ever from a specification point of view: local variable references are on the stack; object field references are on the heap.
它们是对象,就像任何其他对象一样,因此枚举本身位于堆上。保存对枚举的引用的变量如果是函数变量,则可能位于堆栈上;如果它是对象的成员,则可能位于其他对象内的堆上。
They are objects, just like any other object, so the enum itself is on the heap. A variable that holds a reference to an enum may be on the stack if it is a function variable, or it may be on the heap inside some other object if it is a member of an object.