

{"id":11538,"date":"2018-03-23T06:24:28","date_gmt":"2018-03-23T06:24:28","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=11538"},"modified":"2021-12-04T10:16:48","modified_gmt":"2021-12-04T04:46:48","slug":"why-scala","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/why-scala\/","title":{"rendered":"Why Scala? | 16+ Reasons to Why Learn Scala"},"content":{"rendered":"<h3>Why Scala?<\/h3>\n<p>Okay, we see all the hype about <strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-tutorial\/\">Scala<\/a><\/strong>, you also must have heard about Scala, you want to learn it but you are waiting for a solid reason to learn Scala. Why Scala? This article answers you question &#8220;Why should I learn Scala Programming Language?&#8221; Let see why Scala is a beneficiary language to learn and what it offers that you. You can share your own reasons to why Scala with us.<\/p>\n<h4>1. Type Inference<\/h4>\n<p>Scala automatically infers(detects) the data type of an expression partially or fully. This means that we don\u2019t need to declare it ourselves. Such a facility lets the programmer leave out type annotations while still allowing type checking.<\/p>\n<p>So, how does this work? The compiler takes in the types of subexpressions, or those of atomic values like 42(Integer), true(Bool), and so on. It then aggregates this information, and then decides the type for an expression.<br \/>\n<strong>Read: <a href=\"https:\/\/data-flair.training\/blogs\/scala-features-comprehensive-guide\/\">Features of Scala<\/a><\/strong><\/p>\n<h4>2. Singleton Object<\/h4>\n<p>Scala has no static variables or methods. Instead, it makes use of a singleton object. This is essentially a class with only a single object in the source file. We do this using the \u2018object\u2019 keyword, and not the \u2018class\u2019 keyword. For instance:<\/p>\n<pre class=\"EnlighterJSRAW\">object Main extends App {\r\n  println(\"Hello, World!\")\r\n}<\/pre>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-singleton-object\/\">Follow this link to know about Scala Singleton &amp; Scala Companion Object<\/a><\/strong><\/p>\n<h4>3. Immutability<\/h4>\n<p>In Scala, every <strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-variable\/\">variable<\/a><\/strong> you declare is immutable by default. You cannot change it. However, you can also explicitly declare it to be mutable. You can change the value of such a variable.<\/p>\n<p>Immutable data helps us manage concurrency control, as you would imagine. Once we create an immutable object, we cannot modify its state.<\/p>\n<h4>4. Lazy Evaluation<\/h4>\n<p>We discussed this when we talked Scala vs Java. Lazy evaluation is when Scala delays evaluating an expression until it absolutely needs it. This is also called call-by-need.<br \/>\nThis also avoids repeated evaluations. Lazy evaluation has the following benefits:<\/p>\n<ol>\n<li>We can define control flow as abstractions instead of primitives.<\/li>\n<li>We can define potentially infinite data structures.<\/li>\n<li>Lazy evaluation improves performance.<\/li>\n<\/ol>\n<p>In Scala, we can have a lazy declaration:<br \/>\nlazy val images=getImages()<br \/>\n<strong>Read: <a href=\"https:\/\/data-flair.training\/blogs\/scala-control-structures-comprehensive-guide\/\">Control Structures in Scala<\/a><\/strong><\/p>\n<h4>5. Case Classes and Pattern Matching<\/h4>\n<p>A<strong> <a href=\"https:\/\/data-flair.training\/blogs\/scala-case-class\/\">Scala case class<\/a> <\/strong>is a regular one that is immutable by default, and is decomposable via pattern matching. All its parameters are immutable and public by default.<\/p>\n<p>Defining a case class takes the keywords \u2018case class\u2019, an identifier, and a parameter list(can be empty).<\/p>\n<p>case class Book(isbn: String)<\/p>\n<p>val frankenstein = Book(&#8220;978-0486282114&#8221;)<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-pattern-matching\/\">Pattern matching<\/a> <\/strong>lets us check a value against a pattern. You can use this in place of a switch-statement or a series of if-else statements in Java.<\/p>\n<pre class=\"EnlighterJSRAW\">import scala.util.Random\r\nval x: Int = Random.nextInt(10)\r\nx match\r\n{\r\ncase 0 =&gt; \"zero\"\r\ncase 1 =&gt; \"one\"\r\ncase 2 =&gt; \"two\"\r\ncase _ =&gt; \"many\"\r\n}<\/pre>\n<h4>6. Concurrency Control<\/h4>\n<p>Scala\u2019s standard library includes the Actor model. Using this, you can implement concurrency in your code. Apart from this, it also has a platform\/tool, Akka. It is a separate, open-source framework providing for Actor-based concurrency. You can combine or distribute Akka\u2019s actors with software transactional memory.<\/p>\n<h4>7. String Interpolation<\/h4>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-string-interpolation\/\">Scala String interpolation<\/a><\/strong> is the act of evaluating a string literal, consisting of one or more placeholders, to yield a result. In this result, corresponding values replace the placeholders. This is a kind of template-processing.<br \/>\nSince version 2.10.0, Scala offers string interpolation. The three methods it offers for this are- s, f, and raw.<\/p>\n<p><strong>Read: <a href=\"https:\/\/data-flair.training\/blogs\/tuples-in-scala-introduction\/\">Tuples in Scala<\/a><\/strong><\/p>\n<h4>8. Higher-Order Functions<\/h4>\n<p>A higher-order function is one that takes another as a parameter, or returns it. This is possible in Scala only because it treats functions as first-class citizens.<\/p>\n<pre class=\"EnlighterJSRAW\">val salaries = Seq(20000, 70000, 40000)\r\nval doubleSalary = (x: Int) =&gt; x * 2\r\nval newSalaries = salaries.map(doubleSalary) \/\/ List(40000, 140000, 80000)<\/pre>\n<p>Here, map is a higher-order function.<br \/>\nSuch functions also let us implement function compositions, and lambdas.<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-functions\/\">Read more about Scala Functions in detail<\/a><\/strong><\/p>\n<h4>9. Traits<\/h4>\n<p>A<strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-trait\/\"> trait in Scala<\/a><\/strong> is a type containing certain fields and methods. You can combine multiple traits.<br \/>\nTo define a trait, we use the \u2018trait\u2019 keyword:<br \/>\ntrait Greeter<\/p>\n<pre class=\"EnlighterJSRAW\">{\r\ndef greet(name: String): Unit\r\n}<\/pre>\n<p>So, a trait is like a partially implemented interface. You can create a trait with abstract, and optionally, non-abstract methods.<br \/>\nNot yet convinced Why Scala is good for you? We have more reasons.<\/p>\n<h4>10. Rich Set of Collections<\/h4>\n<p>Scala has a huge set of collections in its library. This has classes and traits to help you collect data into a mutable or immutable collection. The scala.collection.mutable package holds all mutable collections. Making use of this package, you can add, remove, and update data. Likewise, the scala.collection.immutable package holds all immutable collections; they don\u2019t let us modify data.<br \/>\n<strong>Read: <a href=\"https:\/\/data-flair.training\/blogs\/best-scala-books-list\/\">Best Books for Scala<\/a><\/strong><\/p>\n<h4>11. Functional<\/h4>\n<p>Scala is a language of a functional paradigm. It treats its functions as first-class citizens. This is why it also lets us create higher-order functions- ones that can return a function, or take it as a parameter.<\/p>\n<p>It also supports nesting of functions, and currying(translating evaluation of a function, that takes multiple arguments, into evaluating a sequence of functions, each with a single argument).<\/p>\n<h4>12. Object-Oriented<\/h4>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-object-oriented-programming\/\">Scala supports object-oriented programming<\/a><\/strong>, purely. Every value is an object. We\u2019ll have more on this later.<\/p>\n<h4>13. Statically-typed<\/h4>\n<p>In most cases, you won\u2019t need to specify redundant type information to Scala; it will figure it out on its own. This is in relevance to type inference.<br \/>\n<strong>Read:<a href=\"https:\/\/data-flair.training\/blogs\/scala-vs-java-performance\/\"> Scala vs Java<\/a><\/strong><\/p>\n<h4>14. Extensibility<\/h4>\n<p>Developing domain-specific applications needs domain-specific language extensions. In a way, Scala puts forward a combination of language mechanisms, making it easy to smoothly add new language constructs as libraries. We can do this using facilities like implicit classes and string interpolation. You don\u2019t necessarily need meta-programming features like macros.<\/p>\n<h4>15. Scala Runs on the JVM<\/h4>\n<p>The Scala compiler turns the source code into byte code. This runs on the <strong><a href=\"https:\/\/data-flair.training\/blogs\/java-virtual-machine-jvm\/\">JVM (Java Virtual Machine)<\/a><\/strong>. This makes it a language similar to Java and Groovy.<\/p>\n<h4>16. Miscellaneous Features<\/h4>\n<p>Some of Scala\u2019s quirks confuse Java programmers:<\/p>\n<ol>\n<li>Where Java mandates that all import statements must be at the top of the source, in Scala, they can be almost anywhere within your whole program.<\/li>\n<li>In Scala, operators are just methods.<\/li>\n<li>Definitely everything looks like an object in Scala.<\/li>\n<li>Here, methods may have multiple parameter lists. Wow! Let\u2019s discuss this in a later lesson, this seems really exciting for a programmer stuck with similar languages.<\/li>\n<li>It is possible to nest methods.<\/li>\n<li>A class\u2019 body is its constructor.<\/li>\n<li>In Scala, we don\u2019t have static methods; we have objects.<\/li>\n<\/ol>\n<p>This was all about Why Scala article. Hope you like the Why Scala article and have now got the answers to &#8220;why should I learn Scala&#8221;.<\/p>\n<h3>Conclusion<\/h3>\n<p>Scala Has many features that makes it special as we saw in the above why Scala post.\u00a0 Wasn\u2019t this a step ahead in your journey with Scala? But don\u2019t worry if it confuses you with its syntax; we will get to explaining the basics pretty soon. Till then, share love, share happiness.<br \/>\n<strong>Next:<\/strong><br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-vs-python\/\">Python vs Scala<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why Scala? Okay, we see all the hype about Scala, you also must have heard about Scala, you want to learn it but you are waiting for a solid reason to learn Scala. Why&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":31322,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[16515,16164,16172],"class_list":["post-11538","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scala","tag-reasons-to-learn-scala","tag-why-learn-scala","tag-why-scala"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Why Scala? | 16+ Reasons to Why Learn Scala - DataFlair<\/title>\n<meta name=\"description\" content=\"Why Scala Tutorial answers to why should I learn Scala and learn the features like Concurrency Control and String Interpolation\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/why-scala\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Why Scala? | 16+ Reasons to Why Learn Scala - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Why Scala Tutorial answers to why should I learn Scala and learn the features like Concurrency Control and String Interpolation\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/why-scala\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-23T06:24:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-04T04:46:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Why-Scala-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Why Scala? | 16+ Reasons to Why Learn Scala - DataFlair","description":"Why Scala Tutorial answers to why should I learn Scala and learn the features like Concurrency Control and String Interpolation","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:\/\/data-flair.training\/blogs\/why-scala\/","og_locale":"en_US","og_type":"article","og_title":"Why Scala? | 16+ Reasons to Why Learn Scala - DataFlair","og_description":"Why Scala Tutorial answers to why should I learn Scala and learn the features like Concurrency Control and String Interpolation","og_url":"https:\/\/data-flair.training\/blogs\/why-scala\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-03-23T06:24:28+00:00","article_modified_time":"2021-12-04T04:46:48+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Why-Scala-1.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/why-scala\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/why-scala\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Why Scala? | 16+ Reasons to Why Learn Scala","datePublished":"2018-03-23T06:24:28+00:00","dateModified":"2021-12-04T04:46:48+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/why-scala\/"},"wordCount":1127,"commentCount":1,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/why-scala\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Why-Scala-1.jpg","keywords":["Reasons to learn Scala","Why learn Scala","why scala"],"articleSection":["Scala Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/why-scala\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/why-scala\/","url":"https:\/\/data-flair.training\/blogs\/why-scala\/","name":"Why Scala? | 16+ Reasons to Why Learn Scala - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/why-scala\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/why-scala\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Why-Scala-1.jpg","datePublished":"2018-03-23T06:24:28+00:00","dateModified":"2021-12-04T04:46:48+00:00","description":"Why Scala Tutorial answers to why should I learn Scala and learn the features like Concurrency Control and String Interpolation","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/why-scala\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/why-scala\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/why-scala\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Why-Scala-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Why-Scala-1.jpg","width":1200,"height":628,"caption":"Why Scala? | 16+ Reasons to Why Learn Scala"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/why-scala\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Scala Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/scala\/"},{"@type":"ListItem","position":3,"name":"Why Scala? | 16+ Reasons to Why Learn Scala"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/11538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=11538"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/11538\/revisions"}],"predecessor-version":[{"id":104834,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/11538\/revisions\/104834"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/31322"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=11538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=11538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=11538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}