0601.scala.AnyVal

sealed final class AnyVal
extends Any

Class AnyVal is the root class of all value types.

AnyVal has a fixed number subclasses, which describe values which are not implemented as objects in the underlying host system.

Classes Double, Float, Long, Int, Char, Short, and Byte are together called numeric value types. Classes Byte, Short, or Char are called subrange types. Subrange types, as well as Int and Long are called integer types, whereas Float and Double are called floating point types.

Direct Known Subclasses:
Boolean, Byte, Char, Double, Long, Float, Int, Short, Unit

The type Int names the class Int in the package scala. Values of this class are implemented just like Java's int values. In fact, Scala treats int as an alias for scala.Int. More generally, all of Java's primitive types are defined as aliases for classes in the scala package. For example, if you type boolean in a Scala program, the type you'll actually get is scala.Boolean. Or if you type float, you'll get scala.Float. When you compile your Scala code to Java bytecodes, however, Scala will compile these types to Java's primitive types where possible to get the performance benefits of Java's primitive types.


  • AnyVal 是 Scala 裡每個內建值類型的父類別。
  • AnyVal 有固定數量的子類別, 用以描述值且不能被實例化成物件。
有九個這樣的值類:Byte,Short,Char,Int,Long,Float,Double,Boolean和Unit。其中的前八個對應到Java的 原始類型,它們的值在運行時表示成Java的原始值。Scala裡這些類的實例都寫成文本。例如,42是Int的實例,'x'是Char的實例,false是Boolean的實例。你不能使用new 創造這些類的實例。這一點被「小伎倆」,值類都被定義為即是抽象的又是final的,強制貫徹

abstract final class Int
extends AnyVal

Class Int belongs to the value classes whose instances are not represented as objects by the underlying host system. There is an implicit conversion from instances of Int to instances of runtime.RichInt which provides useful non-primitive operations. All value classes inherit from class AnyVal.

Values MAX_INT and MIN_INT are in defined in object scala.Math.


Comments