Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scala, what is the difference between Any and Object?

Tags:

scala

Suppose I have the following java method

protected void onEvent(Object obj) {      } 

The Scala compiler accepts

protected override def onEvent(event: Any) 

and

protected override def onEvent(event: Object) 

Is there any difference between the two?

like image 561
deltanovember Avatar asked Aug 23 '11 12:08

deltanovember


People also ask

What is any in Scala?

Scala Type Hierarchy Any is the supertype of all types, also called the top type. It defines certain universal methods such as equals , hashCode , and toString . Any has two direct subclasses: AnyVal and AnyRef . AnyVal represents value types.

What is an object in Scala?

In Scala, an object is a named instance with members such as fields and methods. An object and a class that have the same name and which are defined in the same source file are known as companions.

What is difference between object and class in Scala?

Difference Between Scala Classes and Objects Definition: A class is defined with the class keyword while an object is defined using the object keyword. Also, whereas a class can take parameters, an object can't take any parameter. Instantiation: To instantiate a regular class, we use the new keyword.

Is everything an object in Scala?

Everything is an Object. Scala is a pure object-oriented language in the sense that everything is an object, including numbers or functions. It differs from Java in that respect, since Java distinguishes primitive types (such as boolean and int ) from reference types.


1 Answers

There is an article on scala-lang with great diagram (I even put it on the wall). And also need to be mentioned:

If Scala is used in the context of a Java runtime environment, then scala.AnyRef corresponds to java.lang.Object.

like image 188
4e6 Avatar answered Sep 30 '22 11:09

4e6