I usually end up trying every combination until it compiles. Can somebody explain what I should use where?
3 Answers
I'll disagree with Chris's answer in one regard. The classes Any
, AnyRef
and AnyVal
are classes. But they don't appear as classes in bytecode, because of intrinsic limitations of the JVM.
This arises out of the fact that not everything in Java is an object. In addition to objects, there are primitives. All objects in Java are descendant from java.lang.Object
, but primitives are set apart and, presently*, not extensible by a programmer. Note also that primitives have "operators", not methods.
In Scala, on the other hand, everything is an object, all objects belong to a class, and they interact through methods. The JVM bytecode generated does not reflect this, but that doesn't make them any less so, just as Java has generics, even though the bytecode doesn't have them.
So, in Scala, all objects are descendant from Any
, and that includes both what Java considers objects and what Java considers primitives. There's no equivalent in Java because there is no such unification.
Everything that is considered a primitive in Java is descendant from AnyVal
in Scala. Until Scala 2.10.0, AnyVal
was sealed, and programmers were unable to extend it. It should be interesting to see what will happen with Scala on .Net, since interoperability alone calls for Scala to at least recognize user-defined "primitives".
Also extending Any
is AnyRef
, which is equivalent to java.lang.Object
(on the JVM at any rate).
Up to Scala 2.9.x, a user could not extend Any
or AnyVal
, nor reference them from Java, but there were other uses they could be put to in Scala. Specifically, type signatures:
def f(x: AnyVal) = println(x)
def g(x: AnyRef) = println(x)
def h(x: Any) = println(x)
What each means should be obvious from the class hierarchy. Of note, however, is that f
and h
will auto-box, but g
will not. That is a bit the opposite of what Java does, in that f
and h
cannot be specified, and g
(defined with java.lang.Object
) would cause auto-boxing.
Starting with Scala 2.10.0, though, the user can extend AnyVal
or Any
, with the following semantics:
If a class extends
AnyVal
, no instance will be created for it on the heap under certain conditions. This means the fields of this class (on 2.10.0 only a single field is allowed -- whether that will change remains to be seen) will stay on the stack, whether they are primitives or references to other objects. This allows extension methods without the instantiation cost.If a trait extends
Any
, then it can be used with both classes that extendAnyRef
and classes that extendAnyVal
.
PS: In my own view, Java is likely to follow C# in allowing "struct" primitives, and maybe typedefs, as parallelism without resorting to them is proving difficult to accomplish with good performance.

- 1
- 1

- 295,120
- 86
- 501
- 681
-
2An update: as of Scala 2.9.2, `AnyVal` is defined as `sealed trait AnyVal extends Any`. But in Scala 2.10 this has changed to `abstract class AnyVal extends Any with NotNull`, and it is now possible to extend `AnyVal` with the new value classes feature, e.g.: `class MyValue(val u: Int) extends AnyVal`. – ebruchez Sep 28 '12 at 22:15
-
@ebruchez Thanks for the note. I've updated my answer with an overview of the new capabilities. – Daniel C. Sobral Oct 02 '12 at 23:17
-
How does Nothing and Null relates to this? – Gaurav Khare Jul 16 '18 at 17:40
Seen this? The text of the page has some java interoperability remarks. http://www.scala-lang.org/node/128

- 1
- 1

- 13,186
- 3
- 44
- 32
Any
and AnyVal
are, I believe, part of the scala type system and are not classes as such (in the same way that Nothing
is a type, not a class). You cannot use them explicitly from within Java code.
Howwever, in Java/Scala interoperation, a method which accepts a Java Object
will expect a scala Any
/ AnyRef
.
What are you actually attempting to do?

- 133,303
- 56
- 317
- 449
-
I'm trying to understand this topic better so that I know what type I should be using when I'm planning to have Java call Scala or vice versa. This answer http://stackoverflow.com/questions/2334200/transforming-scala-varargs-into-java-object-varargs/2334331#2334331 and its cast to `AnyRef` just reminded me that this was still mysterious to me. – huynhjl Feb 26 '10 at 14:30