Just curious about the difference between type Any
and AnyRef
in Scala.
Why does int belongs to AnyVal
but string belongs to AnyRef
?
For example:
val a: AnyVal = 3
val b: AnyRef = "1"
Any is the supertype of all types. Any has two direct subclasses: AnyVal and AnyRef.
AnyVal represents value types. There are nine predefined value types and they are non-nullable: Double, Float, Long, Int, Short, Byte, Char, Unit, and Boolean.
AnyRef represents reference types. All non-value types are defined as reference types. Every user-defined type in Scala is a subtype of AnyRef. String in scala equals to java.lang.String and is the subtype of AnyRef.
structure of scala.Any
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With