score:129
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.
score:5
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?
score:44
Seen this? The text of the page has some java interoperability remarks. http://www.scala-lang.org/node/128
Source: stackoverflow.com
Related Query
- What are the relationships between Any, AnyVal, AnyRef, Object and how do they map when used in Java code?
- What are the differences between mapcat in Clojure and flatMap in Scala in terms of what they operate on?
- Scala | what is the difference between '<-', '->' and '=>' operators and how do they work implicitly?
- What are the differences between "object equality" created by the factory method of the companion object and case class?
- What is the formal difference in Scala between braces and parentheses, and when should they be used?
- What are the key differences between Scala and Groovy?
- What are the differences and similarties between Scala traits vs. Java 8 interfaces?
- What are the differences between final class and sealed class in Scala?
- What are the biggest differences between Scala 2.8 and Scala 2.7?
- What are the differences between asInstanceOf[T] and (o: T) in Scala?
- In Scala, what is the difference between Any and Object?
- What are the main differences between Scala and Frege (in programming paradigms)?
- What are the differences between a Scala Future and a Java Future
- What are the important features of the shapeless API (in Scala), and what do they do?
- What are the differences between the type inference of Scala and C++11?
- What are the differences between Akka and Netty besides their choice of language (Scala vs Java)?
- What are the key differences between Java 8's Optional, Scala's Option and Haskell's Maybe?
- What are the differences between Either and Option?
- What are the differences between ++= and += in sbt, say with libraryDependencies?
- What are the similarities and differences between Scala Transducers and Clojure Transducers?
- What are the performance characteristics between curried, partially applied, and 'normal' functions in Scala?
- What are the differences between Scala’s inner classes and Java’s Inner/nested classes?
- What are all the overriding rules between def, val, lazy val, and var in scala?
- What are the subtle differences between val and singleton objects?
- What are the differences between LazyList and List in Scala?
- How are Scala immutable indexed sequences implemented and what is the complexity of their operations?
- According to the Scala Language Specification, packages are AnyRef values and have types. How does this make sense?
- How can an object in Scala be cast to the intersection of its own type and any unrelated trait?
- What is the difference in memory allocation of objects extending AnyVal and AnyRef
- What is the difference between object A extends B and class A extends B and later new B
More Query from same tag
- How to unpack Option[(Int, Int)] in Scala
- How to get good performance on reading cassandra partitions in spark?
- scala how to use val instead of var
- Spark Datasets and variance
- How to register a TypeSerializer with Flink?
- If then else in a map
- Scala currying with three argument function
- ProcessLogger, only collect errors
- Why does jersey doesn't pick up my PATH and give me a 404?
- Nested object name
- Mixing typed matches with sequence matcher gives out strange behaviour in Scala
- How to reaload scala application after code change using actors and sbt
- File handling between akka and play
- Scala: abstract type pattern A is unchecked since it is eliminated by erasure
- How to sum a map of maps of same key in scala
- How to extract slick entities from play framework dao singleton
- To which category Scala Arrays belong?
- Getting a Scala Map from a Java Properties
- Parameter to the Trait in scala
- Is there any repository to hold scala wrappers for javascript libs?
- RDD filter with other function
- define a map over boolean scala
- File not found Exception using Spark-submit
- How to convert functions raising exceptions to functions returning Either?
- How to run scala worksheet with options ?
- Does the synchronized construct in Java use internally (and somehow) the hardware primitive CAS operation?
- Get All the instance subclass of trait using Google Guice
- When to close hbase connection and what will happen if connection not closed
- scala type 'extraction'
- How to skip tests in certain jenkins builds?