0
votes

Scala's String maps to Java's String. So reference will be in heap.

Scala's int, float maps to Java's Integer or primitive int. When does it refer to wrapper class and primitive types?

If it is wrapper class, will ref be at heap? If it is primitive, will ref be at stack?

How does that switch happen for a single variable? I read that scala uses both for optimization.

1

1 Answers

2
votes

When does it refer to wrapper class and primitive types?

Wrappers are used when primitive types can't be: e.g. it's used as a type parameter (other than for Array) or passed to a method taking Any/AnyRef.

You can tell what is used in a specific case by looking at bytecode, see e.g. https://www.toptal.com/scala/scala-bytecode-and-the-jvm.

If it is wrapper class, will ref be at heap? If it is primitive, will ref be at stack?

If it's primitive, there's no reference. And stack is only used for local variables. For local variables of type Int or Float, yes, primitive types are used.