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?

2
  • Why would you need to instantiate an Object in a Scala program? Can you give us an example? Commented Jun 1, 2015 at 0:41
  • Maybe he failed to write new MyTrait and now wants to write something like new AnyRef with MyTrait instead of new MyTrait{}. Commented Jun 12, 2018 at 8:13

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Not useful.But does answer the OP ;)
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)).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.