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 union two tables and remove duplicates with conditions using Spark
- Exception when using the saveToPhoenix method to load/save a RDD on Hbase
- How to return multiple items from a list, given their indices?
- Adding class to class path of sbt plugin
- Merge two LinkedHashMap in Scala
- Scala covariant type error
- Scala isInstanceOf[T] function fail to use bounded ClassTag/TypeTag
- Compare two dataframes to find substring in spark
- What are the weaknesses in using Immutability + Actor model for concurrency programming?
- Get Values from a Slick 2.0 Query in a case class form
- Pattern matching an int against a set of sets of ints
- Intellij not compiling scala project
- Typeclass with multiple parameters. How to summon an instance having only one parameter?
- How does memory management for Scala collections work?
- Update returning queries in ScalikeJDBC
- Finch jsonBody custom parameter conversion
- Scala Log4j configuration
- Post request string to localhost in scala akka
- How to make parallel Http request using Akka-Http?
- Skip/Take with Spark SQL
- Scala Trait with different def based on Type Parameter
- explicitly close db connection in slick
- Spark remove duplicate rows from DataFrame
- How to fix the problem "Fatal error: java.lang.Object is missing called from core module analyzer" in ScalaJS Linking
- Regular Expression: ReplaceAllIn returns a StringIndexOutOfBoundsException when generating UTF-8
- Translate Scala code to Rename and Move CSV file - Spark - PySpark
- How to process object field values before writing to JSON when using combinators
- Monad of Nothing doesn't work in cats
- restfb api not support when user logout from facebook & access key generate every time
- spray-json Cannot find JsonWriter or JsonFormat type class for Class