I usually end up trying every combination until it compiles. Can somebody explain what I should use where?
I usually end up trying every combination until it compiles. Can somebody explain what
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’ll disagree with Chris’s answer in one regard. The classes
Any,AnyRefandAnyValare 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
AnyValin Scala. Until Scala 2.10.0,AnyValwas 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
AnyisAnyRef, which is equivalent tojava.lang.Object(on the JVM at any rate).Up to Scala 2.9.x, a user could not extend
AnyorAnyVal, nor reference them from Java, but there were other uses they could be put to in Scala. Specifically, type signatures:What each means should be obvious from the class hierarchy. Of note, however, is that
fandhwill auto-box, butgwill not. That is a bit the opposite of what Java does, in thatfandhcannot be specified, andg(defined withjava.lang.Object) would cause auto-boxing.Starting with Scala 2.10.0, though, the user can extend
AnyValorAny, 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 extendAnyRefand 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.