{"id":96,"date":"2020-07-29T10:38:31","date_gmt":"2020-07-29T10:38:31","guid":{"rendered":"http:\/\/localhost\/anviam-blog\/?p=96"},"modified":"2024-09-26T04:30:39","modified_gmt":"2024-09-26T04:30:39","slug":"from-java-to-kotlin","status":"publish","type":"post","link":"https:\/\/anviam.com\/blog\/from-java-to-kotlin\/","title":{"rendered":"FROM JAVA TO KOTLIN"},"content":{"rendered":"\r\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Ever since when I first started to learn Java, never did I realize that something so similar could be made. I started with Core, and Advanced and began my journey.<\/p><p>Java is one of the oldest and easiest languages. It has been at the top for more than two decades and it is still a very popular and widely used programming language. Being fluent in Java is a very good advantage to be able to learn new and similar languages.<\/p><\/blockquote>\r\n\r\n\r\n\r\n<p class=\"has-text-align-left\"><\/p>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">Kotlin Overview:<\/span><\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"has-text-align-left\"><span style=\"color:#000000\" class=\"has-inline-color\">Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. Kotlin is designed to interoperate fully with Java, and the JVM version of its standard library depends on the Java Class Library, but type inference allows its syntax to be more concise. Kotlin is a very concise language with handy mutability and immutability features.<\/span><\/p>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">How to create project in Kotlin<\/span><span class=\"has-inline-color has-very-dark-gray-color\">:<\/span><\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"has-text-align-left\"><span style=\"color:#000000\" class=\"has-inline-color\">1.Go to new project from file.<\/span><\/p>\r\n\r\n\r\n\r\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/new_project.png\" alt=\"\" class=\"wp-image-114\" width=\"531\" height=\"72\"\/><\/figure><\/div>\r\n\r\n\r\n\r\n<p><span style=\"color:#000000\" class=\"has-inline-color\">2.Choose new activity.<\/span><\/p>\r\n\r\n\r\n\r\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/activity_new.png\" alt=\"\" class=\"wp-image-115\" width=\"586\" height=\"411\"\/><\/figure><\/div>\r\n\r\n\r\n\r\n<p><span style=\"color:#000000\" class=\"has-inline-color\">3.Choose kotlin language.<\/span><\/p>\r\n\r\n\r\n\r\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/project.png\" alt=\"\" class=\"wp-image-117\" width=\"589\" height=\"425\"\/><\/figure><\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/activity_new.png\" alt=\"\" class=\"wp-image-118\"\/><\/figure><\/div>\r\n\r\n\r\n\r\n<p class=\"has-text-align-left\"><strong><span style=\"color:#000000\" class=\"has-inline-color\">&nbsp;How to add new Activity in Kotlin:<\/span><\/strong><\/p>\r\n\r\n\r\n\r\n<ol class=\"wp-block-list\"><li><span style=\"color:#000000\" class=\"has-inline-color\"><strong>Go to the app folder<\/strong>.<\/span><\/li><\/ol>\r\n\r\n\r\n\r\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/app_folder.png\" alt=\"\" class=\"wp-image-98\"\/><\/figure><\/div>\r\n\r\n\r\n\r\n<p><span style=\"color:#000000\" class=\"has-inline-color\">2. Create Activity.<\/span><\/p>\r\n\r\n\r\n\r\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/new_project.png\" alt=\"\" class=\"wp-image-99\" width=\"528\" height=\"276\"\/><\/figure><\/div>\r\n\r\n\r\n\r\n<p><span style=\"color:#000000\" class=\"has-inline-color\"><strong>The&nbsp;<code>val<\/code>&nbsp;and&nbsp;<code>var<\/code>&nbsp;keywords can be used only when the type can be inferred. Otherwise you need to declare the type<\/strong>.<\/span><\/p>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">var<\/span><\/strong><\/p>\r\n\r\n\r\n\r\n<p><em>var, Kotlin\u2019s keyword representing mutable, non-final <span style=\"color:#000000\" class=\"has-inline-color\">variables<\/span>. Once initialized, we\u2019re free to mutate\/change the data held by the variable.<\/em><\/p>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">How this works:<\/span><\/strong><\/p>\r\n\r\n\r\n\r\n<p>var myVariable = 1<\/p>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">Variables declared as one data type and then initialized with a value of the wrong type will result in an error.<\/span><\/strong><\/p>\r\n\r\n\r\n\r\n<p>var myVariable: Int = b \/\/ERROR! as b is a different type<\/p>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">There is example for var(Mutable property):<\/span><\/strong><\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/var.png\" alt=\"\" class=\"wp-image-100\"\/><\/figure>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/var_error.png\" alt=\"\" class=\"wp-image-101\"\/><\/figure>\r\n\r\n\r\n\r\n<p><span style=\"color:#000000\" class=\"has-inline-color\">There is error on btnSigin because of mutable property of Button.<\/span><\/p>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">val<\/span><\/strong><\/p>\r\n\r\n\r\n\r\n<p>Val keyword works the same as the var keyword, but with one key difference, the variable is read-only\/nonmutable. The use of Val is like declaring a new variable in Java with the final keyword.<\/p>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">For example, in Kotlin:<\/span><\/strong><\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/strng-1.png\" alt=\"\" class=\"wp-image-103\"\/><\/figure>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">Whereas in Java:<\/span><\/strong><\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/var.png\" alt=\"\" class=\"wp-image-104\"\/><\/figure>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">lateinit<\/span><\/strong><\/p>\r\n\r\n\r\n\r\n<p>lateinit means late initialization. If you do not want to initialize a variable in the constructor instead you want to initialize it later on and if you can guarantee the initialization before using it, then declare that variable with lateinit keyword. It will not allocate memory until initialized.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/lateinit.png\" alt=\"\" class=\"wp-image-105\"\/><\/figure>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/lateinit.png\" alt=\"\" class=\"wp-image-106\"\/><\/figure>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">Method Definitions:<\/span><\/strong><\/p>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">Kotlin: <\/span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <\/strong><\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/method.png\" alt=\"\" class=\"wp-image-107\" width=\"197\" height=\"68\"\/><\/figure>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/boolean_method.png\" alt=\"\" class=\"wp-image-109\"\/><\/figure>\r\n\r\n\r\n\r\n<p><strong>&nbsp; &nbsp; Java:&nbsp;<\/strong><\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/method_java-1.png\" alt=\"\" class=\"wp-image-110\"\/><\/figure>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">Use inner class:<\/span><\/strong><\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/inner_class.png\" alt=\"\" class=\"wp-image-111\"\/><\/figure>\r\n\r\n\r\n\r\n<p><strong><a href=\"#000000\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"color:#000000\" class=\"has-inline-color\">Intent<\/span><\/a><\/strong>:<\/p>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">Kotlin<\/span><\/strong><span style=\"color:#000000\" class=\"has-inline-color\">:<\/span><\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/intent.png\" alt=\"\" class=\"wp-image-112\"\/><\/figure>\r\n\r\n\r\n\r\n<p><strong><span style=\"color:#000000\" class=\"has-inline-color\">Java<\/span><\/strong>:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/intent_java.png\" alt=\"\" class=\"wp-image-113\"\/><\/figure>\r\n\r\n\r\n\r\n<p><\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Ever since when I first started to learn Java, never did I realize that something so similar could be made. I started with Core, and Advanced and began my journey. Java is one of the oldest and easiest languages. It has been at the top for more than two decades and it is still a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2010,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5,6],"tags":[],"class_list":["post-96","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general","category-personal","category-technical"],"rttpg_featured_image_url":{"full":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/flutter_start.png",311,162,false],"landscape":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/flutter_start.png",311,162,false],"portraits":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/flutter_start.png",311,162,false],"thumbnail":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/flutter_start-150x150.png",150,150,true],"medium":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/flutter_start-300x156.png",300,156,true],"large":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/flutter_start.png",311,162,false],"1536x1536":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/flutter_start.png",311,162,false],"2048x2048":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/flutter_start.png",311,162,false]},"rttpg_author":{"display_name":"Admin","author_link":"https:\/\/anviam.com\/blog\/author\/admin16anviam_root05\/"},"rttpg_comment":38,"rttpg_category":"<a href=\"https:\/\/anviam.com\/blog\/category\/general\/\" rel=\"category tag\">General<\/a> <a href=\"https:\/\/anviam.com\/blog\/category\/personal\/\" rel=\"category tag\">Personal<\/a> <a href=\"https:\/\/anviam.com\/blog\/category\/technical\/\" rel=\"category tag\">Technical<\/a>","rttpg_excerpt":"Ever since when I first started to learn Java, never did I realize that something so similar could be made. I started with Core, and Advanced and began my journey. Java is one of the oldest and easiest languages. It has been at the top for more than two decades and it is still a&hellip;","_links":{"self":[{"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/posts\/96"}],"collection":[{"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/comments?post=96"}],"version-history":[{"count":3,"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":2032,"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/posts\/96\/revisions\/2032"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/media\/2010"}],"wp:attachment":[{"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}