{"id":35,"date":"2021-12-20T11:15:51","date_gmt":"2021-12-20T11:15:51","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=35"},"modified":"2021-12-20T11:15:51","modified_gmt":"2021-12-20T11:15:51","slug":"scala-basic-program-syntax","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/scala\/scala-basic-program-syntax\/","title":{"rendered":"Scala &#8211; Basic Program Syntax"},"content":{"rendered":"<p>In the previous part (<a href=\"https:\/\/www.kindsonthegenius.com\/scala\/scala-your-first-program\/\" target=\"_blank\" rel=\"noopener\">Your First Scala Program<\/a>), we wrote and ran a simple Scala program. In this part, we would take a look at various Scala concepts you need to know. Actually, most of them derives\u00a0 from <a href=\"https:\/\/www.kindsonthegenius.com\/java\/\" target=\"_blank\" rel=\"noopener\">Java programming language<\/a>.<\/p>\n<ol>\n<li><a href=\"#t1\">Some Useful Scala Concepts<\/a><\/li>\n<li><a href=\"#t2\">Basic Program Syntax<\/a><\/li>\n<li><a href=\"#t3\">Identifiers in Scala<\/a><\/li>\n<li><a href=\"#t4\">Scala Keywords<\/a><\/li>\n<li><a href=\"#t5\">Comments, Whitespace and Newline<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Some Useful Scala Concepts<\/strong><\/h4>\n<p>Let&#8217;s take a look at six important Scala programming concepts. Some of them you may already know.<\/p>\n<p><strong>Class<\/strong> &#8211; A class is a blueprint or template for creating objects. It defines the attributes and behaviour of the objects<\/p>\n<p><strong>Object<\/strong> &#8211; An object is defined as an instance of a class. Just like real world objects, an object in Scala has attribute (fields) and behaviour (methods). These a defined in the class<\/p>\n<p><strong>Method<\/strong> &#8211; A method is the behaviour of an object.<\/p>\n<p><strong>Fields<\/strong> &#8211; These are unique set of attributes possessed by an object. The represents the state of the object. For example, a vehicle object could have a field called &#8216;Color&#8217; or &#8216;Type&#8217;. Values can be assigned to these fields.<\/p>\n<p><strong>Closure<\/strong> &#8211; A closure in Scala is a function whose return values is dependent on one or more variables declared outside the function<\/p>\n<p><strong>Trait<\/strong> &#8211; A trait in Scala is a reusable collection of abstract and non-abstract methods.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Basic Program Syntax<\/strong><\/h4>\n<p>If you remember from the previous part, we did write a HelloWorld Scala program.\u00a0 Now, similar to Java, a Scala program follows some syntactic rules. They are outlined below<\/p>\n<p><strong>Class names<\/strong> &#8211; Class name in Scala must begin with an uppercase letter. If the class name is a combination of several words, then the first letter of each word is capitalized<\/p>\n<p><strong>Cass Sensitivity<\/strong> &#8211; Scala is a case-sensitive language.<\/p>\n<p><strong>Method names<\/strong> &#8211; Method names in Scala must begin with a lower case. If a number of words is combined, then camelcase should be used. This means that the first letter is lower case and subsequently, the first letter of other words is uppercase. Examples are <strong><em>getSum()<\/em><\/strong>, <strong><em>calculateTotalScore()<\/em><\/strong>, <strong><em>addThreeNums()<\/em><\/strong> etc.<\/p>\n<p><strong>File Name<\/strong> &#8211; Just like in Java, the file names in Scala should match the name of the object. Also the file extension of a scala program is .scala.<\/p>\n<p>main function &#8211; The main() method is mandatory for every Scala program. Scala program begins execution from the main() method<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Identifiers in Scala<\/strong><\/h4>\n<p>Identifiers are names the programmer choose to give to the program components like variable, classes and method. The following rule apply to the four types of identifiers available in Scala.<\/p>\n<p><strong>Alphanumeric Identifiers<\/strong> &#8211; Alphanumeric identifiers in Scala begin with a letter or underscore followed by letters, digits or underscores. The $ symbol cannot be used in an identifier. The following are examples of valid identifiers: score, grade, _salary, name1, child_1<\/p>\n<p><strong>Operator Identifiers<\/strong> &#8211; An operator identifier is a combination of one or more operator characters. Examples are &#8211;, ++, ::, &lt;&gt;, +<\/p>\n<p><strong>Mixed Identifiers<\/strong> &#8211; A mixed identifier combines alphanumeric characters and operators. It normally begins with alphanumeric, then followed by operators<\/p>\n<p>Literal Identifiers &#8211; A literal identifier is a string enclosed in back ticks (`). Examples are<br \/>\n`y` `&lt;addtwo&gt;` `evaluate`<\/p>\n<h4><strong id=\"t4\">4. Scala Keywords<\/strong><\/h4>\n<p>You need to know as much Scala keywords as possible. Not just because you&#8217;ll be using them in your program, but also because, you cannot use a keyword as an identifier.<\/p>\n<p>The list of Scala keywords is given below:<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<td>def<\/td>\n<td>with<\/td>\n<td>if<\/td>\n<td>private<\/td>\n<\/tr>\n<tr>\n<td>abstract<\/td>\n<td>do<\/td>\n<td>else<\/td>\n<td>extends<\/td>\n<\/tr>\n<tr>\n<td>false<\/td>\n<td>final<\/td>\n<td>finally<\/td>\n<td>for<\/td>\n<\/tr>\n<tr>\n<td>forSome<\/td>\n<td>catch<\/td>\n<td>implicit<\/td>\n<td>import<\/td>\n<\/tr>\n<tr>\n<td>lazy<\/td>\n<td>match<\/td>\n<td>new<\/td>\n<td>Null<\/td>\n<\/tr>\n<tr>\n<td>object<\/td>\n<td>override<\/td>\n<td>package<\/td>\n<td>class<\/td>\n<\/tr>\n<tr>\n<td>protected<\/td>\n<td>return<\/td>\n<td>sealed<\/td>\n<td>super<\/td>\n<\/tr>\n<tr>\n<td>this<\/td>\n<td>throw<\/td>\n<td>trait<\/td>\n<td>Try<\/td>\n<\/tr>\n<tr>\n<td>true<\/td>\n<td>type<\/td>\n<td>val<\/td>\n<td>Var<\/td>\n<\/tr>\n<tr>\n<td>yield<\/td>\n<td>case<\/td>\n<td>while<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Comments, Whitespace and Newline<\/strong><\/h4>\n<p>Both single line and multiline comments in Scala is similar to Java.<\/p>\n<p>For single line comments you use \/\/<\/p>\n<p>For multi line comments, you use \/* and *\/<\/p>\n<p><strong>Whitespaces and blank lines<\/strong> &#8211; Just like comments, Scala ignores these.<\/p>\n<p>Scala statements are terminated with a semicolon(;) or a new line. Semicolons however are optional. So if you omit a semicolon, then that line is taken as a statement. But if you have multiple statements in a single line, then you must separate them with a semicolon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous part (Your First Scala Program), we wrote and ran a simple Scala program. In this part, we would take a look at &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":[],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-scala-programming"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/35","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=35"}],"version-history":[{"count":1,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":36,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/35\/revisions\/36"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}