What is a Basic Type in Scala
Basically, a Scala program describes the interaction between objects which interchange the messages. For example, things like numbers, character, and strings, etc. are objects that can interact with other objects or similar types of objects.
Wish to get certified in Scala! Learn Scala from top Scala experts and excel in your career with Intellipaat’s Scala certification!
Watch this Apache-Spark-Scala video
Objects with no internal structure that is with no components are said to be of a basic type.
Learn more about the Significance Of Scala Training in this insightful blog now!
Table: Basic types in Scala
|Byte||integers in the range from −128 to 127|
|Short||integers in the range from −32768 to 32767|
|Int||integers in the range from −2147483648 to 2147483647|
|Long||integers in the range from −9223372036854775808 to9223372036854775807|
|Float||largest positive finite float: 3.4028235×1038 and smallest positive finite nonzero float: 1.40×10−45|
|Double||largest positive finite double:1.7976931348623157×10308 and the smallest positive finite nonzero double: 4.9×10−324|
|char||Unicode characters with code points in the range from U+0000to U+FFFF|
|String||a finite sequence of Chars|
|Boolean||either the literal true or the literal false|
|Unit||either the literal true or the literal false|
|Null||null or empty reference|
|Nothing||the subtype of every other type; includes no values|
|Any||Any the supertype of any type; any object is of type Any|
|AnyRef||The supertype of any reference type i.e. nonvalue Scala classes and user-defined classes|
Enroll yourself in Online Scala Training and give a head-start to your career in Scala!
When declaring a variable or a constant use keyword var or the keyword Val then the name of a variable or a constant, an equals sign (i.e., the symbol =) and then the value the variable or the constant. Also, you can wite:
scala> val i : Byte = 32
i: Byte = 32
If the value does not agree with the type then a type of error has occurred.
scala> var x=30
x: Int = 30
An integer literal that is suffixed by an L or an l is assumed to be of type Long:
scala> var x=3l
x: Long = 3
Numbers can be written as a decimal, octal or hexadecimal numerals. Octal numbers can be written by adding prefix 0:
res0: Int = 493
Hexadecimal numbers can be written by adding prefix 0x or 0X:
res1: Int = 8201
A floating-point number contains an integral part and an optional decimal point which is represented by a period character that may be followed by an optional fractional part, an optional exponent and an optional type suffix. The exponent is started with either the letter E or e.
Looking for a job change in Scala? Check out this informative blog on Jobs in Scala and excel in your career!
The letters f and F represents a single-precision floating-point number whereas the letters d and D represents a double-precision floating-point number:
res4: Double = 20.11
res4: Double = 15.5E10
A character must be enclosed in single quotation marks:
scala> var a = ‘i’
a: Char = ‘i’
You cannot assign to a character variable a single quotation mark because it gives an error. To solve this problem escape sequence is used which is started with /.
Prepare yourself for the industry by going through this Top Apache Spark and Scala Interview Questions and Answers!
Table: Escape Sequence
|\uhhhh||h is hexadecimal digit|
A String is a sequence of Chars that is enclosed in double-quotes:
res6: java.lang.String = Scala
The literals true and false denote truth and untruth, respectively. These literals are the only values of type Boolean:
scala> val T = true
T: Boolean = true
Still, have queries? Come to Intellipaat’s Big Data Community, clarify all your doubts, and excel in your career!
Unit Type contains only one value which is designated by two parentheses:
scala> var x = ()
x: Unit = ()