1

It seems for a good reason I need to switch to Scala! So, excuse me if my question might seem novice.

It seems that Any in scala is the supper class of all types and might be considered as an Object type equivalent in Java. But type Any is abstract, while Object is not. More concretely, I can not write the following code in scala (since class Any is abstract and cannot be instantiated):

val k:Any = new Any(); 

while I can write the following code in Java:

Object k=new Object();

What should I use if I need a concrete equivalent type of java Object type?

Community
  • 1
  • 1
qartal
  • 2,024
  • 19
  • 31

2 Answers2

3

AnyRef type which is not abstract seems to be the equivalent of the Java Object Class (see this)

so the following works in scala:

  val k:AnyRef = new AnyRef();
qartal
  • 2,024
  • 19
  • 31
0

AnyRef is the equivalent of java's Object. (Scala unifies the type system rather than having the object/primitive distinction that Java has. So an instance of Any might be a descendant of Object/AnyRef, or an instance of a Java primitive type (e.g. int). (Or it could also be a "value class" descending from AnyVal)).

lmm
  • 17,386
  • 3
  • 26
  • 37