{"id":8626,"date":"2021-05-24T14:32:05","date_gmt":"2021-05-24T14:32:05","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=8626"},"modified":"2024-04-13T10:33:56","modified_gmt":"2024-04-13T10:33:56","slug":"scala-lazy-values-guide-to-lazy-val-in-scala","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/","title":{"rendered":"Scala &#8211; Lazy Values"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Guide to lazy val in Scala&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:6145,&quot;3&quot;:{&quot;1&quot;:0,&quot;3&quot;:1},&quot;14&quot;:{&quot;1&quot;:3,&quot;3&quot;:1},&quot;15&quot;:&quot;Arial&quot;}\">Guide to lazy val in Scala<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p>In <strong><a href=\"https:\/\/prwatech.in\/blog\/scala\/scala-a-quick-overview\/\">Scala <\/a><\/strong>Vals and Lazy vals are present . lazy <a href=\"http:\/\/scala-lang.org\">keyword<\/a> changes the val to get lazily initialized. Lazy initialization means that whenever an object creation seems costly, the lazy keyword can be stick before val. This gives it the advantage to get initialized in the first use i.e. the expression inbound is not evaluated immediately but once on the first access.<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>In Scala, <code>lazy val<\/code> is a language feature that delays the initialization of a value until it is accessed for the first time. This allows for deferred computation of values, improving performance and memory usage in scenarios where the value might not always be needed.<\/p>\r\n<p>When a <code>lazy val<\/code> is defined, its initialization expression is not evaluated immediately during object creation like a regular <code>val<\/code>. Instead, the initialization expression is evaluated only when the <code>lazy val<\/code> is first accessed, and the computed value is stored for subsequent accesses.<\/p>\r\n<p>The use of <code>lazy val<\/code> is particularly beneficial for initializing values that are expensive to compute or involve complex initialization logic. By deferring initialization until the value is actually needed, unnecessary computations are avoided, leading to more efficient resource utilization.<\/p>\r\n<p>It&#8217;s important to note that <code>lazy val<\/code> is thread-safe by default, ensuring that the initialization expression is evaluated exactly once, even in concurrent environments.<\/p>\r\n\r\n\r\n\r\n<h4><strong>use case 1:<\/strong><\/h4>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>object prwatech {\r\n\/\/ Main method \r\n    def main(args:Array[String]) \r\n    { \r\n        lazy val prwatech = {\r\n               \r\n            println (\"Initialization for the first time\")\r\n            2\r\n        }\r\n        \/\/ Part 1\r\n        println(prwatech)\r\n           \r\n        \/\/ Part 2\r\n        print(prwatech)\r\n    }   \r\n}\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Output \r\nInitialization for the first time\r\n2\r\n2\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>In the code above \u2018prwatech\u2019 was a lazy val and so for the first when it is accessed, it returned<\/p>\r\n\r\n\r\n\r\n<p>Initialization for the first time<\/p>\r\n\r\n\r\n\r\n<p>2<\/p>\r\n\r\n\r\n\r\n<p>But for the second time when it is printed, it only returned<\/p>\r\n\r\n\r\n\r\n<p>2<\/p>\r\n\r\n\r\n\r\n<p>Because it is a cached result.<\/p>\r\n\r\n\r\n\r\n<p><strong>use case 2:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>object prwatech {\r\n \/\/ Main method \r\n    def main(args:Array[String]) \r\n    { \r\n        var a = 3\r\n           \r\n        def fun() = { \r\n            a += 1; \r\n            a \r\n        }\r\n           \r\n        lazy val prwatech = Stream.continually( fun() )\r\n           \r\n        (prwatech take 5) foreach {\r\n            x =&gt; println(x)\r\n        }\r\n    } \r\n}\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>output:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>4\r\n5\r\n6\r\n7\r\n8<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>use case 3:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>scala&gt;var x = { println(\"x\"); 15 }\r\nscala&gt;lazy val y = { println(\"y\"); x + 1 }\r\nscala&gt;println(\"-----\")\r\nscala&gt;x = 17\r\nscala&gt;println(\"y is: \" + y)\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"380\" height=\"264\" class=\"wp-image-8628\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-49.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-49.png 380w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-49-300x208.png 300w\" sizes=\"auto, (max-width: 380px) 100vw, 380px\" \/><\/figure>\r\n<p>&nbsp;<\/p>\r\n<p>Guide to lazy val in Scala<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Guide to lazy val in Scala &nbsp; In Scala Vals and Lazy vals are present . lazy keyword changes the val to get lazily initialized. Lazy initialization means that whenever an object creation seems costly, the lazy keyword can be stick before val. This gives it the advantage to get initialized in the first use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[565,1698],"tags":[1359,1358,1363,1362,1355,1360,1356,1361,1354,1357],"class_list":["post-8626","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-define-lazy-values","tag-explain-lazy-values","tag-how-lazy-value-is-used-in-scala","tag-lazy-value-scala","tag-lazy-values","tag-lazy-values-examples","tag-lazy-values-in-scala","tag-lazy-values-use-cases","tag-scala-lazy-values","tag-what-is-scala-lazy-val"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Guide to lazy val in Scala - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Guide to lazy val in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Guide to lazy val in Scala - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Guide to lazy val in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-24T14:32:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T10:33:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-49.png\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/\",\"name\":\"Guide to lazy val in Scala - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-49.png\",\"datePublished\":\"2021-05-24T14:32:05+00:00\",\"dateModified\":\"2024-04-13T10:33:56+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Guide to lazy val in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-49.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-49.png\",\"width\":380,\"height\":264},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Lazy Values\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Guide to lazy val in Scala - Prwatech","description":"Master Guide to lazy val in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Guide to lazy val in Scala - Prwatech","og_description":"Master Guide to lazy val in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-05-24T14:32:05+00:00","article_modified_time":"2024-04-13T10:33:56+00:00","og_image":[{"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-49.png","type":"","width":"","height":""}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/","name":"Guide to lazy val in Scala - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-49.png","datePublished":"2021-05-24T14:32:05+00:00","dateModified":"2024-04-13T10:33:56+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Guide to lazy val in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-49.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-49.png","width":380,"height":264},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lazy-values-guide-to-lazy-val-in-scala\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Lazy Values"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=8626"}],"version-history":[{"count":5,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8626\/revisions"}],"predecessor-version":[{"id":11453,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8626\/revisions\/11453"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=8626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=8626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=8626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}