{"id":25966,"date":"2021-03-10T09:37:00","date_gmt":"2021-03-10T04:07:00","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/"},"modified":"2024-10-24T19:20:09","modified_gmt":"2024-10-24T13:50:09","slug":"scala-tutorial","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/","title":{"rendered":"Scala Tutorial - Learn Scala Step-by-Step Guide"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"what-is-scala\">What is <strong>Scala<\/strong>?<\/h2>\n\n\n\n<p>Scala stands for Scalable Language. It is a multi-paradigm programming language. Scala language includes features of functional programming and object-oriented programming. It is a statically typed language.<strong> <\/strong>Its source code is compiled into bytecode and executed by Java virtual machine(JVM).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"scala-is-object-oriented\"><strong>Scala is object-oriented<\/strong><\/h3>\n\n\n\n<p>Scala is an object-oriented language. In Scala, every value is an object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"scala-execution\"><strong>Scala execution&nbsp;<\/strong><\/h3>\n\n\n\n<p>Scala uses Java Virtual Machine (JVM) to execute bytecode. Its code is compiled into bytecode and executed by Java Virtual Machine. So you need only JVM to start development with it. Scala can also use all java classes and allows us to create our custom class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"why-use-scala\"><strong>Why use Scala?<\/strong><\/h3>\n\n\n\n<p>It is designed to grow with the demands of its user, from writing small scripts to building a massive system for data processing.<\/p>\n\n\n\n<p>Scala is used in Data processing, distributed computing, and web development. It powers the data engineering infrastructure of many companies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"who-uses-scala\"><strong>Who uses Scala?<\/strong><\/h3>\n\n\n\n<p>Scala language is mostly used by Software engineers and Data Engineers. You'll see some data scientists using it with Apache Spark to process huge data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"how-scala-is-different-from-java\"><strong>How Scala is different from Java?<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Nested functions -&nbsp; It allows us to define a function inside another function.<\/li>\n\n\n\n<li>Closures - A function whose return value depends on variables declared outside it of function.<\/li>\n\n\n\n<li>Every value is an object.<\/li>\n\n\n\n<li>Every operation is a method call.<\/li>\n<\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nval sumX = 1.+(3)\nval sumY = 1 + 3\n\n<\/pre><\/div>\n\n\n<p>Output of both is the same.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"popular-frameworks-of-scala\">&nbsp;<strong>Popular frameworks of Scala<\/strong><\/h3>\n\n\n\n<p>Let's understand how we can set up a development environment for Scala. As we know, Scala uses JVM. So in order to execute a Scala program, you need to have java installed on your machine.<\/p>\n\n\n\n<p>sudo apt-get update<\/p>\n\n\n\n<p>Now type: <strong>sudo apt-get install openjdk-8-jdk<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"scala-basics\"><strong>Scala Basics<\/strong><\/h2>\n\n\n\n<p>Scala is very similar to Java&nbsp; Language, and both use Java Virtual Machine(JVM) to execute code. So, learning Scala would be super easy for you if you have a solid understanding of the Java language. But it's not mandatory to know Java before learning the Scala Language.<\/p>\n\n\n\n<p>Let's write our first Scala program!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject HelloWorld {\n  def main(args: Array&#x5B;String]) {\n    println(&quot;Hello world!&quot;)\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Note if you want to execute&nbsp; this using terminal, then follow the below steps.<\/p>\n\n\n\n<p>Save this file as <strong>HelloWorld.scala<\/strong><\/p>\n\n\n\n<p>To compile and run, use the below commands:<\/p>\n\n\n\n<p><strong>\\&gt; scalac HelloWorld.scala<\/strong><\/p>\n\n\n\n<p><strong>\\&gt; scala HelloWorld<\/strong><\/p>\n\n\n\n<p>Let's understand the above code.<\/p>\n\n\n\n<p>It can be noticed that I used the <strong>object<\/strong> keyword instead of class. Scala allows us to create a singleton class using the <strong>object<\/strong> keyword.<\/p>\n\n\n\n<p><strong>object<\/strong> - Scala doesn't use static keywords like Java, instead it allows us to create a singleton object.<\/p>\n\n\n\n<p><strong>def main(args: Array[String])<\/strong> - main() method is compulsory for any Scala Program. Scala starts execution from here.<\/p>\n\n\n\n<p><strong>Case Sensitivity<\/strong> \u2212 It is case-sensitive.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"comments-in-scala\"><strong>Comments in Scala<\/strong><\/h2>\n\n\n\n<p>This language supports single-line and multi-line comments. Multi-line could be nested.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\ndef main(args: Array&#x5B;String]) {\n\n  \/\/println(f&quot;my name is $name%s&quot;)\n  val name:String=&quot;Rhaul roy&quot;\n  \n  \/*\n  println(f&quot;my name is $name%s&quot;)\n  println(f&quot;my name is $name%s&quot;)\n  *\/\n\n\n\n}\n\n<\/pre><\/div>\n\n\n<p>Output: If you run above code, it will just create a string variable and will not produce any output.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-scala-interpreter\"><strong>The Scala Interpreter<\/strong><\/h2>\n\n\n\n<p>The Scala interpreter, an alternative way to run Scala code. Type <strong>scala<\/strong> on your terminal to launch Scala interpreter.<\/p>\n\n\n\n<p>In this Scala tutorial, I will not be using the interactive mode to execute any Scala code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"scala-identifiers\"><strong>Scala Identifiers<\/strong><\/h2>\n\n\n\n<p>Identifiers are nothing but names used for objects, classes, variables and methods. The important thing is, a keyword cannot be used as an identifier.&nbsp;<\/p>\n\n\n\n<p>Scala has four types of identifiers.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"alphanumeric-identifiers\"><strong>Alphanumeric Identifiers<\/strong><\/h4>\n\n\n\n<p>These kinds of identifiers start with a letter or an underscore, which can be followed by letters, digits, or underscores.&nbsp;<\/p>\n\n\n\n<p>Note - The '$' character is a reserved keyword.<\/p>\n\n\n\n<p>Valid identifiers&nbsp; example<\/p>\n\n\n\n<p>sum, _count,&nbsp; __1_salary<\/p>\n\n\n\n<p>Invalid illegal identifiers example<\/p>\n\n\n\n<p>$sum, 1count, -index<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"operator-identifiers\"><strong>Operator Identifiers<\/strong><\/h4>\n\n\n\n<p>These kinds of operator identifiers consist of one or more operator characters.&nbsp;<\/p>\n\n\n\n<p>Valid operator identifiers&nbsp;<\/p>\n\n\n\n<p><strong>+&nbsp; ++ :::<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"mixed-identifiers\"><strong>Mixed Identifiers<\/strong><\/h4>\n\n\n\n<p>It consists of an alphanumeric identifier, which is followed by an underscore and an operator identifier.<\/p>\n\n\n\n<p>Valid mixed identifiers<\/p>\n\n\n\n<p>unary_-<\/p>\n\n\n\n<p><strong>unary_-<\/strong>&nbsp; used as a method name defines a unary<strong> - <\/strong>operator. Scala supports unary prefix operators (-,&nbsp;+,&nbsp;~,&nbsp;!)&nbsp; only.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"literal-identifier\"><strong>Literal identifier<\/strong><\/h4>\n\n\n\n<p>A literal identifier is an arbitrary string enclosed in backticks (` . . . `).<\/p>\n\n\n\n<p>Example:&nbsp;<\/p>\n\n\n\n<p>`xyz`&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"scala-keywords\"><strong>Scala Keywords<\/strong><\/h2>\n\n\n\n<p>Keywords are nothing but reserved words in Scala. The following table shows the reserved words in Scala.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>abstract<\/td><td>case<\/td><td>catch<\/td><td>class<\/td><\/tr><tr><td>def<\/td><td>do<\/td><td>else<\/td><td>extends<\/td><\/tr><tr><td>false<\/td><td>final<\/td><td>finally<\/td><td>for<\/td><\/tr><tr><td>forSome<\/td><td>if<\/td><td>implicit<\/td><td>import<\/td><\/tr><tr><td>lazy<\/td><td>match<\/td><td>new<\/td><td>Null<\/td><\/tr><tr><td>object<\/td><td>override<\/td><td>package<\/td><td>private<\/td><\/tr><tr><td>protected<\/td><td>return<\/td><td>sealed<\/td><td>super<\/td><\/tr><tr><td>this<\/td><td>throw<\/td><td>trait<\/td><td>Try<\/td><\/tr><tr><td>true<\/td><td>type<\/td><td>val<\/td><td>Var<\/td><\/tr><tr><td>while<\/td><td>with<\/td><td>yield<\/td><td>&nbsp;<\/td><\/tr><tr><td>-<\/td><td>:<\/td><td>=<\/td><td>=&gt;<\/td><\/tr><tr><td>&lt;-<\/td><td>&lt;:<\/td><td>&lt;%<\/td><td>&gt;:<\/td><\/tr><tr><td>#<\/td><td>@<\/td><td><\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-types\"><strong>Data Types<\/strong><\/h2>\n\n\n\n<p>A data type is a categorization of data that tells the compiler which type of value a variable holds. For example, a variable may hold an integer value or float. Like java, Scala doesn\u2019t have any primitive data types. It comes with the standard numeric data types. In Scala, all data types are full-blown objects.<\/p>\n\n\n\n<p>Basic data types example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n\n  def main(args: Array&#x5B;String]) {\n\n      val a: Byte = 2\n      val b: Int = 444\n      val c: Long = 324324\n      val d: Short = 5\n      val e: Double = 2.0\n\n  }\n\n}\n\n<\/pre><\/div>\n\n\n<p>In the above example,&nbsp;<strong>val&nbsp;<\/strong>is a keyword which means that the value of variable&nbsp;<strong>\u201ca\u201d<\/strong>&nbsp;cannot be changed. Just after \u201c:\u201d data type of variable \u201ca\u201d has been mentioned.<\/p>\n\n\n\n<p>If you don\u2019t mention data type. The compiler will automatically detect its data type.<\/p>\n\n\n\n<p><strong>val<\/strong>&nbsp;j =10 \/\/ defaults to Int<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"scala-basic-literals\"><strong>Scala basic literals<\/strong><\/h3>\n\n\n\n<p><strong>Integral Literals<\/strong> - The following are Integral literals:<\/p>\n\n\n\n<p>22<\/p>\n\n\n\n<p>02<\/p>\n\n\n\n<p>21&nbsp;<\/p>\n\n\n\n<p>0777L<\/p>\n\n\n\n<p><strong>Floating Point Literal - <\/strong>The following are floating point literals:<\/p>\n\n\n\n<p>11.0&nbsp;<\/p>\n\n\n\n<p>7.14159f&nbsp;<\/p>\n\n\n\n<p>1.0e100<\/p>\n\n\n\n<p>.1<\/p>\n\n\n\n<p><strong>Boolean Literals - <\/strong>Only<strong> true<\/strong>&nbsp;and&nbsp;<strong>false<\/strong>&nbsp;are members of Boolean.<\/p>\n\n\n\n<p><strong>Data types and its range<\/strong><\/p>\n\n\n\n<p>Below table shows data type and its possible values or range.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Data Type<\/strong><\/td><td><strong>Possible Values<\/strong><\/td><\/tr><tr><td>Boolean<\/td><td>true&nbsp;or&nbsp;false<\/td><\/tr><tr><td>Byte<\/td><td>8 bit signed value. Range from -128 to 127<\/td><\/tr><tr><td>Short<\/td><td>16 bit signed value. Range -32768 to 32767<\/td><\/tr><tr><td>Int<\/td><td>32 bit signed value. Range -2147483648 to 2147483647<\/td><\/tr><tr><td>Long<\/td><td>64 bit signed value. -9223372036854775808 to 9223372036854775807<\/td><\/tr><tr><td>Float<\/td><td>32 bit IEEE 754 single-precision float<\/td><\/tr><tr><td>Double<\/td><td>64 bit IEEE 754 double-precision float<\/td><\/tr><tr><td>Char<\/td><td>16 bit unsigned Unicode character. Range from U+0000 to U+FFFF<\/td><\/tr><tr><td>String<\/td><td>A sequence of&nbsp;Char<\/td><\/tr><tr><td>Unit<\/td><td>Corresponds to no value<\/td><\/tr><tr><td>Null<\/td><td>null or empty reference<\/td><\/tr><tr><td>Nothing<\/td><td>The subtype of every other type; includes no values<\/td><\/tr><tr><td>Any<\/td><td>The supertype of any type; any object is of type <em>An<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"escape-sequences\"><strong>Escape Sequences<\/strong><\/h2>\n\n\n\n<p>Below is the escape sequence in Scala.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Escape Sequences<\/strong><\/td><td><strong>Unicode<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td>\\b<\/td><td>\\u0008<\/td><td>backspace BS<\/td><\/tr><tr><td>\\t<\/td><td>\\u0009<\/td><td>horizontal tab HT<\/td><\/tr><tr><td>\\n<\/td><td>\\u000c<\/td><td>formfeed FF<\/td><\/tr><tr><td>\\f<\/td><td>\\u000c<\/td><td>formfeed FF<\/td><\/tr><tr><td>\\r<\/td><td>\\u000d<\/td><td>carriage return CR<\/td><\/tr><tr><td>\\\"<\/td><td>\\u0022<\/td><td>double quote \"<\/td><\/tr><tr><td>\\'<\/td><td>\\u0027<\/td><td>single quote .<\/td><\/tr><tr><td>\\\\<\/td><td>\\u005c<\/td><td>backslash \\<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>String and Char<\/strong><\/p>\n\n\n\n<p>String is nothing but a sequence of characters.<\/p>\n\n\n\n<p>Multi- line String in Scala<\/p>\n\n\n\n<p>Example&nbsp;<\/p>\n\n\n\n<p>\"\"\" This is&nbsp;<\/p>\n\n\n\n<p>multi-line<\/p>\n\n\n\n<p>&nbsp;string\"\"\"<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"variables-in-scala\"><strong>Variables in Scala<\/strong><\/h2>\n\n\n\n<p>Variables are reserved memory locations where values are stored. Which can be referred later in the program.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Scala has two kinds of variables.<\/p>\n\n\n\n<p>1. Immutable<br>2. Mutable<\/p>\n\n\n\n<p>Immutable variables - These kinds of variables can't&nbsp; be changed. It is declared using the Val keyword.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<p>val name:String=\"Rahul\"<\/p>\n\n\n\n<p>Mutable variables - These kinds of variables can be changed. It is declared using <strong>var<\/strong> keyword.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<p>var name:String=\"Rahul\"&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"data-type\"><strong>Data Type&nbsp;<\/strong><\/h2>\n\n\n\n<p>Data type of any variable is mentioned just after the variable name. In this case, if you don't mention the data type. The Scala compiler can detect the type of the variable based on the value assigned to it. This is called variable type inference.<\/p>\n\n\n\n<p><strong>Syntax&nbsp;<\/strong><\/p>\n\n\n\n<p>val VarName : DataType = [Initial Value]<\/p>\n\n\n\n<p>or<\/p>\n\n\n\n<p>var VarName : DataType = [Initial Value]<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>var sum :Int=0;<\/p>\n\n\n\n<p>Here, sum is a variable and Int is a data type of the variable.<\/p>\n\n\n\n<p>var sum =0;<\/p>\n\n\n\n<p>Here, Scala compiler will automatically detect the type of sum variable. This feature is called variable type inference.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"scope-of-variables\"><strong>Scope of Variables&nbsp;<\/strong><\/h2>\n\n\n\n<p>There are three types of scope in Scala-<\/p>\n\n\n\n<p><strong>Fields <\/strong>- These variables are accessible from every method in the object. And even from outside the object if the access modifier is not private for that variable. Access modifiers will be covered later in detail. Variables could be mutable or immutable depending upon the <strong>var<\/strong> and <strong>Val<\/strong> keywords.<\/p>\n\n\n\n<p><strong>Method Parameters<\/strong> - Whenever we call a method, we might pass a few values as parameters. These variables can only be accessed inside and outside the method if there is any reference to the object from outside the method else not. These variables are always mutable.&nbsp;<\/p>\n\n\n\n<p><strong>Local Variables<\/strong> - These kinds of variables are declared inside a method. And it can only be accessed within the method. These could be both mutable &amp; immutable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"if-else-statement\"><strong>If else statement<\/strong><\/h2>\n\n\n\n<p>If else is used to make a decision based on conditions. In this, a piece of code is executed or not executed based on a specified condition. This controls the flow of execution of the program.<\/p>\n\n\n\n<p>The following are flow control statements in Scala :<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>if<\/li>\n\n\n\n<li>if-else<\/li>\n\n\n\n<li>Nested if-else<\/li>\n\n\n\n<li>if-else if ladder<\/li>\n<\/ul>\n\n\n\n<p>if statement - This is the simplest decision-making statement. It only contains \"if&nbsp; block\u201d which is executed If the specified condition is true and&nbsp; If the specified condition is false then \u201cIf block\u201d will not be executed.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]): Unit = {\n    val age:Int=11\n    if(age&gt;10)\n      {\n        print(&quot;true&quot;)\n      }\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>if else statement - It contains two different blocks \u201cif block\u201d and \u201celse block\u201d. If the specified condition is true then \u201cif block\u201d is executed and if condition is not true then \u201celse block\u201d is executed.&nbsp;<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]): Unit = {\n    val age:Int=10\n    if(age&gt;10)\n      {\n        print(&quot;true&quot;)\n      }else\n      print(&quot;false&quot;)\n  }\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Nested if-else<strong> - <\/strong>Scala allows us to define&nbsp; <strong>if-else<\/strong>&nbsp;statement inside an if statement or in a else statement.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]): Unit = {\n    val age:Int=10;\n    if(age&gt;10)\n      {\n        if(age==10)\n          {\n            print(&quot;Age is 10&quot;)\n          }\n          else {\n            print(&quot;Not equal to 10&quot;);\n     }\n      }else {\n     print(&quot;Age is not grater than 10&quot;)\n\n    }\n    \n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>if-else if ladder<strong>&nbsp; - <\/strong>This is useful when you have multiple conditions. And at a time, only one condition could be true. In this, the <em>if<\/em> statements are executed from the top down. As soon as one of the conditions is true, the statement associated with that <em>if<\/em> is executed, and the rest of the ladder is skipped. In case if none of them is true, then \u201celse block\u201d is executed.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]): Unit = {\n    val age:Int=10;\n\n     if(age==10)\n      {\n       print(&quot;age is 10&quot;)\n      }else if(age==18) {\n      print(&quot;age is 19&quot;)\n      }\n     else\n      print(&quot;not matched &quot;)\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"loops\"><strong>Loops&nbsp;<\/strong><\/h2>\n\n\n\n<p>Loops are a very important part of any programming language. Which allows us&nbsp; to execute a block of code several number of times.&nbsp;<\/p>\n\n\n\n<p>The following are loop control statements in Scala:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>While<\/li>\n\n\n\n<li>Do while<\/li>\n\n\n\n<li>For&nbsp;<\/li>\n<\/ol>\n\n\n\n<p>While<strong> <\/strong>- It executes a statement or group of statements while a given condition is true. The condition is tested before executing any statement of the loop body.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]): Unit = {\n\n    var index: Int = 0\n      while(index&lt;10)\n      {\n        print(index);\n        index=index+1 \n      }\n\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Do While<strong> <\/strong>\u2013&nbsp; It is similar to while loop the only difference is it executes the body once and then it tests the condition.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]): Unit = {\n\n     var index: Int = 0\n      do\n      {\n        print(index);\n        index=index+1\n      }while(index&lt;10)\n\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>For&nbsp; \u2013&nbsp; It is often used with collection objects. It loops through a block of code a number of times. I will cover the collection library later in detail.<\/p>\n\n\n\n<p>Here&nbsp; <strong>\u2190<\/strong>&nbsp; is an operator which is called a&nbsp;generator.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]): Unit = {\n\n    \/\/ for with range\n    for( a &lt;- 1 to 20){\n      println( &quot;Value : &quot; + a );\n    }\n\n    \/\/ print all elements of a list\n    val list1:List&#x5B;String]=List(&quot;Apple&quot;,&quot;banana&quot;)\n    for(ele &lt;- list1){\n      println( &quot;ele : &quot; + ele );\n    }\n\n    \n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"class-object\"><strong>Class &amp; object<\/strong><\/h2>\n\n\n\n<p>In this chapter, we are going to cover class &amp; objects in Scala. If you are familiar with any object-oriented programming(OOPs) language then it would be super easy for you to understand concepts related to object &amp; class.<\/p>\n\n\n\n<p>Let's get started with the definition of class and object.&nbsp;<\/p>\n\n\n\n<p>Class - A class is a blueprint of an object.<\/p>\n\n\n\n<p>Object - Object is nothing but an instance of a class.&nbsp;<\/p>\n\n\n\n<p>So, the first step is to define a class and then create an object of that class using a new<strong> <\/strong>keyword. Once you have an object, you can call any method of the class.<\/p>\n\n\n\n<p>Let's understand this by taking an example of <strong>Employee<\/strong> class. As you can see the Employee class has ID and Name as member variables. It also has <strong>setID() <\/strong>and <strong>setName()<\/strong> as Member methods.<\/p>\n\n\n\n<p>Here, Rahul and Chandan are objects of Employee class.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nclass Employee(idc:Int,namec:String) {\n    var ID:Int=idc\n    var name:String=namec\n\n    def getName(): String =\n    {\n       name\n    }\n\n}\n\nobject Main {\n  def main(args: Array&#x5B;String]): Unit = {\n    val emp1:Employee =new Employee(11,&quot;rahul&quot;)\n    val emp2:Employee =new Employee(11,&quot;Chandan&quot;)\n    print(emp1.getName())\n\n  }\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>It printed \u201cRahul\u201d because we only checked the name of one employee.<\/p>\n\n\n\n<p>Here, we created an Employee class with two data members and one member method.&nbsp;<\/p>\n\n\n\n<p><strong>Employee(idc:Int, namec:String) \u2013 <\/strong>This works as a constructor for the class.<\/p>\n\n\n\n<p><strong>new Employee(11,\u201dRahul \u201d)&nbsp; -&nbsp; <\/strong>This statement creates an object<strong>.<\/strong><\/p>\n\n\n\n<p><strong>getName() -&nbsp; <\/strong>Returns name of the employee.<\/p>\n\n\n\n<p>Note \u2013 There is no return keyword inside <strong>getName()<\/strong> method. Actually the last expression&nbsp; becomes the return value for the method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"inheritance\"><strong>Inheritance<\/strong><\/h2>\n\n\n\n<p>This is one of the important OOPS concepts. It allows us to inherit properties of another class using an extended keyword.<\/p>\n\n\n\n<p>Super class or parent class -&nbsp; A class which is extended called super or parent class.<\/p>\n\n\n\n<p>Base class or derived&nbsp; class - A class which extends a class is called derived or base class.<\/p>\n\n\n\n<p>Let\u2019s take a real life example to understand this.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nclass Google {\n \n def search(keyword: String): Unit ={\n   println(&quot;Your Search keyword:&quot;+keyword);\n   println(&quot;Searching please wait....&quot;);\n }\n \n def youtube(url: String): Unit ={\n   println(&quot;Your Video URL:&quot;+url);\n   println(&quot;Playing....&quot;);\n }\n \n}\n \nclass User(serviceT:String,inputT:String) extends Google{\n \n var service:String = serviceT\n var input:String = inputT\n \n def action(): Unit =\n {\n     if(service.equals(&quot;search&quot;)){\n       search(input);\n     }else{\n       youtube(input);\n     }\n \n }\n \n}\n \nobject Main {\n def main(args: Array&#x5B;String]): Unit = {\n   val user1: User = new User(&quot;search&quot;,&quot;what is Scala?&quot;);\n   user1.action()\n   val user2: User = new User(&quot;youtube&quot;,&quot;https:\/\/www.youtube.com\/watch?v=i9o70PMqMGY&quot;);\n   user2.action()\n \n }\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>In the above example, we have considered two classes Google class and User class. User is a derived class. This class will inherit all non-private members of the Google class, such as <strong>search<\/strong> and <strong>youtube <\/strong>methods. So when we call the \u201caction\u201d method of User class, then it uses Google class methods to perform the action. This is possible because of the inheritance feature supported by OOPs languages.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Scala supports different types of inheritance, including single, multilevel, multiple, and hybrid.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"singleton-objects\"><strong>Singleton Objects<\/strong><\/h2>\n\n\n\n<p>As we know, Scala is a&nbsp;<strong>pure object<\/strong>-<strong>oriented language<\/strong>&nbsp;because every value is an&nbsp;<strong>object<\/strong> in Scala. Instead&nbsp; of static members Scala offers <strong>singleton objects.<\/strong> A singleton is&nbsp; nothing but a class that can have only one instance. In this type of class you need not to create an object to call methods declared inside a singleton object.<\/p>\n\n\n\n<p>It is possible to create <strong>singleton <\/strong>objects by just using<strong> object <\/strong>keyword Instead&nbsp; of a class keyword. You have already seen the singleton<strong> class, <\/strong>which we call<strong> <\/strong>Scala's main method. Below is an example of <strong>singleton class.<\/strong><\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n\n  def main(args: Array&#x5B;String]): Unit = {\n\n    print(&quot;singleton object&quot;)\n}}\n \n\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"access-modifiers\"><strong>Access Modifiers&nbsp;<\/strong><\/h2>\n\n\n\n<p><strong>Access Modifiers<\/strong>&nbsp;are nothing but a set of keywords in Scala which controls the&nbsp;<strong>accessibility<\/strong>&nbsp;of classes, methods, and other members. Scala provides the following keywords.<\/p>\n\n\n\n<p>1.Private&nbsp;<\/p>\n\n\n\n<p>2.Protected<\/p>\n\n\n\n<p>3.Public (not a keyword )<\/p>\n\n\n\n<p>Private \u2013 When any&nbsp; member is declared as private, it can only be used inside defining class.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n \nclass Employee {\n  private val name:String=&quot;Rahul kr&quot;\n  def getName(): String ={\n    name\n  }\n\n}\nobject Main {\n  def main(args: Array&#x5B;String]): Unit = {\n    val emp: Employee = new Employee();\n    print(emp.getName()) \n    print(emp.name) \/\/ not accessible \n  }\n}\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Protected &nbsp;\u2013 Protected members are only accessible in the class itself and its subclasses.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nclass Employee {\n  protected val name:String=&quot;Rahul kr&quot;\n}\nclass Programmer extends Employee {\n\n  def getName(): String ={\n    name\n  }\n}\nobject Main {\n  def main(args: Array&#x5B;String]): Unit = {\n    val emp: Programmer = new Programmer();\n    print(emp.getName())\n\n    val emp1: Employee = new Employee();\n    print(emp1.name) \/\/ not accessable\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Public \u2013 Public is not a keyword. By default, all members are public and can be accessed from anywhere.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nclass Employee {\n   val name:String=&quot;Rahul kr&quot;\n}\nobject Main {\n  def main(args: Array&#x5B;String]): Unit = {\n    val emp1: Employee = new Employee();\n    print(emp1.name)\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>*Top-level&nbsp;protected&nbsp;and&nbsp;private&nbsp;members are accessible from inside the package.<\/p>\n\n\n\n<p><strong>Companion - &nbsp;<\/strong>It is a singleton object named same as the class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"scala-functions\"><strong>Scala Functions<\/strong><\/h2>\n\n\n\n<p>Scala is a pure Object-oriented programming language. But it also supports a functional programming approach. So, It provides built-in functions and allows us to create user-defined functions as well. Functions are first-class values in Scala. It means, it is possible to store function value, pass a function as an argument, and return a function as a value from another function.&nbsp;<\/p>\n\n\n\n<p><strong>def<\/strong> keyword is used to create a function in Scala.<\/p>\n\n\n\n<p>Basic Syntax:&nbsp;<\/p>\n\n\n\n<p><strong>def<\/strong>&nbsp;funName(parameters&nbsp;:&nbsp;type)&nbsp;:&nbsp;return type&nbsp;=&nbsp;{&nbsp;&nbsp;<br>\/\/&nbsp;statements<br>}&nbsp;&nbsp;<\/p>\n\n\n\n<p>Let\u2019s create a simple Scala function!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n\n  def getAge():Int  = {\n    val age:Int=10\n    age\n  }\n\n  def main(args: Array&#x5B;String]): Unit = {\n   print(getAge())\n\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Above code, created a function called <strong>getAge()<\/strong> which returns a value.<\/p>\n\n\n\n<p>Anonymous function - Any function without a name is called an anonymous function. Below is an example of an anonymous function. It takes two integers and prints the sum.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\ndef main(args: Array&#x5B;String]): Unit = {\n  \n  val res1=(a:Int,b:Int) =&gt; a+b\n  val res2=(_:Int )+( _:Int)\/\/ same as above\n  print(res1(1,2))\n  print(res2(1,2))\n  \n}\n\n<\/pre><\/div>\n\n\n<p>Output: 3 3<\/p>\n\n\n\n<p>Nested function \u2013 Scala allows us to define a function within another function.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s understand this by an example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  \n  def multiply(a:Int,b:Int,c:Int): Int ={\n\n      def multi(x:Int,y:Int): Int =\n      {\n        x*y\n      }\n      c* multi(a,b);\n  }\n\n  def main(args: Array&#x5B;String]): Unit = {\n    println(multiply(1,2,3))\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output: 6<\/p>\n\n\n\n<p>Here, multi is a function defined inside another function.&nbsp;<\/p>\n\n\n\n<p>Currying Functions \u2013 Currying function is a technique of transforming a function that takes multiple parameters into a chain of functions, each taking a single parameter.&nbsp;<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  \n  def main(args: Array&#x5B;String]): Unit = {\n    \n    def add(a:Int)=(b:Int)=&gt; a+b;\n     \/\/ can also use below\n    def add1(a:Int)(b:Int) = a+b;\n\n    println(add(1)(2))\n    println(add1(1)(2))\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Recursion function \u2013 Recursion is a very important concept of pure functional programming. Recursion happens when&nbsp; a function can call itself repeatedly.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def printFun(n:Int): Unit ={\n    if(n&lt;0)\n      return ;\n    println(n)\n    printFun(n-1)\n  }\n\n  def main(args: Array&#x5B;String]): Unit = {\n    printFun(3);\n  }\n}\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Functions with Named Arguments \u2013 Generally, while calling a function the order of argument is very important. But a named argument allows us to pass arguments to a function in any order.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Demo {\n  def printInt( a:Int, b:Int ) = {\n    println(&quot;a : &quot; + a );\n    println(&quot;b : &quot; + b );\n  }\n  def main(args: Array&#x5B;String]) {\n    printInt(b = 5, a = 7);\n  }\n  \n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Partially Applied Functions \u2013 This is a technique to generate a brand new function from an existing function. As we know, whenever we call any function we need to provide all parameters to the function. But Scala allows us to provide only some of them, leaving remaining parameters to be passed later.<\/p>\n\n\n\n<p>Once you give the required initial parameters, you get back a new function whose parameter list only contains those parameters from the original function that were left blank.<\/p>\n\n\n\n<p>Let\u2019s understand this by taking an example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n\n   def getPrice(discount:Float,original_price:Float): Float =\n   {\n     original price - (original_price * discount \/ 100)\n   }\n  def main(args: Array&#x5B;String]) {\n     println(getPrice(20,100))\n     \/*\n     if 10 % discount is fixed for all product\n     then we can create another function with fixed discount\n     *\/\n     val getPriceWithFixedDiscount=getPrice(10,_:Float)\n     println(getPriceWithFixedDiscount(100))\n  }\n\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Here,&nbsp; we created a function <strong>getPrice()<\/strong> which returns the price after subtracting the discount from the original price. Now, suppose the discount is fixed for all products then probably we can create&nbsp; another function where you need to pass only original_price.&nbsp;<\/p>\n\n\n\n<p>Below statement creates<strong> getPriceWithFixedDiscount()<\/strong> function from <strong>getPrice().<\/strong>&nbsp;<\/p>\n\n\n\n<p><strong>val getPriceWithFixedDiscount=getPrice(10,_:Float);<\/strong><\/p>\n\n\n\n<p>Now, call it like this and provide only the original price.<\/p>\n\n\n\n<p><strong>getPriceWithFixedDiscount(100)<\/strong><\/p>\n\n\n\n<p>Call-by-name - Generally, parameters to functions are call-by-value parameters. It means the value of the parameter is calculated before it is passed to the function. But in call-by-name a code block is passed to the function and each time the function accesses the parameter, the code block is executed and the value is calculated.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  \n  def byName(x: =&gt; Long)\n  {\n    println(x)\n    println(x)\n  }\n  def main(args: Array&#x5B;String]) {\n    byName(System.nanoTime())\n  }\n\n}\n\n\n<\/pre><\/div>\n\n\n<p>Output :&nbsp;<\/p>\n\n\n\n<p>29951397835608<\/p>\n\n\n\n<p>29951400165187<\/p>\n\n\n\n<p>Here, we created a <strong>byName()<\/strong> function and used =&gt; symbol to perform call-by-name and this function contains two print statements. Now, the question is when we called <strong>byName<\/strong> function only once. Then why it printed different values. The reason is whenever we assess the value of x it is called <strong>System.<\/strong><strong><em>nanoTime<\/em><\/strong><strong>()<\/strong> method which was passed while calling the function.<\/p>\n\n\n\n<p>Function with Variable Arguments \u2013 This is useful when you don\u2019t know how many arguments you are going to pass to a function.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def sum(args: Int *):Int=\n  {\n   var sum:Int=0;\n    for(value &lt;- args) {\n      sum=sum+value\n    }\n    sum\n  }\n  def main(args: Array&#x5B;String]) {\n      println(sum(1,2,3,4))\n      println(sum(4,5,6))\n  }\n\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Just use * just after the data type to denote more than one argument.<\/p>\n\n\n\n<p>Default Parameter Values for a Function -&nbsp; It allows to set default values for parameters<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n\n  \/\/with default value \n  def sub(a:Int=0,b:Int=0):Int= {\n    a-b;\n  }\n\n  def main(args: Array&#x5B;String]): Unit = {\n\n     print(sub(1,2))\n     print(sub(1))\n  }\n\n\n}\n\n\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>In the above example, <strong>the sub ()<\/strong> function has been defined with two parameters a and b. The default value for these parameters is zero. You can also notice that there is no return keyword. Because the last expression becomes the return value for the method.&nbsp;<\/p>\n\n\n\n<p><strong>Note <\/strong>\u2013 If you don\u2019t use \u201c=\u201d your function will not return anything and will work like a subroutine.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"higher-order-functions\"><strong>Higher-order functions<\/strong><\/h2>\n\n\n\n<p>Higher order functions are those functions that can either receive a function as an argument<\/p>\n\n\n\n<p>or returns a function. This feature is not available in java.<\/p>\n\n\n\n<p>Let\u2019s see a couple of examples.<\/p>\n\n\n\n<p>Example: Function as an argument<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n\n  def main(args: Array&#x5B;String]): Unit = {\n    higherOrderFunction(add)\n  }\n\n  def higherOrderFunction(f:(Int,Int)=&gt;Int):Unit = {\n      println(f(11,11))                                    \n  }\n\n  def add(a:Int,b:Int):Int = {\n    a+b\n  }\n  \n  \n\n<\/pre><\/div>\n\n\n<p>Output:&nbsp;22<\/p>\n\n\n\n<p>In the above code, we created a function called <strong>higherOrderFunction <\/strong>and instead of passing a value, we passed a function called add() as an argument<strong>. <\/strong>And this <strong>higherOrderFunction<\/strong> doesn\u2019t return anything.<\/p>\n\n\n\n<p><strong>f:(Int,Int)=&gt;Int&nbsp; -&nbsp; <\/strong>It means the function will take two integers and will return an integer. This definition should match with the function that you are going to pass as an argument to another function.<\/p>\n\n\n\n<p>Example: Function returns a function<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n\n  def main(args: Array&#x5B;String]): Unit = {\n\n    val add = higherOrderFunction()\n    add(1,4)\n  }\n\n  def higherOrderFunction()= {\n    (a:Int,b:Int) =&gt; println(a+b)\n  }\n  \n}\n\n<\/pre><\/div>\n\n\n<p>Output \u2013 5<\/p>\n\n\n\n<p>In the above example, we created a <strong>higherOrderFunction<\/strong> which returns a function which can add two elements.<\/p>\n\n\n\n<p><strong>val add = <\/strong><strong><em>higherOrderFunction<\/em><\/strong><strong>()- <\/strong>This line&nbsp; stores the return of higherOrderFunction into a variable called add.<\/p>\n\n\n\n<p><strong>add(1,4)<\/strong>- As the return type of higherOrderFunction function is function.&nbsp; So add() function&nbsp; is called with arguments.<\/p>\n\n\n\n<p><strong>(a:Int,b:Int) =&gt; println(a+b) <\/strong>-&nbsp; This is a function without a name called anonymous function. It takes two integers and prints the sum.<\/p>\n\n\n\n<p>Closure&nbsp;<\/p>\n\n\n\n<p>A&nbsp;<strong>closure<\/strong>&nbsp;is a function, whose return value depends on the value of one or more variables declared outside this function.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  var increment  = 1\n  val add = (i:Int) =&gt; i + increment\n  def main(args: Array&#x5B;String]) {\n    println( add(2) )\n  }\n\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Here, increment is a free variable(defined outside ) <strong>and&nbsp; add()<\/strong> is a <strong>closure<\/strong>&nbsp;function.&nbsp; If the closure function changes increment value then the change becomes visible outside the function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"string\"><strong>String&nbsp;<\/strong><\/h2>\n\n\n\n<p>Sting is a very common data structure. It is nothing but a sequence of characters. In Scala, objects of String are immutable which means we can\u2019t change once created.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s see how to create a string and its common functions.<\/p>\n\n\n\n<p><strong>indexOf(int index)<\/strong> - Returns the index of the specified character.<\/p>\n\n\n\n<p><strong>charAt(int index)<\/strong> - Returns the character at the specified index.<\/p>\n\n\n\n<p><strong>Length <\/strong>\u2013 Returns the length of the string.<\/p>\n\n\n\n<p><strong>String[] split(String regex)<\/strong> - Splits this string around matches of the given regular expression.<\/p>\n\n\n\n<p><strong>string1.concat(string2)-<\/strong> this returns a new string that is string1 with string2 added to it at the end.&nbsp;<\/p>\n\n\n\n<p><strong>string1.equals(string2<\/strong>)-&nbsp; compares two strings, returns true if both strings are same else false<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n\n  def main(args: Array&#x5B;String]) {\n    val name1:String=&quot;Shyam kumar&quot;\n    val name2:String=&quot;Shyam&quot;\n    println(name1.indexOf(&#039;S&#039;))\n    println(name1.length)\n    val str:Array&#x5B;String]=name1.split(&#039; &#039;)\n    str.foreach(x=&gt;println(x))\n    println(name1.charAt(0))\n    println(name1.toLowerCase)\n    println(name2.concat(&quot; kumar&quot;) )\n    println(name1 )\n    println(name1.equals(name2) )\n\n  }\n\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output :<\/p>\n\n\n\n<p>0<\/p>\n\n\n\n<p>11<\/p>\n\n\n\n<p>Shyam<\/p>\n\n\n\n<p>kumar<\/p>\n\n\n\n<p>S<\/p>\n\n\n\n<p>shyam kumar<\/p>\n\n\n\n<p>Shyam kumar&nbsp;<\/p>\n\n\n\n<p>Shyam kumar<\/p>\n\n\n\n<p>False<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"string-interpolation\"><strong>String Interpolation<\/strong><\/h2>\n\n\n\n<p>String Interpolation is the new way to create Strings in Scala programming language. This feature supports the versions of Scala-2.10 and later.&nbsp;<\/p>\n\n\n\n<p><strong>The \u2018s\u2019 String Interpolator - <\/strong>The literal \u2018s\u2019 allows us to use a variable directly in a string, when you prepend \u2018s\u2019 to it.&nbsp;<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  \n  def main(args: Array&#x5B;String]) {\n    val name:String=&quot;Rhaul roy&quot;\n    println(s&quot;my name is $name&quot;)\n\n  }\n\n}\n\n<\/pre><\/div>\n\n\n<p>Output: my name is Rahul Roy<\/p>\n\n\n\n<p><strong>The&nbsp;f&nbsp;Interpolator<\/strong> - Prepending&nbsp;<strong>f&nbsp;<\/strong>to any string literal allows the creation of simple formatted strings, similar to&nbsp;printf&nbsp;in other languages. When using the&nbsp;f&nbsp;interpolator, all variable references should be followed by a&nbsp;printf-style format string, like&nbsp;%s for&nbsp; string.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n\n  def main(args: Array&#x5B;String]) {\n    val name:String=&quot;Rhaul roy&quot;\n    println(f&quot;my name is $name%s&quot;)\n  }\n\n}\n\n<\/pre><\/div>\n\n\n<p>Output: my name is Rhaul roy<\/p>\n\n\n\n<p><strong>\u2018raw\u2019 Interpolator<\/strong> - The \u2018raw\u2019 interpolator is similar to \u2018s\u2019 interpolator except that it performs no escaping of literals within a string.&nbsp;<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n\n  def main(args: Array&#x5B;String]) {\n    val name:String=&quot;Rhaul roy&quot;\n    println(raw&quot;my \\n name is $name&quot;)\n  }\n\n}\n\n<\/pre><\/div>\n\n\n<p>output: my \\n name is Rhaul roy<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"array\"><strong>Array&nbsp;<\/strong><\/h2>\n\n\n\n<p>Scala provides an array data structure. which stores a&nbsp; sequential collection of elements of the same type. The size of an array is fixed.&nbsp;<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<p>var temp:Array[String] = new Array[String](3)<\/p>\n\n\n\n<p>or<\/p>\n\n\n\n<p>var temp = new Array[String](3)<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]): Unit = {\n\n    \/\/ print all elements of a array\n    val arr:Array&#x5B;String]=Array(&quot;Apple&quot;,&quot;banana&quot;)\n    for(ele &lt;- arr){\n      println( &quot;ele : &quot; + ele );\n    }\n    \n  }\n}\n \n \n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Two-Dimensional Arrays \u2013 Two-Dimensional is nothing but an array of arrays. Data in this kinds of arrays are stored in tabular format.<\/p>\n\n\n\n<p>var matrix = ofDim[Int](3,3)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nimport Array._\nobject Main {\n  def main(args: Array&#x5B;String]) {\n    var matrix = ofDim&#x5B;Int](4,4)\n\n    for (i &lt;- 0 to 3) {\n      for ( j &lt;- 0 to 3) {\n        matrix(i)(j) = j;\n      }\n    }\n\n    for (i &lt;- 0 to 3) {\n      for ( j &lt;- 0 to 3) {\n        print(&quot; &quot; + matrix(i)(j));\n      }\n      println();\n    }\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>&nbsp;0 1 2 3<\/p>\n\n\n\n<p>&nbsp;0 1 2 3<\/p>\n\n\n\n<p>&nbsp;0 1 2 3<\/p>\n\n\n\n<p>&nbsp;0 1 2 3<\/p>\n\n\n\n<p>Concatenate Arrays \u2013 Below example shows how to concatenate two arrays in Scala.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]) {\n    var list1 = Array(&quot;a&quot;, &quot;b&quot;)\n    var list2 = Array(&quot;c&quot;,&quot;d&quot;)\n    var list =  concat( list1, list2)\n    \n    for ( x &lt;- list ) {\n      print( x )\n    }\n  }\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output: abcd<\/p>\n\n\n\n<p>Create Array with Range -&nbsp; In Scala, it is possible to use range() method to generate an array containing a sequence of increasing integers in a given range. We will cover range() in detail.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nimport Array._\nobject Main {\n  def main(args: Array&#x5B;String]) {\n\n    var list:Array&#x5B;Int] = range(1,3,1)\n    for ( x &lt;- list ) {\n      println( x )\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n\n<p>1<\/p>\n\n\n\n<p>2<\/p>\n\n\n\n<p>Common Array Methods&nbsp;<\/p>\n\n\n\n<p>The following are commonly used methods of Arrays:<\/p>\n\n\n\n<p><strong>def concat[T]( xss: Array[T]* ): Array[T]<\/strong> - Concatenates all arrays into a single array.<\/p>\n\n\n\n<p><strong>def empty[T]: Array[T]<\/strong> - Returns an array of length 0.<\/p>\n\n\n\n<p><strong>def ofDim[T]( n1: Int ): Array[T]<\/strong> - Creates array with given dimensions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"collections\"><strong>Collections<\/strong><\/h2>\n\n\n\n<p>Scala provides a rich set of collection libraries. It contains classes as well as traits. These collections could&nbsp; be mutable or immutable. You can use them according to your requirement.&nbsp;<\/p>\n\n\n\n<p><strong>Scala.collection.mutable<\/strong>&nbsp;package has all the mutable collections. It means if you import this package you will be able to&nbsp; add, remove and update data.<\/p>\n\n\n\n<p><strong>Scala.collection.immutable<\/strong>&nbsp;has all the immutable collections. It does not allow you to modify data. Scala will import this package by default. If you want mutable collection, then you need import&nbsp;<strong>scala.collection.mutable<\/strong>&nbsp;package in your code.<\/p>\n\n\n\n<p>Let\u2019s get started with the first collection object.<\/p>\n\n\n\n<p>List \u2013\u00a0 List is nothing but LinkedList in Scala. It\u00a0 is an <a href=\"https:\/\/www.mygreatlearning.com\/blog\/interface-in-java\/\">interface in Java<\/a>. But in Scala it is a class.\u00a0 Lists are immutable in Scala,\u00a0 which means elements of a list cannot be changed by assignment operator.<\/p>\n\n\n\n<p>Let\u2019s see an example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]) {\n    var list:List&#x5B;String] = List(&quot;Scala&quot;,&quot;Java&quot;,&quot;Python&quot;,&quot;C++&quot;)\n    for ( x &lt;- list ) {\n      println( x )\n    }\n  }\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output :&nbsp;<\/p>\n\n\n\n<p>Scala<\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<p>C++<\/p>\n\n\n\n<p>Scala List Methods&nbsp; - The following are commonly used List methods:<\/p>\n\n\n\n<p><strong>def length: Int<\/strong>&nbsp;\u2192 Returns the length of the list.<\/p>\n\n\n\n<p><strong>def reverse[Y &gt;: X]: List[X]<\/strong>&nbsp;\u2192 Reverses the list.<\/p>\n\n\n\n<p><strong>def sorted[Y &gt;: X]: List[X]<\/strong>&nbsp;\u2192 Sorts the list according to an Ordering.<\/p>\n\n\n\n<p><strong>def toString(): String<\/strong>&nbsp;\u2192 Converts the list to a string.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]) {\n    var list:List&#x5B;String] = List(&quot;Scala&quot;,&quot;Java&quot;,&quot;Python&quot;,&quot;C++&quot;)\n\n    println(list.length)\n    println(list.reverse)\n    println(list.sorted)\n    println(list.indexOf(&quot;Java&quot;))\n    println(list.toString())\n    \n  }\n}\n \n\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>4<\/p>\n\n\n\n<p>List(C++, Python, Java, Scala)<\/p>\n\n\n\n<p>List(C++, Java, Python, Scala)<\/p>\n\n\n\n<p>1<\/p>\n\n\n\n<p>List(Scala, Java, Python, C++)<\/p>\n\n\n\n<p>Basic Operations on Lists \u2013 There are basic three different basic operations that can be performed on a list.<\/p>\n\n\n\n<p>Head \u2013 It returns the head node.<\/p>\n\n\n\n<p>tail \u2013 It returns all elements except first<\/p>\n\n\n\n<p>isEmpty \u2013 It&nbsp; returns true if list is empty<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main{\n  def main(args: Array&#x5B;String]) {\n    var list:List&#x5B;String] = List(&quot;Scala&quot;,&quot;Java&quot;,&quot;Python&quot;,&quot;C++&quot;)\n\n    println(list.head)\n    println(list.tail)\n    println(list.isEmpty)\n    \n  }\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Scala<\/p>\n\n\n\n<p>List(Java, Python, C++)<\/p>\n\n\n\n<p>false<\/p>\n\n\n\n<p>Concatenating Lists&nbsp; -&nbsp; <strong>:::&nbsp;<\/strong>operator or&nbsp;<strong>List.:::()<\/strong>&nbsp;method or&nbsp;<strong>List.concat()<\/strong>&nbsp;methods&nbsp; can be used to concatenate more than one Scala lists.&nbsp;<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]) {\n    var list1:List&#x5B;String] = List(&quot;Scala&quot;,&quot;Java&quot;)\n    var list2:List&#x5B;String] = List(&quot;Python&quot;,&quot;C++&quot;)\n\n    println(list1:::list2)\n    println(List.concat(list1,list2))\n    println(list1.:::(list2))\n    \n  }\n}\n<\/pre><\/div>\n\n\n<p>Outpt:<\/p>\n\n\n\n<p>List(Scala, Java, Python, C++)<\/p>\n\n\n\n<p>List(Scala, Java, Python, C++)<\/p>\n\n\n\n<p>List(Python, C++, Scala, Java)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"set\"><strong>Set&nbsp;<\/strong><\/h2>\n\n\n\n<p>In a list duplicates elements are allowed. But Set is a collection that contains no duplicate elements. There are two different type of set&nbsp; <strong>immutable<\/strong>&nbsp;and <strong>mutable.&nbsp;<\/strong><\/p>\n\n\n\n<p>By default, Scala uses the immutable Set. In case, if you need a mutable Set. You can import&nbsp; <strong>scala.collection.mutable.Set<\/strong>&nbsp;class explicitly.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\n \nobject Main {\n  def main(args: Array&#x5B;String]) {\n    var set1:Set&#x5B;String] = Set(&quot;Scala&quot;,&quot;Java&quot;,&quot;C++&quot;,&quot;C++&quot;);\n    for(ele&lt;-set1)\n      println(ele)\n    \n  }\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Scala<\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<p>C++<\/p>\n\n\n\n<p>Scala set methods&nbsp; - The following are commonly used set methods:<\/p>\n\n\n\n<p><strong>def contains(element: A): Boolean<\/strong> - Returns true if element is present in this set, false otherwise.<\/p>\n\n\n\n<p><strong>def min: A<\/strong> \u2013 Returns the smallest element.<\/p>\n\n\n\n<p><strong>def max: A -<\/strong> Returns the largest&nbsp; element.<\/p>\n\n\n\n<p><strong>def +(element: A): Set[A]<\/strong> - Creates a new set with an additional element, unless the element is already present.<\/p>\n\n\n\n<p><strong>def size: Int<\/strong>&nbsp;\u2192 Returns the size of the set.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Mains {\n  def main(args: Array&#x5B;String]) {\n    var set1: Set&#x5B;Int] = Set(1, 2, 3 ,3,4);\n    println(set1.size)\n    println(set1.contains(3));\n    println(set1.min);\n    println(set1.max);\n    \/\/creates new set with new element\n    println(set1.+(20))\n    \n  }\n}\n \n\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>4<\/p>\n\n\n\n<p>true<\/p>\n\n\n\n<p>1<\/p>\n\n\n\n<p>4<\/p>\n\n\n\n<p>Set(20, 1, 2, 3, 4)<\/p>\n\n\n\n<p>Basic Operations on set \u2013 There are basic three different basic operations that can be performed on a list.<\/p>\n\n\n\n<p>Head \u2013 It returns the first element.<\/p>\n\n\n\n<p>tail \u2013 It returns all elements except first.<\/p>\n\n\n\n<p>isEmpty \u2013It&nbsp; returns true if the set is empty.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Demo5 {\n  def main(args: Array&#x5B;String]) {\n    var set1: Set&#x5B;Int] = Set(1, 2, 3 ,3,4);\n    println(set1.head)\n    println(set1.tail);\n    println(set1.isEmpty);\n\n\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>1<\/p>\n\n\n\n<p>Set(2, 3, 4)<\/p>\n\n\n\n<p>False<\/p>\n\n\n\n<p>Concatenating sets&nbsp; -&nbsp; <strong>++&nbsp;<\/strong>operator or&nbsp;<strong>List.++()<\/strong>&nbsp;method can be used to concatenate more than one Scala sets.&nbsp;<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\n \nobject Main {\n  def main(args: Array&#x5B;String]) {\n    var set1: Set&#x5B;Int] = Set(1, 2, 3 ,3,4);\n    var set2: Set&#x5B;Int] = Set(10, 20, 3 );\n    var set3=set1++set2\n    var set4=set1.++(set2)\n    println(set3)\n    println(set4)\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Set(10, 20, 1, 2, 3, 4)<\/p>\n\n\n\n<p>Set(10, 20, 1, 2, 3, 4)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"find-common-values-in-sets\"><strong>Find common values in sets&nbsp;<\/strong><\/h3>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Demo5 {\n  def main(args: Array&#x5B;String]) {\n    var set1: Set&#x5B;Int] = Set(1, 2, 3 ,3,4);\n    var set2: Set&#x5B;Int] = Set(10, 20, 3 ,4);\n    println(set1.intersect(set2));\n\n  }\n}\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Set(3, 4)<\/p>\n\n\n\n<p>As you can see both contain 3 and 4.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"map\"><strong>Map<\/strong><\/h2>\n\n\n\n<p>Map is data structure which contains collection of key\/value pairs. It is possible to fetch the value if we know the key. Keys are unique in map but value may not&nbsp; be unique.<\/p>\n\n\n\n<p>.There are two kinds of Maps&nbsp;<strong>immutable<\/strong>&nbsp;and <strong>mutable<\/strong>.&nbsp;<\/p>\n\n\n\n<p>By default, Scala uses the immutable Map. For mutable&nbsp; map please import <strong>scala.collection.mutable.Map<\/strong>.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\n \nobject Main {\n  def main(args: Array&#x5B;String]) {\n\n    var map1:Map&#x5B;Int,String]= Map(1-&gt;&quot;one&quot;,2-&gt;&quot;two&quot;,3-&gt;&quot;three&quot;)\n    println(map1);\n  \n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Map(1 -&gt; one, 2 -&gt; two, 3 -&gt; three)<\/p>\n\n\n\n<p>In the above example,&nbsp; 1-&gt; \u201cone\u201d is one element where 1 is a key and \u201cone\u201d its value.&nbsp;<\/p>\n\n\n\n<p>Scala map methods&nbsp; - The following are commonly used map methods:<\/p>\n\n\n\n<p><strong>def get(key: A): Option[B]<\/strong> - Optionally returns the value associated with a key.<\/p>\n\n\n\n<p><strong>def contains(key: A): Boolean<\/strong> - Returns true if there is a binding for key in this map, false otherwise<\/p>\n\n\n\n<p>def<strong>&nbsp; - (elem1: A, elem2: A, elems: A*): Map[A, B]<\/strong> - Returns a new map containing all the mappings of this map except mappings with a key equal to elem1, elem2 or any of elems.<\/p>\n\n\n\n<p><strong>def empty: Map[A, B]<\/strong> - Returns the empty map of the same type.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]) {\n\n    var map1:Map&#x5B;Int,String]= Map(1-&gt;&quot;one&quot;,2-&gt;&quot;two&quot;,3-&gt;&quot;three&quot;)\n    println(map1.get(1).getOrElse(&quot;Not found&quot;));\n    println(map1.get(8).getOrElse(&quot;Not found&quot;));\n    println(map1.contains(1));;\n   \/\/ removing one element\n    println(map1.-(1))\n\n    println(map1.isEmpty)\n    \n  }\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>one<\/p>\n\n\n\n<p>Not found<\/p>\n\n\n\n<p>true<\/p>\n\n\n\n<p>Map(2 -&gt; two, 3 -&gt; three)<\/p>\n\n\n\n<p>false<\/p>\n\n\n\n<p>Basic Operations on Map \u2013 There are basic three different basic operations that can be performed on a list.<\/p>\n\n\n\n<p><strong>key<\/strong> \u2013 This method returns an iterable containing each key in the map.<\/p>\n\n\n\n<p><strong>values<\/strong> \u2013It returns an iterable which contains all values of the map.<\/p>\n\n\n\n<p><strong>isEmpty<\/strong> \u2013It&nbsp; returns true if the set is empty.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]) {\n    var map1:Map&#x5B;Int,String]= Map(1-&gt;&quot;one&quot;,2-&gt;&quot;two&quot;,3-&gt;&quot;three&quot;)\n    var keys=map1.keys\n    for(key&amp;lt;-keys)\n      println(key)\n\n    var values=map1.values\n    for(value&amp;lt;-values)\n      println(value)\n\n  }\n}\n \n\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>1<\/p>\n\n\n\n<p>2<\/p>\n\n\n\n<p>3<\/p>\n\n\n\n<p>one<\/p>\n\n\n\n<p>two<\/p>\n\n\n\n<p>three<\/p>\n\n\n\n<p>Concatenating maps&nbsp; -&nbsp; <strong>++&nbsp;<\/strong>operator or&nbsp;<strong>Map.++()<\/strong>&nbsp;method can be used to concatenate more than one Scala Maps.&nbsp;<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]) {\n    var map1:Map&#x5B;Int,String]= Map(1-&gt;&quot;one&quot;,2-&gt;&quot;two&quot;,3-&gt;&quot;three&quot;)\n    var map2:Map&#x5B;Int,String]= Map(4-&gt;&quot;four&quot;,5-&gt;&quot;five&quot;)\n    println(map1++map2)\n    println(map1.++(map2))\n\n  }\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Map(5 -&gt; five, 1 -&gt; one, 2 -&gt; two, 3 -&gt; three, 4 -&gt; four)<\/p>\n\n\n\n<p>Map(5 -&gt; five, 1 -&gt; one, 2 -&gt; two, 3 -&gt; three, 4 -&gt; four)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"scala-tuple\"><strong>Scala tuple&nbsp;<\/strong><\/h2>\n\n\n\n<p>Tuple is a unique data structure where a fixed number of items can be stored&nbsp; together and can be passed around as a whole. It allows us to store objects with different types.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]) {\n    val tuple1=(1,2);\n    val tuple2=Tuple3(1,2,3);\n    println(tuple1._1+tuple1._2)\n    println(tuple2._1+tuple2._2)\n\n  }\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>3<\/p>\n\n\n\n<p>3<\/p>\n\n\n\n<p>In the above example, we created a tuple with three values and stored it into tuple2. It is very easy to pass this to a function as a parameter and values can be accessed using <strong>._1<\/strong> or <strong>._2 <\/strong>so on.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"iterate-over-the-tuple\"><strong>Iterate over the Tuple<\/strong><\/h2>\n\n\n\n<p>There is direct method to<strong> <\/strong>iterate over all elements like List. You need to use <strong>Tuple.productIterator()<\/strong>&nbsp;method to iterate over all the elements of a Tuple.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]) {\n    val tuple2=Tuple3(1,2,3);\n    tuple2.productIterator.foreach{ i =&gt;println( i )}\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>1<\/p>\n\n\n\n<p>2<\/p>\n\n\n\n<p>3<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"converting-to-string\"><strong>Converting to String<\/strong><\/h3>\n\n\n\n<p>It is possible to convert a Tuple into a string.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]) {\n    val tuple2=Tuple3(1,2,3);\n    println(tuple2.toString())\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p><strong>(1,2,3)<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"scala-option\"><strong>Scala Option<\/strong><\/h2>\n\n\n\n<p>Scala Option[ T ] is a container for zero or one element of a given type. An Option[T] can be either&nbsp;<strong>Some[T]<\/strong>&nbsp;or&nbsp;<strong>None<\/strong>&nbsp;object, which represents a missing value.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n \nobject Main {\n  def main(args: Array&#x5B;String]) {\n\n    var map1:Map&#x5B;Int,String]= Map(1-&gt;&quot;one&quot;,2-&gt;&quot;two&quot;,3-&gt;&quot;three&quot;)\n    println(map1.get(1));\n    \n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Some(one)<\/p>\n\n\n\n<p>Map1.get(1) returns Some[T] . If you want to get the&nbsp; value you can use the get<strong>()<\/strong> method or <strong>getOrelse().<\/strong><\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]) {\n\n    var map1:Map&#x5B;Int,String]= Map(1-&gt;&quot;one&quot;,2-&gt;&quot;two&quot;,3-&gt;&quot;three&quot;)\n    println(map1.get(1).get);\n\n  }\n}\n<\/pre><\/div>\n\n\n<p>Output: one<\/p>\n\n\n\n<p>Here, we used the get method to fetch value from Some[T].&nbsp;<\/p>\n\n\n\n<p>Suppose if the key doesn\u2019t exist then it returns <strong>get <\/strong>returns an exception<strong>. <\/strong>To avoid this you can use<strong> getOrelse(). <\/strong>See below example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n \nobject Main {\n  def main(args: Array&#x5B;String]) {\n\n    var map1:Map&#x5B;Int,String]= Map(1-&gt;&quot;one&quot;,2-&gt;&quot;two&quot;,3-&gt;&quot;three&quot;)\n    println(map1.get(5).getOrElse(&quot;Not found&quot;));\n\n  }\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output : Not found<\/p>\n\n\n\n<p>It printed \u201cNot found\u201d because key 5 doesn\u2019t exist in the map1.<\/p>\n\n\n\n<p><strong>isEmpty()<\/strong>&nbsp;\u2013 you can use this method to check whether an Option[T]&nbsp; is None or not.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]) {\n\n    val t1:Option&#x5B;Int] = Some(5)\n    val t2:Option&#x5B;Int] = None\n\n    println(t1.isEmpty)\n    println(t2.isEmpty)\n\n  }\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>false<\/p>\n\n\n\n<p>true<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"iterators\"><strong>Iterators<\/strong><\/h2>\n\n\n\n<p>An iterator allows us to access the elements of a collection one by one. But iterator itself is not a collection.<\/p>\n\n\n\n<p>Let\u2019s see one simple example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]) {\n    val it = Iterator(1, 2, 3, 5)\n    while (it.hasNext){\n      println(it.next())\n\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>Here, hasNext is a method that returns true if the next element is present or false. Whereas next() method moves the pointer to the next element.<\/p>\n\n\n\n<p>Few commonly used methods \u2013<\/p>\n\n\n\n<p><strong>def size: Int<\/strong> - Returns the number of elements in this traversable or iterator.<\/p>\n\n\n\n<p><strong>def min: A<\/strong> - Finds the minimum element. The iterator is at its end after this method returns.<\/p>\n\n\n\n<p><strong>def contains(elem: Any): Boolean<\/strong> - Tests whether this iterator contains a given value as an element.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nobject Demo6 {\n  def main(args: Array&#x5B;String]) {\n    val it1 = Iterator(1, 2, 3, 5)\n    println(it1.size)\n    val it2 = Iterator(1, 2, 3, 5)\n    println(it2.min)\n    val it3 = Iterator(1, 2, 3, 5)\n    println(it3.contains(1))\n\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>4<\/p>\n\n\n\n<p>1<\/p>\n\n\n\n<p>true<\/p>\n\n\n\n<p>In the above example, we created three iterator because it can be traversed only once.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"trait\"><strong>Trait<\/strong><\/h2>\n\n\n\n<p>Unlike a class, Scala traits cannot be instantiated. It cannot have arguments or parameters. However, you can inherit it using classes and objects<\/p>\n\n\n\n<p>A Trait can have both&nbsp;<strong>abstract<\/strong>&nbsp;and&nbsp;<strong>non-abstract<\/strong>&nbsp;methods, fields as its members. If you initialize any method then that method becomes not-abstract and those methods that are not initialized are called abstract.<\/p>\n\n\n\n<p>But the important thing is, In case of&nbsp; abstract methods, the class that implements the trait takes care of the initialization part. If not then the compiler will throw an error.<\/p>\n\n\n\n<p>Now, let's quickly see how it works!<\/p>\n\n\n\n<p><strong>Scala Trait Syntax<\/strong><\/p>\n\n\n\n<p><strong>trait<\/strong> Trait_Name{<\/p>\n\n\n\n<p>\/\/ Variables<\/p>\n\n\n\n<p>\/\/ Methods<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntrait Course{\n\n  def Java()\n  def Scala()\n\n}\n\nclass GreatLearning extends{\n\n  def Java(): Unit ={\n    println(&quot;Welcome to Java Course&quot;)\n\n  }\n\n  def Scala(): Unit ={\n    println(&quot;Welcome to Scala Course&quot;)\n\n  }\n\n}\n\nobject Demo6 {\n  def main(args: Array&#x5B;String]) {\n   val gl:GreatLearning =new GreatLearning();\n    gl.Java()\n\n  }\n}\n\n\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p><br>In the above example, the method&nbsp;Java()&nbsp; and Scala() were&nbsp; an abstract method. Hence, the declaration was made in the class that inherited that trait. But in case if you have a non-abstract method then the class which extends need not to implement the method.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"pattern-matching\"><strong>Pattern Matching<\/strong><\/h2>\n\n\n\n<p>It is similar to java switch. But Pattern matching of Scala is more powerful. It&nbsp; allows us to match a value against a pattern. Pattern matching is the most commonly used feature of Scala. You can use this to identify object\u2019s type.<\/p>\n\n\n\n<p>Let\u2019s take a simple example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nobject Main {\n  def main(args: Array&#x5B;String]) {\n\n    val x: Int=4\n    var res= x match {\n      case 1 =&gt; &quot;one&quot;\n      case 2 =&gt; &quot;two&quot;\n      case 3 =&gt; &quot;three&quot;\n      case _ =&gt; &quot;other&quot;\n    }\n    println(res)\n    \n  }\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output: other<\/p>\n\n\n\n<p>Here, X is 4 and it will be matched against all cases and the result will be stored in res variable .<\/p>\n\n\n\n<p>Another example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nobject Demo6 {\n  def main(args: Array&#x5B;String]) {\n\n    def matchNow(x: Int): String = x match {\n      case 1 =&gt; &quot;one&quot;\n      case 2 =&gt; &quot;two&quot;\n      case _ =&gt; &quot;other&quot;\n    }\n    println( matchNow(3))\n    println( matchNow(1) )\n   \n    \n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Here, <strong>matchNow() <\/strong>is a function which will return matched value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"matching-on-case-classes\"><strong>Matching on case classes<\/strong><\/h2>\n\n\n\n<p>Let\u2019s understand this by taking an example of Employee class.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nabstract class Employee\n\ncase class SalariedEmployee(name:String,annualSalary:Int) extends  Employee\n\n\ncase class HourlyEmployee(name:String,perHour:Int) extends Employee\n\nobject Main {\n  def processPayment(employee: Employee): String = {\n    employee match {\n      case SalariedEmployee(name, annualSalary) =&gt;\n        s&quot;Name:$name \\nannual salary : $annualSalary  &quot;\n      case HourlyEmployee(name, perHour) =&gt;\n        s&quot;Name:$name \\nPer hour :$perHour  &quot;\n    }\n  }\n  def main(args: Array&#x5B;String]) {\n\n   var emp1=SalariedEmployee(&quot;Rahul&quot;,800000)\n    var emp2=HourlyEmployee(&quot;Shayam&quot;,89)\n\n    println(processPayment(emp1))\n    println(processPayment(emp2))\n\n  }\n}\n<\/pre><\/div>\n\n\n<p>Output :<\/p>\n\n\n\n<p>Name:Rahul&nbsp;<\/p>\n\n\n\n<p>annual salary : 800000&nbsp;&nbsp;<\/p>\n\n\n\n<p>Name:Shayam&nbsp;<\/p>\n\n\n\n<p>Per hour :89&nbsp;&nbsp;<\/p>\n\n\n\n<p>The function&nbsp;<strong>processPayment()<\/strong> takes an abstract class&nbsp;Employee&nbsp; as a parameter. And this is matched against the type of&nbsp;Employee&nbsp;(i.e <em>HourlyEmployee<\/em>,&nbsp;<em>SalariedEmployee<\/em>).&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"exception-handling\"><strong>Exception Handling&nbsp;<\/strong><\/h2>\n\n\n\n<p>An exception is an event that disturbs&nbsp; the normal flow of a program. Exception handling is a mechanism which allows us to handle abnormal conditions.&nbsp;<\/p>\n\n\n\n<p>Scala makes \"checked vs unchecked\" very simple. It doesn't have checked exceptions.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"scala-try-catch\"><strong>Scala Try Catch<\/strong>&nbsp;<\/h2>\n\n\n\n<p>Scala has a try and catch block to handle exceptions. If you think that some part of your code might throw an exception then you keep that part in the try block. The catch block exception occurred in the try block. It is possible to add any number of try catch blocks in your program according to need.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nobject Main{\n\n  def divide(first:Int, second:Int) = {\n    try{\n      first\/second\n    }catch{\n      case e: ArithmeticException =&gt; println(e)\n    }\n    println(&quot;executing code after exception&quot;)\n  }\n  def main(args:Array&#x5B;String]){\n\n    divide(100,0)\n\n  }\n}\n\n\n\n<\/pre><\/div>\n\n\n<p>Output :&nbsp;<\/p>\n\n\n\n<p>java.lang.ArithmeticException: \/ by zero<\/p>\n\n\n\n<p>executing code after exception<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"finally-block\"><strong>Finally Block<\/strong><\/h2>\n\n\n\n<p>If your program acquires some resources then you can use&nbsp; finally block to release resources during an exception. Here, resources means a file, database connection etc. The finally block is always&nbsp; executed.&nbsp;<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nobject Main11{\n\n  def divide(first:Int, second:Int) = {\n    try{\n      first\/second\n    }catch{\n      case e: ArithmeticException =&gt; println(e)\n    }\n    finally {\n      println(&quot;Finally block&quot;)\n    }\n    println(&quot;executing code after exception&quot;)\n  }\n  def main(args:Array&#x5B;String]){\n\n    divide(100,0)\n\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:&nbsp;<\/p>\n\n\n\n<p>java.lang.ArithmeticException: \/ by zero<\/p>\n\n\n\n<p>Finally block<\/p>\n\n\n\n<p>executing code after exception<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"throwing-exceptions\"><strong>Throwing Exceptions<\/strong><\/h2>\n\n\n\n<p>You can throw exception explicitly in your code. Scala provides a <strong>throw<\/strong> keyword to throw an exception. This throw keyword is mainly used to throw custom exception.&nbsp;<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nobject Main{\n\n  def subscribe(coins:Int)={\n    if(coins&amp;lt;200)\n      throw new ArithmeticException(&quot;you have less coins&quot;)\n    else println(&quot;Thanks for subscribing!&quot;)\n  }\n  def main(args:Array&#x5B;String]){\n\n    subscribe(100)\n\n  }\n}\n \n\n<\/pre><\/div>\n\n\n<p>Output:&nbsp;<\/p>\n\n\n\n<p>Exception in thread \"main\" java.lang.ArithmeticException: you have less coin<\/p>\n\n\n\n<p>at com.learnscala.hr.Main11$.subscribe(Main.scala:184)<\/p>\n\n\n\n<p>at com.learnscala.hr.Main11$.main(Main.scala:189)<\/p>\n\n\n\n<p>at com.learnscala.hr.Main11.main(Main.scala)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"file-handling\"><strong>File handling<\/strong><\/h2>\n\n\n\n<p>Scala provides predefined methods to deal with file. You can create, open, write and read file. Scala offers a package called scala.io which contains all functions to deal with all IO operations.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nobject Main{\n\n\n  def main(args:Array&#x5B;String]){\n\n    var fileSource:BufferedSource=null\n    try {\n       fileSource = Source.fromFile(&quot;\/user\/loc\/temp.txt&quot;)\n    }catch {\n      case e:FileNotFoundException=&gt; println(s&quot; Please check file path \\n $e&quot;)\n    }\n\n    for(line&amp;lt;-fileSource.getLines){\n      println(line)\n    }\n    fileSource.close()\n  }\n\n\n}\n \n\n<\/pre><\/div>\n\n\n<p>output:<\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<p>scala<\/p>\n\n\n\n<p>c++<\/p>\n\n\n\n<p>python<\/p>\n\n\n\n<p>In the above example, we are reading a file called temp.txt if the file does not exist in the specified location then it throws <strong>FileNotFoundException<\/strong> exception.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"extractor\"><strong>Extractor<\/strong><\/h2>\n\n\n\n<p>An extractor object is&nbsp; nothing but an object with an&nbsp;<strong>unapply<\/strong>&nbsp;method. Whereas the&nbsp;<strong>apply<\/strong>&nbsp;method is like a constructor which takes arguments and creates an object. But the&nbsp;<strong>unapply<\/strong>&nbsp;takes an object and tries to give back the arguments.&nbsp;<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nobject Name {\n\n  def apply(firstName: String, lastName: String): String = {\n    firstName +&quot; &quot;+lastName\n  }\n\n  def unapply(str: String): Option&#x5B;(String, String)] = {\n    var temp = str.split(&quot; &quot;);\n    if (temp.length == 2)\n      Some(temp(0), temp(1)) else None\n  }\n\n\n  def main(args: Array&#x5B;String]) {\n    var name= Name(&quot;Ram&quot;,&quot;kumar&quot;);\n    println( Name.unapply(name))\n    println( Name.unapply(name).get)\n    println( Name.unapply(name).get._1)\n\n\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>Here, The&nbsp;<strong>apply<\/strong>&nbsp;method createsa&nbsp;Name&nbsp;string from a first name &amp; Last name. The&nbsp;<strong>unapply<\/strong>&nbsp;does the inverse.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"regular-expression\"><strong>Regular expression<\/strong><\/h2>\n\n\n\n<p>Regular expressions are strings which can be used to find patterns in data. In scala, any string can be converted to a regular expression using the&nbsp;.r&nbsp;method.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nobject Main {\n\n  def main(args: Array&#x5B;String]) {\n    val numberPattern: Regex = &quot;&#x5B;0-9]&quot;.r\n\n    numberPattern.findFirstMatchIn(&quot;mypassword&quot;) match {\n      case Some(_) =&gt; println(&quot;Password set&quot;)\n      case None =&gt; println(&quot;Password must contain a number&quot;)\n    }\n\n  }\n}\n \n\n\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n\n<p>If you found this Scala Tutorial helpful and wish to learn more such concepts, you can check out our various free <a href=\"https:\/\/www.mygreatlearning.com\/scala\/free-courses\" target=\"_blank\" rel=\"noreferrer noopener\">Scala Courses<\/a> available on <a href=\"https:\/\/www.mygreatlearning.com\/academy\" target=\"_blank\" rel=\"noreferrer noopener\">Great Learning Academy. <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Scala? Scala stands for Scalable Language. It is a multi-paradigm programming language. Scala language includes features of functional programming and object-oriented programming. It is a statically typed language. Its source code is compiled into bytecode and executed by Java virtual machine(JVM). Scala is object-oriented Scala is an object-oriented language. In Scala, every value [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":26049,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[],"content_type":[],"class_list":["post-25966","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Scala Tutorial - What is Scala used for &amp; Examples of it<\/title>\n<meta name=\"description\" content=\"Scala Tutorial: Scala language is mostly used by Software engineers and Data Engineers. In this tutorial you will know what is scala used for and difference between scala &amp; java.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala Tutorial - Learn Scala Step-by-Step Guide\" \/>\n<meta property=\"og:description\" content=\"Scala Tutorial: Scala language is mostly used by Software engineers and Data Engineers. In this tutorial you will know what is scala used for and difference between scala &amp; java.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-03-10T04:07:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-24T13:50:09+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1254\" \/>\n\t<meta property=\"og:image:height\" content=\"837\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/scala-tutorial\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/scala-tutorial\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Scala Tutorial - Learn Scala Step-by-Step Guide\",\"datePublished\":\"2021-03-10T04:07:00+00:00\",\"dateModified\":\"2024-10-24T13:50:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/scala-tutorial\\\/\"},\"wordCount\":5802,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/scala-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/iStock-1075599562.jpg\",\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/scala-tutorial\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/scala-tutorial\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/scala-tutorial\\\/\",\"name\":\"Scala Tutorial - What is Scala used for & Examples of it\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/scala-tutorial\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/scala-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/iStock-1075599562.jpg\",\"datePublished\":\"2021-03-10T04:07:00+00:00\",\"dateModified\":\"2024-10-24T13:50:09+00:00\",\"description\":\"Scala Tutorial: Scala language is mostly used by Software engineers and Data Engineers. In this tutorial you will know what is scala used for and difference between scala & java.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/scala-tutorial\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/scala-tutorial\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/scala-tutorial\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/iStock-1075599562.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/iStock-1075599562.jpg\",\"width\":1254,\"height\":837,\"caption\":\"scala tutorial\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/scala-tutorial\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Scala Tutorial &#8211; Learn Scala Step-by-Step Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Scala Tutorial - What is Scala used for & Examples of it","description":"Scala Tutorial: Scala language is mostly used by Software engineers and Data Engineers. In this tutorial you will know what is scala used for and difference between scala & java.","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:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"Scala Tutorial - Learn Scala Step-by-Step Guide","og_description":"Scala Tutorial: Scala language is mostly used by Software engineers and Data Engineers. In this tutorial you will know what is scala used for and difference between scala & java.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2021-03-10T04:07:00+00:00","article_modified_time":"2024-10-24T13:50:09+00:00","og_image":[{"width":1254,"height":837,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Scala Tutorial - Learn Scala Step-by-Step Guide","datePublished":"2021-03-10T04:07:00+00:00","dateModified":"2024-10-24T13:50:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/"},"wordCount":5802,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562.jpg","articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/","url":"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/","name":"Scala Tutorial - What is Scala used for & Examples of it","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562.jpg","datePublished":"2021-03-10T04:07:00+00:00","dateModified":"2024-10-24T13:50:09+00:00","description":"Scala Tutorial: Scala language is mostly used by Software engineers and Data Engineers. In this tutorial you will know what is scala used for and difference between scala & java.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562.jpg","width":1254,"height":837,"caption":"scala tutorial"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/scala-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Scala Tutorial &#8211; Learn Scala Step-by-Step Guide"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562.jpg",1254,837,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562-768x513.jpg",768,513,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562-1024x683.jpg",1024,683,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562.jpg",1254,837,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562.jpg",1254,837,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562.jpg",640,427,false],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562.jpg",96,64,false],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/02\/iStock-1075599562.jpg",150,100,false]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"What is Scala? Scala stands for Scalable Language. It is a multi-paradigm programming language. Scala language includes features of functional programming and object-oriented programming. It is a statically typed language. Its source code is compiled into bytecode and executed by Java virtual machine(JVM). Scala is object-oriented Scala is an object-oriented language. In Scala, every value&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/25966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=25966"}],"version-history":[{"count":102,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/25966\/revisions"}],"predecessor-version":[{"id":109481,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/25966\/revisions\/109481"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/26049"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=25966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=25966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=25966"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=25966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}