Thursday 20 October 2016

Basics of Scala : Defining a Types in Scala : Day 2 Learnings

Scala has both numeric (e.g., Int and Double ) and nonnumeric types (e.g., String ) that can be used to define values and variables. These core types are the building blocks for all other types including objects and collections, and are themselves objects that have methods and operators that act on their data. 

Unlike Java and C there is no concept of a primitive type in Scala. While the Java Virtual Machine supports the primitive integer type int and the integer class Integer , Scala only supports its own integer class, Int .


Displays Scala’s numeric data types:


Name Description Size Min Max
Byte Signed integer 1 byte -127 128
Short Signed integer 2 bytes -32768 32767
Int Signed integer 4 bytes -2^31 2^31 - 1
Long Signed integer 8 bytes -2^63 2^63 - 1
Float Signed floating point 4 bytes n/a n/a
Double Signed floating point 8 bytes n/a n/a



In addition to those types, Boolean can have the values true or false.

Scala supports the ability to automatically convert numbers from one type to another in the order
Byte ➤ Short ➤ Int ➤ Long ➤ Float ➤ Double



Note: See the API documentation for java.lang.Float and java.lang.Double for a description of the calculated maximum and minimum values for these floating-point numbers.


Displays Scala’s numeric literals:

Literal Type Description
5 Int Unadorned integer literals are Int by default
0x0f Int The “0x” prefix denotes hexadecimal notation
5l Long The “l” suffix denotes a Long type
5.0 Double Unadorned decimal literals are Double by default
5f Float The “f” suffix denotes a Float type
5d Double The “d suffix denotes a Double type

Note: You can use either lowercase or uppercase letters in Scala’s literal types. The literal number 5L is the same as the literal number 5l .




I will cover each type in depth next articles.





Share this article with your friends.

1 comment :

Related Posts Plugin for WordPress, Blogger...