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

Basic Types in Scala

Objects with no internal structure that is with no components are said to be of a basic type.

class hierarchy of scala
Learn more about the Significance Of Scala Training in this insightful blog now!

Table: Basic types in Scala

Data typeRange
Byteintegers in the range from −128 to 127
Shortintegers in the range from −32768 to 32767
Intintegers in the range from −2147483648 to 2147483647
Longintegers in the range from −9223372036854775808 to9223372036854775807
Floatlargest positive finite float: 3.4028235×1038 and smallest positive finite nonzero float: 1.40×10−45
Doublelargest positive finite double:1.7976931348623157×10308 and the smallest positive finite nonzero double: 4.9×10−324
charUnicode characters with code points in the range from U+0000to U+FFFF
Stringa finite sequence of Chars
Booleaneither the literal true or the literal false
Uniteither the literal true or the literal false
Nullnull or empty reference
Nothingthe subtype of every other type; includes no values
AnyAny the supertype of any type; any object is of type Any
AnyRefThe 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:

scala> 0755
res0: Int = 493

Hexadecimal numbers can be written by adding prefix 0x or 0X:

scala> 0x2009
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:

scala> 20.11
res4: Double = 20.11
scala> 15.2E10
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

Escape sequenceMeaning
\tHorizontal tab
\’Single quote
\”Double Quote
oooOctal digit
\uhhhhh is hexadecimal digit
\fForm feed
\rCarriage return

A String is a sequence of Chars that is enclosed in double-quotes:

scala> "Scala"
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 = ()

Recommended Videos

Leave a Reply

Your email address will not be published. Required fields are marked *