| 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
Anyis the root of the Scala class hierarchy. Every class in a Scala execution environment inherits directly or indirectly from this class. ClassAnyhas two direct subclasses:AnyRefandAnyVal
From the above Image
AnyValis extended by all numeric types. All types reside in packagescalaScala 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
Unitis similar tovoidin Java. If you remember the Hello World program,Unitis the return type ofmainmethod
def main(args: Array[String]): Unit = { .... }
Scala also has got a type Boolean :
val trueBoolean: Boolean = trueString 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
ifstamement, 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
Nullis an object,Nullcorresponds toNullvalue or empty referenceNo one can create object like
Nothingin scala, this is used to denote empty collection or abnormal terminationAnyRefis the Object World of the Scala, it corresponds tojava.lang.Objectin Java, and is the supertype of all objectsAnyValon 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,
nullis 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.OptionClass
scala.Optioncan prevent unintended null pointer exceptions when using ScalaNow Java has also followed Scala and Introduced
java.util.Optional