score:8
anyval
subclasses are stack allocated, where possible. the exception happens with newer user-created classes that extend anyval
on 2.10.0, if the object escapes the scope.
any
and anyval
will be stored on the heap... unless you @specialized
.
score:3
i'm also new to scala, but afaik, a scala variable can not contain an actual object. it can at most contain a reference to an object. (you get a reference from new
and there's no dereference operator to follow that reference to an object (such as *
in c++ for instance).)
in other words, all non-primitive values live on the heap. (just as in java.)
score:3
the jvm does not support reification of generics and provides no means of having a primitive super type for all primitive types. thus a field or parameter of type anyval
will always be of type java.lang.object
in the byte code and boxing/unboxing will be performed.
this does not necessarily mean that the value is stored on the heap though as the jvm may perform certain optimizations. you have to still expect a runtime penalty though.
score:14
a.scala:
class a {
val a: anyval = 1
val b: int = 1
}
scalac a.scala
javap -c a
public class a extends java.lang.object implements scala.scalaobject{
public java.lang.object a();
code:
0: aload_0
1: getfield #13; //field a:ljava/lang/object;
4: areturn
public int b();
code:
0: aload_0
1: getfield #16; //field b:i
4: ireturn
public a();
code:
0: aload_0
1: invokespecial #22; //method java/lang/object."<init>":()v
4: aload_0
5: iconst_1
6: invokestatic #28; //method scala/runtime/boxesruntime.boxtointeger:(i)ljava/lang/integer;
9: putfield #13; //field a:ljava/lang/object;
12: aload_0
13: iconst_1
14: putfield #16; //field b:i
17: return
}
so explicit anyval usage leads to boxed primitive on the heap, as expected.
Source: stackoverflow.com
Related Query
- Are Scala's `AnyVal`s stack allocated?
- Spark - How many Executors and Cores are allocated to my spark job
- How many threads are allocated by default by actor system?
- What are the limits on scalas ability to combine implict conversions
- What are the best use cases for Akka framework
- What are all the uses of an underscore in Scala?
- What are Scala context and view bounds?
- What do <:<, <%<, and =:= mean in Scala 2.8, and where are they documented?
- What Scala web-frameworks are available?
- What's the standard way to work with dates and times in Scala? Should I use Java types or there are native Scala alternatives?
- What are type lambdas in Scala and what are their benefits?
- Are HLists nothing more than a convoluted way of writing tuples?
- What are the key differences between Scala and Groovy?
- What are some compelling use cases for dependent method types?
- What are the relationships between Any, AnyVal, AnyRef, Object and how do they map when used in Java code?
- What are the precise rules for when you can omit parenthesis, dots, braces, = (functions), etc.?
- What are the disadvantages to declaring Scala case classes?
- Why are `private val` and `private final val` different?
- What are the use cases of scala.concurrent.Promise?
- What are some example use cases for symbol literals in Scala?
- When are higher kinded types useful?
- What are the differences and similarties between Scala traits vs. Java 8 interfaces?
- What are Scala continuations and why use them?
- What are the pros of using traits over abstract classes?
- How are the multiple Actors implementation in Scala different?
- Are FP and OO orthogonal?
- What are practical uses of applicative style?
- What are the problems with an ADT encoding that associates types with data constructors? (Such as Scala.)
- What are type classes in Scala useful for?
- What are all the instances of syntactic sugar in Scala?
More Query from same tag
- Implements trait with an anonymous object in Scala
- Scalaz Task not starting
- Set Play Framework Environment
- Twirl view engine look alike
- Writing a functional and yet functional image processing library in Scala
- How to get the failure that happens in another class scope in my test?
- Is it possible to make an Akka HTTP core client request inside an Actor?
- scala: how to use a class like a variable
- Finding the index of a specific SortedMap
- Play Idea failing - no sbt-idea plugin for this version of sbt 0.11.3
- Convert Set to cats.data.NonEmptySet?
- scala cats applicative failed to compile
- "for" translation into lists high order functions
- Modify a method parameter value using aspectj in Scala
- Gatling - Set Request name dynamically
- Spark multiple dynamic aggregate functions, countDistinct not working
- Scala project with maven in IntelliJ does not compile
- What is the difference between a class and a type in Scala (and Java)?
- Unknown Error from SBT console for dispatch-Http Get request
- Gatling Login scenario with CSV feeder
- Scala: Use case for Array.toArray?
- Basic FunctionN cov/contravariance
- Get Runtime Type picked by implicit evidence
- Blocking netty IO using Scala
- Is there any way to get Key/Value type from a map in Scala?
- Functional approach for a transaction processing
- pom configuration to force usage of jvm 7 with scala maven plugin
- Different ZonedDateTime of GMT and BST become same Instant after toInstant conversion in Scala
- Displaying your own Error messages in Scala/Play Framework
- Is it good practice to use @BeanProperty in Scala instead of defining getter/setter functions?