{"id":36651,"date":"2016-04-27T11:00:50","date_gmt":"2016-04-27T08:00:50","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=36651"},"modified":"2019-03-29T14:06:27","modified_gmt":"2019-03-29T12:06:27","slug":"scala-tutorial-beginners","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/","title":{"rendered":"Scala Tutorial for Beginners"},"content":{"rendered":"<p>In this Tutorial article, we will see how to work with Scala Programming language, which is similar to Java but has got lot more advancements, as it was designed to overcome the pitfalls\/shortcomings of Java programming language.<\/p>\n<p>According to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Scala_%28programming_language%29\">Wikipedia<\/a>, the definition of Scala Programming language goes as follows.<\/p>\n<blockquote>\n<p>Scala (SKAH-lah) is a general purpose programming language. Scala has full support for functional programming and a very strong static type system. Designed to be concise, many of Scala&#8217;s design decisions were inspired by criticism of the shortcomings of Java.<\/p>\n<p>Scala source code is intended to be compiled to Java bytecode, so that the resulting executable code runs on a Java virtual machine. Java libraries may be used directly in Scala code and vice versa (language interoperability). Like Java, Scala is object-oriented, and uses a curly-brace syntax reminiscent of the C programming language. Unlike Java, Scala has many features of functional programming languages like Scheme, Standard ML and Haskell, including currying, type inference, immutability, lazy evaluation, and pattern matching.<\/p>\n<\/blockquote>\n<p><b>Pre-requisite: <\/b>The readers are expected to know the basics of Java Programming.<\/p>\n<p>The following table shows an overview of the entire article:\n<\/p>\n<div class=\"toc\">\n<h3>Table of Contents<\/h3>\n<dl>\n<dt><a href=\"#env\">1. Environment<\/a><\/dt>\n<dt><a href=\"#hello-world\">2. Hello World in Scala<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#hello-world-repl\">2.1 Hello World in REPL Interpreter<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#hello-world-repl-start\">2.1.1 Starting the Interpreter<\/a><\/dt>\n<dt><a href=\"#hello-world-repl-work\">2.1.2 Working with the Interpreter<\/a><\/dt>\n<dt><a href=\"#hello-world-repl-exit\">2.1.3 Exiting from the Interpreter<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#hello-world-file\">2.2 Hello World in Scala file<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#val-vs-var\">3. val Vs var in Scala<\/a><\/dt>\n<dt><a href=\"#string\">4. Working with Strings<\/a><\/dt>\n<dt><a href=\"#number\">5. Working with Numbers<\/a><\/dt>\n<dt><a href=\"#control\">6. Control Structures in Scala<\/a><\/dt>\n<dt><a href=\"#collection\">7. Collections in Scala<\/a><\/dt>\n<dt><a href=\"#conclusion\">8. Conclusion<\/a><\/dt>\n<dt><a href=\"#ref\">9. References<\/a><\/dt>\n<dt><a href=\"#download\">10. Download the Source Code<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"env\"><\/a>1. Environment<\/h2>\n<p>The examples in this article are executed in the <i>Command Prompt \/ Shell<\/i> with the Scala Interpreter. But they are guaranteed to work with any of the IDEs (like <i>Eclipse &#8211; Scala IDE, IntelliJ IDEA etc.,<\/i>) through the respective plugins. The examples are executed with the <strong>Scala Version: 2.11.7 in Windows 7<\/strong>.<\/p>\n<p>Let us first download and install Scala in our desktop. To download the latest version of Scala for your Operating System, please refer this <a href=\"http:\/\/www.scala-lang.org\/download\/\">link<\/a>. It is a straight forward installation like any other executable. You may please follow the instructions if any for your Operating System. This will set the <code>%SCALA_HOME%\/bin<\/code> directory of Scala to the <code>PATH<\/code> environment variable, for us to start working with the Scala programs.<\/p>\n<h2><a name=\"hello-world\"><\/a>2. Hello World in Scala<\/h2>\n<p>Let us write some Scala code to see the typical, popular <code>Hello, World<\/code> in Scala. We have two ways to do that. First is the Scala Interpreter which is a REPL, and another is the usual way of saving the code in a file and compiling and executing it afterwards. We will see both of them here.<\/p>\n<h3><a name=\"hello-world-repl\"><\/a>2.1 Hello World in REPL Interpreter<\/h3>\n<p>Scala Interpreter is a REPL (Read-Evaluate-Print-Loop) Interpreter, in which you can execute the quick one liners and scripts without any underlying class structure as an accompanying barebone skeleton as in few of the programming languages like C, C++ and Java. This is an advantage for evaluating the programming language and its constructs thereby you can save time. You use this REPL interpterter for your instant results.<\/p>\n<p>To be doubly sure, let us verify whether Scala installed in our machine correctly, by seeing the <code>%SCALA_HOME%<\/code> environment variable.<\/p>\n<pre class=\"brush:java;\">C:\\Users\\Raghs&gt; echo %SCALA_HOME%\nC:\\Program Files (x86)\\scala\n<\/pre>\n<p>As you see, Scala has been installed in the <code>C:\\Program Files (x86)\\scala<\/code> directory successfully. This could vary in your machine depending on the type of Operating System you have.<\/p>\n<h4><a name=\"hello-world-repl-start\"><\/a>2.1.1 Starting the Interpreter<\/h4>\n<p>For using this REPL interpreter, you need to open the <code>Command Prompt\/Terminal<\/code> and execute the command <code>scala<\/code>. If your installation was successul and your <code>PATH<\/code> environment variable has an entry to the <code>%SCALA_HOME%\/bin<\/code> directory, the command prompt \/ terminal will now prepare the Scala Interpreter ready for you to listen to your commands as follows.<\/p>\n<pre class=\"brush:java;\">C:\\Users\\Raghs&gt;scala\nWelcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_80).\nType in expressions to have them evaluated.\nType :help for more information.\n\n<\/pre>\n<p>After we executed the command <code>scala<\/code>, the Scala interpreter was invoked and has printed a Welcome message by displaying the version of Java and the Virtual Machine details.<\/p>\n<blockquote>\n<p>Please remember that Scala code is executed on JVM (JRE) only. The actual code\/script in Scala, will finally be executed on top of JVM , like a Java program. Hence, we could typically use the Java libraries directly in our Scala code. This is called <strong>language interoperability<\/strong><\/p>\n<\/blockquote>\n<h4><a name=\"hello-world-repl-work\"><\/a>2.1.2 Working with the Interpreter<\/h4>\n<p>Now, let us actually type the <code>Hello, World<\/code> in the Scala Interpreter to see in action.<\/p>\n<pre class=\"brush:java;\">scala&gt; \"Hello, World\"\nres0: String = Hello, World\n\nscala&gt; println(\"Hello, World\")\nHello, World\n\nscala&gt; \"Hello, World\"\nres1: String = Hello, World\n\nscala&gt; \"Hello, World\".getClass().getName()\nres2: String = java.lang.String\n\nscala&gt; res1\nres3: String = Hello, World\n\nscala&gt; res2\nres4: String = java.lang.String\n<\/pre>\n<p>It was a very simple command we passed to the REPL Shell\/Interpreter. We tried in two ways. The first one was the literal string &#8220;Hello, World&#8221; passed as double quotes surrounded String. The second command invocation to print the same Hello, World but with the <code>println()<\/code> method.<\/p>\n<p>It was considered as an expression being evaluated. Hence the interpreter printed the result with an identifier along with a prefix <code>res0:<\/code> meaning <code>result 0<\/code> \u2192 the 0th index of result entries. You can see the subsequent invocations (starting from 3rd and 4th commands) have the result printed with <code>res1:<\/code> and <code>res2:<\/code>. The 3rd command was the same command repeated but to show that the REPL Interpreter treats every command as a separate command and prints with the next result quotient &#8211; <code>res1:<\/code>. The 4th command is to invoke some methods on the command line. The command printed the result with <code>res2:<\/code>. Likewise any subsequent expressions will be printed with the results alongwith the incremented index of <code>res<\/code>.<\/p>\n<p>The interesting part is the you can directly use the result names as a variable for the subsequent operations, as <i>Scala Interpreter stores the result entries in the memory as long as the REPL Interpreter is running<\/i>. You can see this in action in the last 2 invocations where in <code>res1<\/code> and <code>res2<\/code> were directly used like a variables and the interpreter printed the corresponding values then and there.<\/p>\n<h4><a name=\"hello-world-repl-exit\"><\/a>2.1.3 Exiting from the Interpreter<\/h4>\n<p>Once we are done with our work, it is always good to exit the interpreter in a normal way. To do so, you may please enter <code><strong>:q<\/strong><\/code> to quit and exit from the Interpreter and come back to the normal Command Prompt \/ Terminal.<\/p>\n<pre class=\"brush:java;\">scala&gt; :q\n\nC:\\Users\\Raghs\\\n\n<\/pre>\n<h3><a name=\"hello-world-file\"><\/a>2.2 Hello World in Scala file<\/h3>\n<p>As we saw earlier, the commands and expressions entered in the REPL interpreter were shortlived and once you exit from the Interpreter, you can&#8217;t get them back. If we need to work with our scripts\/code any number of times in future, it is better to get the Source code saved in a file much like a Java program.<\/p>\n<p>Typical to how a Java program will be saved as <code>&lt;fileName&gt;.java<\/code> and we use <code>javac &lt;fileName&gt;.java<\/code> to compile the source code to get a class file named <code>&lt;fileName&gt;.class<\/code> and we use the Java interpreter to execute the binary file as <code>java &lt;fileName&gt;<\/code>, we have the counter parts for Scala file as well.<\/p>\n<p>We save the source file name as <code>&lt;fileName&gt;.scala<\/code> and use <code>scalac &lt;fileName&gt;.scala<\/code> to compile the source code and <code>scala &lt;fileName&gt;<\/code> to execute the binary class file. We will see them in action for the same Hello World scala program below.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>HelloWorld.scala<\/em><\/span><\/p>\n<pre class=\"brush:java;\">object HelloWorld {\n   def main(args: Array[String]):Unit = {\n      println(\"Hello, World\");\n   }\n}\n<\/pre>\n<p>The structure is more like a Java program, where we define a <code>main()<\/code> method that takes an array of String as an argument (which will be accepted from Command Line) and it is encapsulated inside the skeleton named<code>HelloWorld<\/code>. Please note that <code>object<\/code> is a keyword in Scala that helps us to easily create a Singleton instance &#8211; meaning only one instance of a class inside a JVM.<\/p>\n<p>A method in Scala is defined using the keyword <code>def<\/code> and the parameter is declared with its type with the colon after the parameter name accompanied with the Data type. Here you see, the parameter named <code>args<\/code> is of type <code>Array of Strings<\/code>. The return type is specified at the end of the method declaration, which is here <code>Unit<\/code> &#8211; one of Scala&#8217;s data types. The method definition is separated here with a <code>=<\/code> sign with a set of curly braces <code>{<\/code> and <code>}<\/code>.<\/p>\n<p>The only executable statement in the main method was printing the literal String &#8220;Hello, World&#8221; to console by invoking the <code>println()<\/code> method.<\/p>\n<blockquote>\n<p>Please note that the <strong>semicolon is <i><u>optional<\/u><\/i><\/strong> for each executable statement in Scala, unlike in a Java program.<\/p>\n<\/blockquote>\n<p>Let us now compile and execute the above scala program. It is fairly straight forward as given below.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java;\">C:\\Users\\Raghs&gt; scalac HelloWorld.scala\n\nC:\\Users\\Raghs&gt; scala HelloWorld\nHello, World\n<\/pre>\n<p>The first command was to invoke <code>scalac<\/code>, the Scala compiler to get our source code compiled into a <code>.class<\/code> file. If the compilation was successful, it simply returns to the command prompt without any text being displayed on the console. The second command was to invoke <code>scala<\/code>, the Scala Interpreter to execute the compiled class file. As per the program, it displayed <code>Hello, World<\/code> on the console.<\/p>\n<h2><a name=\"val-vs-var\"><\/a>3. val Vs var in Scala<\/h2>\n<p>Let us understand the very significant and important keywords in Scala. They are <code>val<\/code> and <code>var<\/code>. As you might have guessed, they are the identifiers for denoting the ability to change the value of variables at a later point time in the source code\/ script.<\/p>\n<p><strong>var<\/strong> indicates an mutable nature of the varaible&#8217;s value. That is a varaible&#8217;s value can be changed later after the inital declaration whereas <strong>val<\/strong> indicates an immutable nature of the variable&#8217;s value, which is like a <code>final<\/code> keyword in Java.<\/p>\n<blockquote>\n<p><strong>var<\/strong> is like varying value for a variable, whereas <strong>val<\/strong> is like <code>variable + final<\/code> to restrict the subsequent changes if any to a variable in the Program.<\/p>\n<\/blockquote>\n<p>Let us see them in action in REPL interpreter<\/p>\n<pre class=\"brush:java;\">C:\\Users\\Raghs&gt; scala\n\nscala&gt; var s = \"Hello World\"\ns: String = Hello World\n\nscala&gt; s = \"Hello Scala\"\ns: String = Hello Scala\n\nscala&gt; val c = \"Hello, World\"\nc: String = Hello, World\n\nscala&gt; c = \"Hello Scala\"\n:11: error: reassignment to val\n       c = \"Hello Scala\"\n      \n         ^\n\nscala&gt; c = \"Hello, World\"\n:11: error: reassignment to val\n       c = \"Hello, World\"\n         ^\n\n<\/pre>\n<p>As you see, in the above executions, at first we declared a variable <code>s<\/code> of type <code>var<\/code>. We initialized the variable <code>s<\/code> with a String literal &#8220;Hello, World&#8221; and the REPL interpreter printed the result of the expression, which in this case is the same string literal. Later, at step 2, we reassigned the varaible <code>s<\/code> with a different value <strong>&#8220;Hello Scala&#8221;<\/strong>, which was perfectly valid and legal because it was a <strong>var<\/strong>. Hence the second expression was allowed and printed the result, which was the latest value of <code>s<\/code>.<\/p>\n<p>However, the 3rd, 4th and 5th invocations show us a different set of results. The 3rd statement attempts to declare a variable <code>c<\/code> of type <code>val<\/code>, with an initial value &#8220;Hello, World&#8221;. The scala REPL interpreter printed the result of the expression, as like any other String literal in the console. However, at the 4th invocation, we attempt to reassign another value which is not allowed being the variable of type val. Hence, the REPL interpreter showed an error indicating that <code>reassignment to val<\/code> is not permitted. It is also helpful by indicating which line and position the error had occurred with an upward arrow key in the next line indicating the position.<\/p>\n<p>5th expression was to indicate that it was not just a different\/new value but even the same value being assigned again will throw the same error of reassignment to a val. This was because of the fact that the <strong><i><u>variable of type <code>val<\/code> is not allowed to have an assignment further once it is assigned with a value during its declaration<\/u><\/i><\/strong>.<\/p>\n<h2><a name=\"string\"><\/a>4. Working with Strings<\/h2>\n<p>Scala treats a String as an array of characters. It has got several methods on its own and it allows us to use the methods from the Java API classes like String, StringBuilder etc.,<\/p>\n<p>Let us play around with the Scala REPL interpreter on Strings and its methods. The following expressions (or code snippets) are self explanatory.<\/p>\n<pre class=\"brush:java;\">scala&gt; \"Hello, World\".getClass.getName\nres0: String = java.lang.String\n\nscala&gt; val s = \"Hello, World\"\ns: String = Hello, World\n\nscala&gt; s.length\nres1: Int = 12\n\nscala&gt; \"scala\".foreach(println)\ns\nc\na\nl\na\n\nscala&gt; \"scala\".drop(2)\nres0: String = ala\n\nscala&gt; \"scala\".take(2)\nres1: String = sc\n\nscala&gt; \"scala\".drop(2).capitalize\nres2: String = Ala\n\nscala&gt; \"scala\".take(2).toUpperCase\nres3: String = SC\n<\/pre>\n<p>As you see, we have got several method on String related classes like <code>StringLike<\/code>, <code>StringOps<\/code> etc., in Scala Library like <code>drop<\/code> and <code>take<\/code>, which are useful to delete or get the characters upto the index passed. We also have methods named <code>foreach<\/code> to ietrate each character at a time in a String, <code>capitalize<\/code> which will make the first character of the String to be in upper case.<\/p>\n<p>We could also use the String class methods from Java API &#8211; like <code>length()<\/code> and <code>toUpperCase<\/code>. Please note that the parantheses are optional in the REPL interpreter.<\/p>\n<p>The methods can be chained &#8211; meaning the methods can be invoked one after another in a way that result of executing one method will give the return value as an input to the next method &#8211; ie., the method on the right. The last two examples show the same. The drop method leaves the 2 characters from the left (0th index) and the remaining characters are passed on to the next method on the right capitalize which converts the first character (char at 0th index) to be in upper case, which in our case is &#8220;A&#8221; (&#8220;SC&#8221; dropped from the initial input string &#8220;scala&#8221;). The same way, the <code>toUpperCase<\/code> method was invoked on the result of invoking <code>take(2)<\/code> on the initial input string &#8220;scala&#8221;.<\/p>\n<blockquote>\n<p>You can see the API methods in <a href=\"http:\/\/www.scala-lang.org\/api\/current\/#scala.collection.immutable.StringLike\">StringLike class methods<\/a> in Scala API. The Web page has a good set of options for filtering, searching a specific package, class, method etc.,<\/p>\n<\/blockquote>\n<p>Let us see some String examples like String Concatenation, String Comparison, Null check etc.,<\/p>\n<pre class=\"brush:java;\">scala&gt; val s1 = \"Hello\"\ns1: String = Hello\n\nscala&gt; val s2 = \"Hello\"\ns2: String = Hello\n\nscala&gt; val s3 = \"H\" + \"ello\"\ns3: String = Hello\n\nscala&gt; s1 == s2\nres6: Boolean = true\n\nscala&gt; s1 == s3\nres7: Boolean = true\n\nscala&gt; val s4:  String = null\ns4: String = null\n\nscala&gt; s3 == s4\nres8: Boolean = false\n\nscala&gt; s1 == s4\nres9: Boolean = false\n\nscala&gt; s1.toUpperCase == s2.toUpperCase\nres13: Boolean = true\n\nscala&gt; val s1: String = null\ns1: String = null\n\nscala&gt; s1.toUpperCase == s2.toUpperCase\njava.lang.NullPointerException\n  ... 33 elided\n<\/pre>\n<p>As you see, the first 2 expressions declare two different variables of type <code>String<\/code> with an initial value &#8220;Hello&#8221;. The 3rd expression shows a declaration of a String variable with a String concatenation for its initial value &#8220;H&#8221; + &#8220;ello&#8221;. The 4th and 5th expressions show the String comparison where both of them are evaluated to true.<\/p>\n<blockquote>\n<p>The <code>==<\/code> operator in Scala checks for String content equality, like the <code>equals()<\/code> method in Java, whereas Java uses <code>==<\/code> to check the object reference (address in memory).<\/p>\n<\/blockquote>\n<p>An interesting thing to note that when you check for the String content equality with one of the Strings being null, Scala does NOT throw a NPE (NullPointerException) like Java. However it checks those constraints at the beginning as a safety measure and prints the result as <i>false<\/i>. However, please be careful that whenever any other attempt is made on the String contents like <code>toUpperCase, length etc.,<\/code>, Scala obviously throws a <code>NullPointerException<\/code>, which is what is shown in the last few code snippets above, where the toUpperCase method was invoked on a null reference S1.<\/p>\n<h2><a name=\"number\"><\/a>5. Working with Numbers<\/h2>\n<p>Scala has the same set of numeric data types as that of Java. They are <code>Char, Byte, Short, Int, Long, Float, Double, Boolean<\/code> with the same data range\/limit for each of the data types. In addition, it has got its own data types like <code>AnyRef, AnyVal, Any etc.,<\/code>. You can see the list of data types available in <a href=\"http:\/\/docs.scala-lang.org\/tutorials\/tour\/unified-types\">Scala website<\/a>.<\/p>\n<p>We have several methods available in the respective classes bearing the name as Data type listed above. The below code snippets \/ expressions are self explanatory.<\/p>\n<pre class=\"brush:java;\">scala&gt; Int.MinValue\nres4: Int = -2147483648\n\nscala&gt; Int.MaxValue\nres5: Int = 2147483647\n\nscala&gt; Byte.MinValue\nres6: Byte = -128\n\nscala&gt; Short.MinValue\nres7: Short = -32768\n\nscala&gt; Short.MaxValue\nres8: Short = 32767\n<\/pre>\n<p>As you see above, the respective data type classes had the methods minValue and maxValue for getting the lower and higher boundaries for each. The expressions in the scala REPL intepreter were evaluated and printed the results for our information.<\/p>\n<p>Let us have some mathematical operations on numbers in the Scala REPL interpreter.<\/p>\n<pre class=\"brush:java;\">scala&gt; 1 + 1\nres13: Int = 2\n\nscala&gt; \"1\" + \"1\"\nres14: String = 11\n\nscala&gt; \"1\".toInt + \"2\".toInt\nres15: Int = 3\n\nscala&gt; \"1\".toShort + \"1\".toShort\nres16: Int = 2\n\nscala&gt; \"1\".toByte + \"1\".toByte\nres17: Int = 2\n\nscala&gt; \"1\".toLong + \"1\".toLong\nres18: Long = 2\n<\/pre>\n<p>As you see above, the first expression was directly evaluated and the result was printed on the console. It was the sum of two numbers 1 and 1 equalling 2. However the 2nd invocation was different &#8211; where the numbers were given as String literals and hence the String Concatenation occurred producing a result of 11. If we need to parse and convert the numbers represented as a String, we can use the <code>toInt<\/code> method for the same. We have similar methods to convert to the different other data types like <code>toByte, toLong, toShort<\/code> etc., which you see in the last few expressions. These toInt() etc., methods convert the string literal into the corresponding numeric form and apply the mathematical operation to produce the result.<\/p>\n<p>Let us see some negative scenarios where you may get an error while attempting to convert the String literal to a numeric form.<\/p>\n<pre class=\"brush:java;\">scala&gt; \"129\".toByte\njava.lang.NumberFormatException: Value out of range. Value:\"129\" Radix:10\n  at java.lang.Byte.parseByte(Byte.java:150)\n  at java.lang.Byte.parseByte(Byte.java:174)\n  at scala.collection.immutable.StringLike$class.toByte(StringLike.scala:264)\n  at scala.collection.immutable.StringOps.toByte(StringOps.scala:30)\n  ... 33 elided\n\nscala&gt; \"foo\".toInt\njava.lang.NumberFormatException: For input string: \"foo\"\n  at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)\n  at java.lang.Integer.parseInt(Integer.java:492)\n  at java.lang.Integer.parseInt(Integer.java:527)\n  at scala.collection.immutable.StringLike$class.toInt(StringLike.scala:272)\n  at scala.collection.immutable.StringOps.toInt(StringOps.scala:30)\n  ... 33 elided\n\n<\/pre>\n<p>The first expression was attempting to convert the String Literal 129 to a Byte. However it failed because the Byte value range was only from -128 to 127. Hence you got an error with a meaningful description (see the very first line in the stack trace), which helps us to understand the mistake and take a corrective action. The second expression was obvious where a non-numeric value (actual String value) was attempted to be converted an Integer, which will definitely throw a <code><strong>NumberFormatException<\/strong><\/code>.[ulp id=&#8217;r5HxHBP7fqxvOHkw&#8217;]<\/p>\n<p>You can see that the methods <code>toInt, toByte<\/code> are present in the <code>StringOps<\/code> class which then further delegated to the <code>toInt()<\/code> method of the class <code>StringLike<\/code>. These calls are further stacked up with the Java API method <code>parseInt(), parseByte()<\/code> of the respective Wrapper classes <code>Integer, Byte<\/code>.<\/p>\n<p>Let us see some arithmetic operations with REPL interpreter.<\/p>\n<pre class=\"brush:java;\">scala&gt; var count = 1\ncount: Int = 1\n\nscala&gt; count ++\n:12: error: value ++ is not a member of Int\n       count ++\n             ^\n\nscala&gt; count += 1\n\nscala&gt; count\nres27: Int = 2\n\nscala&gt; count = count +  1\ncount: Int = 3\n<\/pre>\n<p>As you see, the first expression declared a variable of type <code>var<\/code> count with an initial value of 1. To increment the value with 1, we use the <code>++<\/code> in Java. However in Scala, it is slightly different as it uses <code>+=<\/code> in place. The 2nd expression fails due to the usage of <code>++<\/code> whereas the 3rd expression gets it right. However the evaluation of expression did not yield any result in the console. The 4th expression printed the value of <code>count<\/code> which is now 2 after the increment operation. The 5th expression shows the usual way to add 1 to the value of a variable. Here the value is explicitly assigned back to the variable, and hence the result (3) is printed on the console.<\/p>\n<blockquote>\n<p>Please note that the symbols <code>+, -, *, \/<\/code> etc., are the <strong>methods<\/strong> in Scala and <i><u>NOT<\/u><\/i> the operators like in Java.<\/p>\n<\/blockquote>\n<p>Because there is no method named <code>+=<\/code> in Int class, we got an error on 2nd expression above.<\/p>\n<p>Let us see how easy to work with a range of numbers in Scala.<\/p>\n<pre class=\"brush:java;\">scala&gt; val r = 1 to 10\nr: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n\nscala&gt; var r = 1 to 10 by 2\nr: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)\n\nscala&gt; for(i &lt;- 1 to 5) println(i)\n1\n2\n3\n4\n5\n\nscala&gt; for(i  for(i &lt;- 1 until 5) println(i)\n1\n2\n3\n4\n<\/pre>\n<p>The first expression above used the construct <code>1 to 10<\/code> to have a range of numbers from 1 to 10. The result shows that the a <code>Range<\/code> of type <code>scala.collection.immutable.Range.Inclusive<\/code> was created and assigned to the variable <code>r<\/code>.<\/p>\n<p>The 2nd expression shows that we can create a range of numbers from 1 to 10 with an incremental value of 2. That is why the resultant Range has the numbers <code>1, 3, 5, 7 and 9.<\/code>.<\/p>\n<p>The 3rd expression uses a for loop with a range within the paranthesis indicating that 1 to 5 to be used, with a closure that prints each of the value in the range in a separate line.<\/p>\n<p>The 4th expression uses a for loop but with the <code><strong>until<\/strong><\/code> which is upto the max value, meaning one lesser than the max boundary. The result printed on the console ranges from 1 to 4 and NOT upto 5.<\/p>\n<h2><a name=\"control\"><\/a>6. Control Structures in Scala<\/h2>\n<p>Like many other high level Programming languages, Scala offers <code>if, if-else, switch, while, for<\/code> control structures as a control statements and looping statements. We will see the examples for few of them below.<\/p>\n<p><strong>If<\/strong> statement has an expression and if that expression evalutes to true, the statement will be executed.<\/p>\n<pre class=\"brush:java;\">scala&gt; var num = 2\nnum: Int = 2\n\nscala&gt; if(num%2==0) println(\"Even\")\nEven\n\nscala&gt; if(num%2!=0) println(\"Odd\")\n\n<\/pre>\n<p>The first expression above assigns the value 2 to a variable named <code>num<\/code>. The 2nd expression tests for the value of the variable &#8216;num&#8217; being even and if so, it prints the String &#8220;Even&#8221; in the console. Since the variable &#8216;num&#8217; holds the value &#8216;2&#8217;, the condition in the if statement evaluates to true and hence the String &#8220;Even&#8221; is printed on the console as a result.<\/p>\n<p>If you see the 3rd expression, the if statement checks for the value of the variable &#8216;num&#8217; for odd number. Since the value 2 is not an odd number, the condition fails but nothing gets printed because the if statement will execute ONLY when the condition evaluates to true.<\/p>\n<p><strong>If-else<\/strong> &#8211; helps in cirumstances when we want to handle an alternate situation when the condition inside the if statement does not evaluate to true. See the previous example, it would be good if we have something else printed when the condition not evaluates to true, which otherwise be a return nothing on the console.<\/p>\n<pre class=\"brush:java;\">scala&gt; if(num%2!=0) println(\"Odd\") else println(\"Even\")\nEven\n\nscala&gt; num=1\nnum: Int = 1\n\nscala&gt; if(num%2!=0) println(\"Odd\") else println(\"Even\")\nOdd\n<\/pre>\n<p>As you see, the if statement was used with an <code>else<\/code> to have a different message printed on the alternate. The above examples show the same in action after altering the value of the variable &#8216;num&#8217; to 1.<\/p>\n<pre class=\"brush:java;\">scala&gt; val x=10; val y =20\nx: Int = 10\ny: Int = 20\n\nscala&gt; val max = if(x&gt;y) x else y\nmax: Int = 20\n<\/pre>\n<p>The first expression above declares two variables x and y in the same line with the values 10 and 20 respectively. The 2nd expression uses an if-else statment to check for the bigger value held by x and y and the chosen value is assigned to the meaningful variable named &#8216;max&#8217;.<\/p>\n<p><strong>for<\/strong> &#8211; loop is useful when you have a fixed boundaried collection to operate. It is used in two flavours &#8211; simple for loop and a variant <strong>foreach<\/strong> loop to iteration through a collection.<\/p>\n<pre class=\"brush:java;\">scala&gt; for(i &lt;- 1 to 10) println(i)\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n\nscala&gt; \"hello\".foreach(println)\nh\ne\nl\nl\no\n<\/pre>\n<p>The above expressions show that usage of <code>for, foreach<\/code> loops in a straightforward manner.<\/p>\n<p><strong>for with an iterator guard<\/strong> &#8211; is a special case in Scala where a for loop can have a conditional statement to give the eligibility of the element to be considered further. It acts like a filter to each element in the collection being operated.<\/p>\n<blockquote>\n<p>Sytax of an Iterator Guard is :<br \/>\n<code>for(&lt;identifier&gt; &lt;- &lt;collection\/range&gt; if &lt;Boolean expression&gt;) { ... }<\/code><\/p>\n<\/blockquote>\n<pre class=\"brush:java;\">scala&gt; for(i &lt;- 1 to 10 if(i%2==0)) println(i)\n2\n4\n6\n8\n10\n<\/pre>\n<p>The above expression used a for loop with an iterator guard to filter out ONLY the even numbers. The element will get printed ONLY if it is passing out of the filter, which is <code>i%2==0<\/code>.<\/p>\n<p><strong>while<\/strong> &#8211; loop is used at a situation when you are not sure of the boundary of the collection especially when to stop iterating.<\/p>\n<h2><a name=\"collection\"><\/a>7. Collections in Scala<\/h2>\n<p>Scala offers several different Collection elements like <code>List, Set, and Map<\/code>. We will see the list collection with an example below.<\/p>\n<p>We can instantiate a List by calling it as a function and passing the list of elements as a comma separated values within the paranthesis.<\/p>\n<pre class=\"brush:java;\">scala&gt; val numbers=List(1,2,3,4,5)\nnumbers: List[Int] = List(1, 2, 3, 4, 5)\n\nscala&gt; numbers.size\nres55: Int = 5\n<\/pre>\n<p>The above expressions create a List with the numbers from 1 to 5 and the size of which is printed. Let us see how easily we can iterate the collections using a for loop and its variants.<\/p>\n<pre class=\"brush:java;\">scala&gt; numbers.foreach(println)\n1\n2\n3\n4\n5\n\nscala&gt; for(n  for(n &lt;- numbers if(n%2==0)) println(n)\n2\n4\n<\/pre>\n<p>The 1st expression used a foreach loop as a method invoked on the collection to print out each element in the collection, whereas the 2nd expression used the for loop on the collection with a loop variable named <code>n<\/code> to hold each value at a time from the collection and prints out the same. The 3rd expression uses the <code>for loop with an iterator guard<\/code> to print out ONLY the even numbers.<\/p>\n<p>Let us see few of the interesting methods available in Scala API for List objects. <strong>head<\/strong> method gets you the very first element &#8211; at 0th index, <strong>tail<\/strong> method invokes the rest of the elements in the collection excluding the very first at 0th index.<\/p>\n<pre class=\"brush:java;\">scala&gt; numbers.head\nres60: Int = 1\n\nscala&gt; numbers.tail\nres61: List[Int] = List(2, 3, 4, 5)\n\nscala&gt; numbers(0)\nres67: Int = 1\n\nscala&gt; numbers(2)\nres68: Int = 3\n\nscala&gt; numbers.tail.size\nres69: Int = 4\n\nscala&gt; numbers.tail(0)\nres62: Int = 2\n\nscala&gt; numbers.tail(2)\nres63: Int = 4\n<\/pre>\n<p>The above code snippets and expressions are self explanatory. The first element at index 0 is retrieved by using <code>head<\/code> method and the rest of the elements in the collection excluding the 0th element is retrieved by using the <code>tail<\/code> method. A particular element from the collection is obtained directly by passing its index as an argument like <code>numbers(0)<\/code> etc.,<\/p>\n<p>Using method chaining, the <code>res69<\/code> was obtained of the size after tail() method was applied. Initial list was containing 5 elements, whereas the tail() method gives you the list of 4 elements (from element index 1 to the end, excluding the element at index 0). The size of which was 4.<\/p>\n<p>The last 2 expressions were attempting to retrieve a particular element of the resultant list of tail() method. The <code>tail(0)<\/code> will get the element at 0th index after tail() method &#8211; which is 2 in the list <code>(2,3,4,5)<\/code>, whereas <code>tail(2)<\/code> will get the element at index 2 of the list <code>2,3,4,5<\/code> which is 4. Remember all elements in a collection starts from Index 0.<\/p>\n<p>Let us look at another useful method <strong>isEmpty<\/strong> on a collection which returns true or false depending on the elements available in a collection.<\/p>\n<pre class=\"brush:java;\">scala&gt; numbers.isEmpty\nres64: Boolean = false\n\nscala&gt; numbers.tail.tail.size\nres70: Int = 3\n\nscala&gt; numbers.tail.tail.tail.tail.tail.size\nres71: Int = 0\n\nscala&gt; numbers.tail.tail.tail.tail.tail.isEmpty\nres72: Boolean = true\n\n<\/pre>\n<p>The first expression invoked <code>isEmpty<\/code> on the collection <code>numbers<\/code>, which returns <code>false<\/code> as the underlying collection has a list of values\/elements from 1 to 5. The 2nd expression showed the method chaining of size() method invoked after two successive invocations of <code>tail<\/code> method on the collection <code>numbers<\/code>. The result of whcih is 3.<\/p>\n<p>The 3rd expression applied 5 consecutive tail() methods on the <code>numbers<\/code> which eventually empties the collection and hence the size() method at the last returns 0. Similarly, the 4th expression invokes the <code>isEmpty<\/code> method with the same method chaining of 5 consecutive tail() methods, which in case returns <code>true<\/code>.<\/p>\n<p>You need to be careful while iterating the elements. For example, the below code snippet throws an <code>UnsupportedOperationException<\/code> when the tail method was invoked on an empty list &#8211; due to the consecutive method chanining. Please use them sparingly as they are here shown for the demonstration purpose.<\/p>\n<pre class=\"brush:java;\">scala&gt; numbers.tail.tail.tail.tail.tail.tail.size\njava.lang.UnsupportedOperationException: tail of empty list\n  at scala.collection.immutable.Nil$.tail(List.scala:422)\n  at scala.collection.immutable.Nil$.tail(List.scala:417)\n  ... 33 elided\n<\/pre>\n<h2><a name=\"conclusion\"><\/a>8. Conclusion<\/h2>\n<p>Hope you had enjoyed the Beginner Tutorial of Scala and you got a good taste of it. Please try the examples on your own by exploring the different methods available in the Scala API. You would really enjoy the power of Scala, especially if are coming from a professional Java development background as Scala offers a more consistent and simple way of doing things which is more verbose in Java.<\/p>\n<h2><a name=\"ref\"><\/a>9. References<\/h2>\n<p>You may please refer the following URLs for further reading.<\/p>\n<ol>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Scala_%28programming_language%29\">Scala &#8211; Wikipedia<\/a><\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Criticism_of_Java\">Critism of Java &#8211; Wikipedia<\/a><\/li>\n<li><a href=\"http:\/\/www.scala-lang.org\/download\/\">Download Scala<\/a><\/li>\n<li><a href=\"http:\/\/docs.scala-lang.org\/tutorials\/tour\/tour-of-scala\">Scala Official Tutorial &#8211; Tour of Scala<\/a><\/li>\n<li><a href=\"http:\/\/docs.scala-lang.org\/tutorials\/tour\/unified-types\">Scala Datatypes<\/a><\/li>\n<li><a href=\"http:\/\/www.scala-lang.org\/api\/current\/\">Scala Language API Documentation<\/a><\/li>\n<li><a href=\"http:\/\/www.scala-lang.org\/documentation\/getting-started.html\">Getting Started with Scala<\/a><\/li>\n<\/ol>\n<h2><a name=\"download\"><\/a>10. Download the Source Code<\/h2>\n<p>This is a beginner&#8217;s tutorial to Scala having all the examples tested in the Command Prompt \/ Shell against Scala Version 2.11.7.<\/p>\n<p>The zip file has only the source code of <code>HelloWorld.scala<\/code> where the rest of the examples were executed in <code>REPL Interpreter<\/code>.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <strong><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/scalaTutorialForBeginners.zip\">Scala Tutorial for Beginners<\/a><\/strong>.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this Tutorial article, we will see how to work with Scala Programming language, which is similar to Java but has got lot more advancements, as it was designed to overcome the pitfalls\/shortcomings of Java programming language. According to Wikipedia, the definition of Scala Programming language goes as follows. Scala (SKAH-lah) is a general purpose &hellip;<\/p>\n","protected":false},"author":87,"featured_media":36666,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1462],"tags":[1226],"class_list":["post-36651","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scala","tag-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Scala Tutorial for Beginners - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In this Tutorial article, we will see how to work with Scala Programming language, which is similar to Java but has got lot more advancements, as it was\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala Tutorial for Beginners - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In this Tutorial article, we will see how to work with Scala Programming language, which is similar to Java but has got lot more advancements, as it was\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-04-27T08:00:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-29T12:06:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/scala-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Raghavan Muthu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/itsraghz\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Raghavan Muthu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"25 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/\"},\"author\":{\"name\":\"Raghavan Muthu\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/aa8716fe8bdb3e382d5bdf9b9e960315\"},\"headline\":\"Scala Tutorial for Beginners\",\"datePublished\":\"2016-04-27T08:00:50+00:00\",\"dateModified\":\"2019-03-29T12:06:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/\"},\"wordCount\":4045,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/scala-logo.jpg\",\"keywords\":[\"tutorial\"],\"articleSection\":[\"Scala\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/\",\"name\":\"Scala Tutorial for Beginners - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/scala-logo.jpg\",\"datePublished\":\"2016-04-27T08:00:50+00:00\",\"dateModified\":\"2019-03-29T12:06:27+00:00\",\"description\":\"In this Tutorial article, we will see how to work with Scala Programming language, which is similar to Java but has got lot more advancements, as it was\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/scala-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/scala-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JVM Languages\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/jvm-languages\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Scala\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/jvm-languages\/scala\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Scala Tutorial for Beginners\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/aa8716fe8bdb3e382d5bdf9b9e960315\",\"name\":\"Raghavan Muthu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/Raghavan-Muthu-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/Raghavan-Muthu-96x96.jpg\",\"caption\":\"Raghavan Muthu\"},\"description\":\"Raghavan alias Saravanan Muthu is a seasoned IT professional having more than 2 decades of experience on Java SE\/EE based Application Architecture, Design, Development, Management and Administration for Banking, Insurance, Telecom, HealthCare and Automobile Industries, having a very good hands on experience on Multi-threaded, batch processing applications and Relational Databases. He is currently working as a Director of Engineering for one of the Product based companies in India that delivers the product on Health Care and Insurance Domain. He holds a Post Graduation (Master of Science), and a PG Degree on Big Data Engineering from Birla Institute of Technology and Science (BITS), Pilani, India. He is a Founder, Chief Executive Volunteer and a Web Master of a non-profit charity organization named SHaDE (http:\/\/shade.org.in).\",\"sameAs\":[\"https:\/\/www.raghsonline.com\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/itsraghz\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/raghavan-muthu\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scala Tutorial for Beginners - Java Code Geeks","description":"In this Tutorial article, we will see how to work with Scala Programming language, which is similar to Java but has got lot more advancements, as it was","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/","og_locale":"en_US","og_type":"article","og_title":"Scala Tutorial for Beginners - Java Code Geeks","og_description":"In this Tutorial article, we will see how to work with Scala Programming language, which is similar to Java but has got lot more advancements, as it was","og_url":"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2016-04-27T08:00:50+00:00","article_modified_time":"2019-03-29T12:06:27+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/scala-logo.jpg","type":"image\/jpeg"}],"author":"Raghavan Muthu","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/itsraghz","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Raghavan Muthu","Est. reading time":"25 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/"},"author":{"name":"Raghavan Muthu","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/aa8716fe8bdb3e382d5bdf9b9e960315"},"headline":"Scala Tutorial for Beginners","datePublished":"2016-04-27T08:00:50+00:00","dateModified":"2019-03-29T12:06:27+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/"},"wordCount":4045,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/scala-logo.jpg","keywords":["tutorial"],"articleSection":["Scala"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/","url":"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/","name":"Scala Tutorial for Beginners - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/scala-logo.jpg","datePublished":"2016-04-27T08:00:50+00:00","dateModified":"2019-03-29T12:06:27+00:00","description":"In this Tutorial article, we will see how to work with Scala Programming language, which is similar to Java but has got lot more advancements, as it was","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/scala-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/scala-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/jvm-languages\/scala\/scala-tutorial-beginners\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JVM Languages","item":"https:\/\/examples.javacodegeeks.com\/category\/jvm-languages\/"},{"@type":"ListItem","position":3,"name":"Scala","item":"https:\/\/examples.javacodegeeks.com\/category\/jvm-languages\/scala\/"},{"@type":"ListItem","position":4,"name":"Scala Tutorial for Beginners"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/aa8716fe8bdb3e382d5bdf9b9e960315","name":"Raghavan Muthu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/Raghavan-Muthu-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/01\/Raghavan-Muthu-96x96.jpg","caption":"Raghavan Muthu"},"description":"Raghavan alias Saravanan Muthu is a seasoned IT professional having more than 2 decades of experience on Java SE\/EE based Application Architecture, Design, Development, Management and Administration for Banking, Insurance, Telecom, HealthCare and Automobile Industries, having a very good hands on experience on Multi-threaded, batch processing applications and Relational Databases. He is currently working as a Director of Engineering for one of the Product based companies in India that delivers the product on Health Care and Insurance Domain. He holds a Post Graduation (Master of Science), and a PG Degree on Big Data Engineering from Birla Institute of Technology and Science (BITS), Pilani, India. He is a Founder, Chief Executive Volunteer and a Web Master of a non-profit charity organization named SHaDE (http:\/\/shade.org.in).","sameAs":["https:\/\/www.raghsonline.com\/","https:\/\/x.com\/https:\/\/twitter.com\/itsraghz"],"url":"https:\/\/examples.javacodegeeks.com\/author\/raghavan-muthu\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/36651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=36651"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/36651\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/36666"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=36651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=36651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=36651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}