简体   繁体   English

存储在Scala中的AnyVal子类型在哪里?

[英]Where are AnyVal subtypes stored in Scala?

In Java, within a method when we create primitives they are stored in stack memory, while objects instantiated (like using new ) are put on heap. 在Java中,在我们创建基元的方法中,它们存储在堆栈内存中,而实例化的对象(如使用new )则放在堆上。 In scala, AnyVal 's subtypes(like Int ) are immutable value instances that can't be instantiated. 在scala中, AnyVal的子类型(如Int )是无法实例化的不可变值实例。 So if I create an Int in a Scala method, does it go to heap or stack. 因此,如果我在Scala方法中创建一个Int ,它会转到堆还是堆栈。 I am asking it because in Scala Int is an object. 我问它,因为在Scala Int是一个对象。

The Scala Language Specification does not say anything about how memory is organized. Scala语言规范没有说明内存的组织方式。 Every implementation is free to organize its memory however it wants. 每个实现都可以随意组织其内存,但它需要。

By the way, your statements about Java are wrong: 顺便说一下,关于Java的陈述是错误的:

In Java, within a method when we create primitives they are stored in stack memory, 在Java中,在我们创建基元的方法中,它们存储在堆栈内存中,

There is nothing in the Java Language Specification which says that. Java语言规范中没有任何内容可以说明这一点。 And in fact, for many implementations, this isn't true. 事实上,对于许多实现,情况并非如此。 For example, Oracle JDK's implementation will try to store primitives in registers, or elide them completely, instead of storing them on the stack. 例如,Oracle JDK的实现将尝试将基元存储在寄存器中,或者完全忽略它们,而不是将它们存储在堆栈中。

while objects instantiated (like using 'new') are put on heap. 实例化对象(如使用'new')放在堆上。

Again, there is nothing in the Java Language Specification which says that. 同样,Java语言规范中没有任何内容可以说明这一点。 And again, for example in Oracle's implementation, this is not necessarily true. 而且,例如在Oracle的实现中,这不一定是真的。 Oracle's optimizing compilers will perform Escape Analysis and allocate objects on the stack whenever possible, ie when the compiler can prove that the reference doesn't escape the scope. Oracle的优化编译器将执行Escape Analysis并尽可能在堆栈上分配对象,即编译器可以证明引用不会超出范围。 Azul's implementation always allocates objects on the stack (unless it can prove that the reference will definitely escape the local scope), and performs Escape Detection, ie when it detects a reference escaping the local scope, it will move the object from the stack to the heap. Azul的实现总是在堆栈上分配对象(除非它可以证明引用肯定会逃避本地范围),并执行转义检测,即当它检测到转义本地范围的引用时,它会将对象从堆栈移动到堆。

In fact, you can implement Java without a stack at all, and be perfectly compliant with the Java Language Specification. 实际上,您可以在没有堆栈的情况下实现Java,并且完全符合Java语言规范。

In scala, AnyVal's subtypes(like Int) are immutable value instances that can't be instantiated. 在scala中,AnyVal的子类型(如Int)是无法实例化的不可变值实例。 So if I create an Int in a Scala method, does it go to heap or stack. 因此,如果我在Scala方法中创建一个Int,它会转到堆还是堆栈。

That depends on the implementation, the version, the specific circumstances, and many other things. 这取决于实现,版本,具体情况和许多其他事情。 The Scala Language Specification doesn't say one way or the other, in fact, it doesn't say anything about how to memory, let alone about stack and heap. Scala语言规范不说的一种方式或其他,其实它并没有说如何存储任何东西 ,更不用说有栈和堆。

I am asking it because in Scala Int is an object. 我问它,因为在Scala Int中是一个对象。

But again, that doesn't say anything about how it is implemented. 但同样,这并没有说明它是如何实施的。 Scala-JVM, for example, implements scala.Int as a JVM primitive, and then simply "fakes" the methods. 例如,Scala-JVM将scala.Int实现为JVM原语,然后简单地“伪造”这些方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM