{"id":177,"date":"2021-12-31T14:18:09","date_gmt":"2021-12-31T14:18:09","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=177"},"modified":"2021-12-31T14:18:09","modified_gmt":"2021-12-31T14:18:09","slug":"scala-data-types","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/scala\/scala-data-types\/","title":{"rendered":"Scala &#8211; Data Types"},"content":{"rendered":"<p>Since Scala has some similarity with Java, it is no surprise that it has the same data types as in Java. Although there are a few additional data types.<\/p>\n<p>We cover the following sub-topics:<\/p>\n<ol>\n<li><a href=\"#t1\">Scala Data Types<\/a><\/li>\n<li><a href=\"#t2\">Literals in Scala<\/a><\/li>\n<li><a href=\"#t3\">Escape Sequences<\/a><\/li>\n<\/ol>\n<h4><strong id=\"t1\">1. Scala Data Types<\/strong><\/h4>\n<p>The table below gives a list of Scala data types.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>Data Type<\/th>\n<th>Brief Description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\"><b>Byte<\/b><\/td>\n<td>8 bit signed value. Range from -128 to 127<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\"><b>Short<\/b><\/td>\n<td>16 bit signed value. Range -32768 to 32767<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\"><b>Int<\/b><\/td>\n<td>32 bit signed value. Range -2147483648 to 2147483647<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\"><b>Long<\/b><\/td>\n<td>64 bit signed value. -9223372036854775808 to 9223372036854775807<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\"><b>Float<\/b><\/td>\n<td>32 bit IEEE 754 single-precision float<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\"><b>Double<\/b><\/td>\n<td>64 bit IEEE 754 double-precision float<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\"><b>Char<\/b><\/td>\n<td>16 bit unsigned Unicode character. Range from U+0000 to U+FFFF<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\"><b>String<\/b><\/td>\n<td>A sequence of Chars<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\"><b>Boolean<\/b><\/td>\n<td>Either the literal true or the literal false<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\"><b>Unit<\/b><\/td>\n<td>Corresponds to no value<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\"><b>Null<\/b><\/td>\n<td>null or empty reference<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\"><b>Nothing<\/b><\/td>\n<td>The subtype of every other type; includes no values<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\"><b>Any<\/b><\/td>\n<td>The supertype of any type; any object is of type\u00a0<i>Any<\/i><\/td>\n<\/tr>\n<tr>\n<td class=\"ts\"><b>AnyRef\u00a0<\/b><\/td>\n<td>The supertype of any reference type<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Just like in Python, all data types in Scala are object data types<\/p>\n<p>It is also good to know that that the data types in Scala are objects. Also called reference types or non-primitive types. This means that the<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Literals in Scala<\/strong><\/h4>\n<p>Literals are used to define a constant value for example &#8216;Kindson&#8217;, 0.023, 120.<\/p>\n<p>Let&#8217;s take a look at some of the basic literals in Scala programming language.<\/p>\n<p>Int Literals &#8211; These are values without a decimal point. I could be of type Long when suffixed by L or l. Otherwise it is an Int<\/p>\n<p><strong>Floating Point Literals<\/strong> &#8211; These are floating point values which maybe sometimes followed by the suffix F or\u00a0 f. When followed by a suffix, it is a Float, otherwise it is a double.<\/p>\n<p><strong>Boolean Literals<\/strong> &#8211; Boolean literals are <strong>true<\/strong> or <strong>false<\/strong> values<\/p>\n<p><strong>Symbol Literals<\/strong> &#8211; A symbol literal &#8216;x&#8217; in scala represents the expression scala.Symbol(&#8220;x&#8221;)<\/p>\n<p><strong>Character Literals<\/strong> &#8211; Character literals in Scala is enclosed in single quotes. For example &#8216;A&#8217;, &#8216;2&#8217;. The character could also be an escape sequence (&#8216;\\n&#8217; for example) or unicode characters (&#8216;\\u0041&#8217;) for example.<\/p>\n<p><strong>String Literals<\/strong> &#8211; Just like in Java, String literals in Scala is enclosed in double quotes.<\/p>\n<p><strong>Multi-line Strings<\/strong> &#8211; A multi-line string in Scala begins with tripple double quotes (&#8220;&#8221;&#8221;) and ends with same. An example is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"background-color: #fff0f0;\">\"\"\"this is example <\/span>\r\n<span style=\"background-color: #fff0f0;\"> of multi-line string<\/span>\r\n<span style=\"background-color: #fff0f0;\">in scala programming.\"\"\"<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Null Values<\/strong> &#8211; Null value is scala is of type scala.Null. It represents a reference to a special &#8220;null&#8221; object<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Escape Sequences<\/strong><\/h4>\n<p>An escape sequence is a sequence of characters that when used within a string, translates to another character or sequence which would otherwise be difficult to represent directly. For instance, &#8216;\\n&#8217; translates a newline.<\/p>\n<p>The table below provides a list of escape sequences in Scala.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>Escape Sequences<\/th>\n<th>Unicode<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>\\n<\/td>\n<td>\\u000c<\/td>\n<td>newline NL (Form feed)<\/td>\n<\/tr>\n<tr>\n<td>\\t<\/td>\n<td>\\u0009<\/td>\n<td>horizontal tab HT<\/td>\n<\/tr>\n<tr>\n<td>\\b<\/td>\n<td>\\u0008<\/td>\n<td>backspace BS<\/td>\n<\/tr>\n<tr>\n<td>\\f<\/td>\n<td>\\u000c<\/td>\n<td>formfeed FF<\/td>\n<\/tr>\n<tr>\n<td>\\r<\/td>\n<td>\\u000d<\/td>\n<td>carriage return CR<\/td>\n<\/tr>\n<tr>\n<td>\\&#8221;<\/td>\n<td>\\u0022<\/td>\n<td>double quote &#8220;<\/td>\n<\/tr>\n<tr>\n<td>\\&#8217;<\/td>\n<td>\\u0027<\/td>\n<td>single quote .<\/td>\n<\/tr>\n<tr>\n<td>\\\\<\/td>\n<td>\\u005c<\/td>\n<td>backslash \\<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Since Scala has some similarity with Java, it is no surprise that it has the same data types as in Java. Although there are a &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[25],"class_list":["post-177","post","type-post","status-publish","format-standard","hentry","category-scala-programming","tag-data-types"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/comments?post=177"}],"version-history":[{"count":1,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/177\/revisions"}],"predecessor-version":[{"id":178,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/177\/revisions\/178"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}