While going through Programming in Scala, i came across:
While you can define your own value classes (see Section 11.4), there are nine value classes built into Scala: Byte, Short, Char, Int, Long, Float, Double, Boolean, and Unit. The first eight of these correspond to Java's primitive types, and their values are represented at run time as Java's primitive values. The instances of these classes are all written as literals in Scala. For example, 42 is an instance of Int, 'x' is an instance of Char, and false an instance ofBoolean. You cannot create instances of these classes using new. This is enforced by the "trick" that value classes are all defined to be both abstract and final.
Due to which new Int
gives the error class Int is abstract; cannot be instantiated
val a: Int = new Int
in Scala. Java allows new Integer(23)
.
Question: What is the trick
the author is taking about. Why Scala defines value classes
to be abstract and final
.
scala.Int
is notjava.lang.Integer
, and Java does not allownew int(23)
. - Alexey Romanov