Topic | Common types in Scala |
---|---|
Git sample | ValuesTest.scala AnyValTest.scala |
References | docs.scala-lang.org ktoso.github.io |
As Scala is also an object oriented language, each and every value in Scala is an object. Every Scala data type is an object
Class
Any
is the root of the Scala class hierarchy. Every class in a Scala execution environment inherits directly or indirectly from this class. ClassAny
has two direct subclasses:AnyRef
andAnyVal
From the above Image
AnyVal
is extended by all numeric types. All types reside in packagescala
Scala has 7 numeric types
- Byte : 8 bits :
val maxByteValue: Byte = Byte.MaxValue
- Char : 16 bits :
val maxCharValue: Char = Char.MaxValue
- Short : 16 bits :
val maxShortValue: Short = Short.MaxValue
- Int : 32 bits :
val maxIntValue: Int = Int.MaxValue
- Long : 64 bits :
val maxLongValue: Long = Long.MaxValue
- Float : 32 bits :
val maxFloatValue: Float = Float.MaxValue
- Double : 64 bits :
val maxDoubleValue: Double = Double.MaxValue
- Byte : 8 bits :
Type
Unit
is similar tovoid
in Java. If you remember the Hello World program,Unit
is the return type ofmain
method
def main(args: Array[String]): Unit = { .... }
Scala also has got a type Boolean :
val trueBoolean: Boolean = true
String is a sequence of Chars, alike a Java String
/* Two strings */
val stringOne = "stringOne"
val stringTwo = "stringTwo"
val contactOperationOne = stringOne + stringTwo
val contactOperationTwo = stringOne ++ stringTwo
/* Both values are same */
if (contactOperationOne.equals(contactOperationTwo)) {
println("Both operations ('+' & '++') performs similar on strings")
} else {
println("Both operations ('+' & '++') performs differently on strings")
}
- In Scala the Bottom Types are - Nothing and Null
val intThing: Int =
if (test) {
/* Int(AnyVal) */
11
}
else {
/* Nothing */
throw new Exception("Nothing!")
}
val stringThing: String =
if (test) {
/* String(AnyRef) */
"Yay!"
}
else {
/* Null */
null
}
- Type inference always looks for the common type of both branches in an
if
stamement, so if the other branch is a Type that extends everything, the infered type will automatically be the Type from the first branch
Nothing -> [Int] -> ... -> AnyVal -> Any : infered type: Int
Null -> [String] -> AnyRef -> Any : infered type: String
In Scala
Null
is an object,Null
corresponds toNull
value or empty referenceNo one can create object like
Nothing
in scala, this is used to denote empty collection or abnormal terminationAnyRef
is the Object World of the Scala, it corresponds tojava.lang.Object
in Java, and is the supertype of all objectsAnyVal
on the other hand represents the Value World of Scala, such as int and other JVM primitives
/* A dummy class */
class Person
/* A buffer which can store 'Any' */
val allThings = ArrayBuffer[Any]()
/* Int, kept as low-level 'int' during runtime */
val myInt = 10
/* Add an Int (extends AnyVal). boxed (!) -> becomes java.lang.Integer in the collection */
allThings += myInt
/* Person (extends AnyRef), no magic here */
allThings += new Person
Use None
instead of null
While it was habit in Java to initialize values to null, Scala provides an Option type for the same purpose
In Java,
null
is often used as a placeholder to denote a nonfatal error as a return value or to denote that a variable isn’t yet initializedIn Scala, one can denote this through the None subclass of
scala.Option
Class
scala.Option
can prevent unintended null pointer exceptions when using ScalaNow Java has also followed Scala and Introduced
java.util.Optional