{"id":55969,"date":"2016-05-07T15:00:42","date_gmt":"2016-05-07T12:00:42","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=55969"},"modified":"2016-05-08T13:17:41","modified_gmt":"2016-05-08T10:17:41","slug":"kotlin-java-developers-10-features-will-love-kotlin","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html","title":{"rendered":"Kotlin for Java Developers: 10 Features You Will Love About Kotlin"},"content":{"rendered":"<p>Kotlin is a statically typed JVM language built by Jetbrains, the makers of the IntelliJ IDE. <a href=\"https:\/\/kotlinlang.org\/\" target=\"_blank\">Kotlin<\/a>\u00a0is built\u00a0upon Java and provides useful features such as null-safety, data classes, extensions, functional concepts, smart casts, operator overloading and more.<\/p>\n<p>This crash course into Kotlin for Java developers demonstrates the most important advantages that Kotlin has over Java and compares some of the language concepts. You can skim the code snippets and boldly marked parts for a quick overview but I recommend you read the whole article (even though it is rather long).<span id=\"more-845\"><\/span><\/p>\n<h2>Why Would I Care About Kotlin?<\/h2>\n<p>What made me particularly interested in Kotlin is the fact that it is <strong>uber-interoperable with Java<\/strong> and is backed up by Jetbrains and their popular Java IDE IntelliJ.\u00a0Why did that make me more interested in Kotlin you ask?<\/p>\n<p>Well, interoperability with Java is majorly important because Java has been one of the <a href=\"http:\/\/www.tiobe.com\/index.php\/content\/paperinfo\/tpci\/index.html\" target=\"_blank\">most widely used programming language<\/a> for quite a while now. From a business perspective, this means that most real-world code is written in Java and that companies want to maintain their Java code base as long as possible \u2014 typically, the value of the legacy code is in the millions.<\/p>\n<p><strong>With Kotlin, organizations have the chance to try out a new programming language with minimal risk.<\/strong> Java files can be converted to equivalent Kotlin files which can then be worked on. Similarly, all types defined in Kotlin (like classes and enums) can be used from within Java just like any other Java type. From a developer point of view, it is great to be able to use the Java libraries that you are used to. You can use Java IO, JavaFX, Apache Commons, Guava and all\u00a0your own classes\u00a0right from Kotlin.<\/p>\n<p>Also, hyperbolically, <strong>a programming language is only as good as its tool support<\/strong>. This is why the second point speaking in favor of Kotlin for me was that IntelliJ provides built-in language\u00a0support. It also contains the aforementioned Java-to-Kotlin converter and code generators for Java and JavaScript from Kotlin code.<\/p>\n<p>These two points also separate\u00a0Kotlin from other JVM languages such as Scala, Ceylon, Clojure or Groovy.<\/p>\n<p><strong>Alright, enough talk.<\/strong> Let\u2019s jump into the actual language features of\u00a0Kotlin.<\/p>\n<h2>Null Safety<\/h2>\n<pre class=\"brush:java\">class Person {\r\n    val givenName: String = \"\"\r\n    val familyName: String = \"\"\r\n    val address: Address? = null\r\n}<\/pre>\n<p>In this example, givenName and familyName cannot be null \u2014 the program would <strong>fail at compile-time<\/strong>. You\u00a0must explicitly make a variable nullable to be able to assign null to it. This is done via the \u201c?\u201d after the variable type. So the address property may be null in the given code.<\/p>\n<p>Kotlin also fails at compile-time whenever a\u00a0NullPointerException may\u00a0be thrown at run-time \u2014 that is, when you try to call a method or reference a property from a nullable type:<\/p>\n<pre class=\"brush:java\">val givenName: String? = null\r\nval len = givenName.length<\/pre>\n<p>If you try to compile this, the Kotlin compiler will give you an error: \u201cOnly safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type kotlin.String?\u201d. We\u2019ll see how to handle these cases where you\u00a0<em>know<\/em>\u00a0the variable <em>cannot<\/em> be null in a second.<\/p>\n<p>So far so good, but what are those safe and non-null asserted calls the compiler is talking about?\u00a0<strong>Safe calls simply return the value of the call as normally if the callee is not null, and return null otherwise:<\/strong><\/p>\n<pre class=\"brush:java\">val givenName: String? = \"\"\r\nval len = givenName?.length<\/pre>\n<p>In this case, len will be zero as expected. If givenName was null, len would also be assigned null. Thus the return type of givenName?.length is Int?, a nullable integer.<\/p>\n<p>With non-null asserted calls, you\u00a0assert to the compiler that you\u00a0<em>know<\/em>\u00a0the variable <em>cannot<\/em> be null at run-time at the position you use it:<\/p>\n<pre class=\"brush:java\">val givenName: String? = \"Roger\"\r\nval len = givenName!!.length<\/pre>\n<p>To work with nullable types effectively, the <strong>Elvis operator<\/strong> comes in handy. It allows you to use a nullable if it is not null and a default value otherwise:<\/p>\n<pre class=\"brush:java\">val text: String? = null\r\nval len = text?.length ?: -1<\/pre>\n<p>In this example, len will be -1 because the nullable text is in fact null so that the defined default value is used. You may have noticed that this is basically just the widely known <em>ternary operator<\/em> where the first operand is equal to the expression itself:<\/p>\n<pre class=\"brush:java\">val len = text?.length ?: -1\r\nval len = text?.length ? text?.length : -1<\/pre>\n<p>These two lines are semantically the same.<\/p>\n<h2>Data Classes<\/h2>\n<p>For simple classes which mainly hold data, you\u00a0can avoid a lot of boilerplate compared to Java code. Consider the following <strong>typical data class in Java:<\/strong><\/p>\n<pre class=\"brush:java\">class Book {\r\n    private String title;\r\n    private Author author;\r\n\r\n    public String getTitle() {\r\n        return title;\r\n    }\r\n    \r\n    public void setTitle(String title) {\r\n        this.title = title;\r\n    }\r\n\r\n    public Author getAuthor() {\r\n        return author;\r\n    }\r\n    \r\n    public void setAuthor(Author author) {\r\n        this.author = author;\r\n    }\r\n}<\/pre>\n<p>Lots of boilerplate code that\u00a0you\u2019ll\u00a0skip when trying to find out what the class really does. <strong>In Kotlin, you can define the same class concisely in one line:<\/strong><\/p>\n<pre class=\"brush:java\">data class Book(var title: String, var author: Author)<\/pre>\n<p>Kotlin will also <strong>generate useful hashCode(), equals(), and toString()<\/strong> implementations. Printing a book will create an output like Book(title=Effective Java, author=Author(name=Joshua Bloch)).<\/p>\n<blockquote>\n<p><strong>Challenge<\/strong>: <a href=\"http:\/\/try.kotlinlang.org\/\" target=\"_blank\">Write an Author class<\/a> that will lead to an output like this!<\/p>\n<\/blockquote>\n<p>Not only that, it will also allow you to <strong>easily make copies of data classes:<\/strong><\/p>\n<pre class=\"brush:java\">val book = Book(\"Effective Java\", Author(\"Joshua Bloch\"))\r\nval copy = book.copy()\r\nval puzzlers = book.copy(title = \"Java Puzzlers\")\r\nval gof = book.copy(title = \"Design Patterns\", author = Author(\"Gang of Four\"))<\/pre>\n<p>You\u00a0can change arbitrary properties of the copied object by adding named parameters to the copy() method.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>One more goody is the possibility to use <strong>destructuring declarations<\/strong> on data classes, retrieving the respective property values:<\/p>\n<pre class=\"brush:java\">val book = Book(\"The Phoenix Project\", Author(\"Kevin Behr\"))\r\nval (title, author) = book<\/pre>\n<h2>3) Extension Functions<\/h2>\n<p>Kotlin allows us to <strong>extend the functionality of existing classes without inheriting from them<\/strong>. This is enabled by extension functions and extension properties. Let\u2019s say you want to extend the GridPane class from the JavaFX GUI framework with a method to retrieve the elements in row i and column j:<\/p>\n<pre class=\"brush:java\">fun GridPane.getElementAt(rowIndex: Int, columnIndex: Int): Node? {\r\n    this.children.forEach {\r\n        if (GridPane.getColumnIndex(it) == columnIndex &amp;&amp; GridPane.getRowIndex(it) == rowIndex) {\r\n            return it;\r\n        }\r\n    }\r\n\r\n    return null;\r\n}<\/pre>\n<p>There are several things to mention here. First, inside the extension function you can refer to the object on which it was called using \u201cthis\u201d. Second, you use Kotlin\u2019s forEach() function on the list of child nodes. This is equivalent to the forEach() method included in Java 8 and allows some functional-style programming. Third, inside the forEach(), you can refer to the current element using the implicit loop variable \u201cit\u201d.<\/p>\n<p>Note that the return type comes after the parentheses containing the parameters and is a nullable Node since the method\u00a0may return null.<\/p>\n<p><strong>There\u2019s one thing to be aware of:<\/strong>\u00a0If you try to call\u00a0an extension function with the arguments that are also applicable for an existing member function inside the class, the member will always \u201cwin\u201d \u2014 meaning that it will take precedence and overshadow your extension function.<\/p>\n<h2>4) Smart Casts<\/h2>\n<p>How often have you already cast objects where it was actually redundant? More often than you can count I bet, like this:<\/p>\n<pre class=\"brush:java\">if (node instanceof Leaf) {\r\n    return ((Leaf) node).symbol;\r\n}<\/pre>\n<p>The Kotlin compiler on the other hand is really intelligent when it comes to casts. Meaning: <strong>it will handle all those redundant casts for you<\/strong>. This is called <strong>smart casts<\/strong>.<\/p>\n<p>The equivalent Kotlin code for the code snippet above looks like this:<\/p>\n<pre class=\"brush:java\">if (node is Leaf) {\r\n    return node.symbol;\r\n}<\/pre>\n<p>The instanceof operator in Kotlin is called \u201cis\u201d. And, more importantly, there is no need to clutter your code with casts that the compiler can actually take care of.<\/p>\n<p>Now this goes much further than just this simple case:<\/p>\n<pre class=\"brush:java\">if (person !is Student)\r\n    return\r\n\r\nperson.immatriculationNumber<\/pre>\n<p>In this case, if person were not a student, the control flow would never reach line 4. Accordingly, the Kotlin compiler knows that person must be Student object and performs a smart cast.<\/p>\n<p>Let\u2019s look at some <strong>lazily evaluated conditional expressions<\/strong>:<\/p>\n<pre class=\"brush:java\">if (document is Payable &amp;&amp; document.pay()) { \/\/ Smart cast\r\nprintln(\"Payable document ${document.title} was payed for.\")\r\n}<\/pre>\n<p>Conditionals like these use lazy evaluation in Kotlin, just like in Java. So if the document were not a Payable, the second part would not be evaluated in the first place. Hence, if evaluated, Kotlin knows that document is a Payable and uses a smart cast.<\/p>\n<p>The same goes for disjunction:<\/p>\n<pre class=\"brush:java\">if (document !is Payable || document.pay() == false) {  \/\/ Smart cast\r\n    println(\"Cannot pay document ${document.title}.\")\r\n}<\/pre>\n<p>When expressions are another place where Kotlin will apply smart casts wherever possible:<\/p>\n<pre class=\"brush:java\">val result = when (expr) {\r\n    is Expr.Number      -&gt; expr.value\r\n    is Expr.Sum         -&gt; expr.first + expr.second\r\n    is Expr.Difference  -&gt; expr.first - expr.second\r\n    is Expr.Exp         -&gt; Math.pow(expr.base, expr.exponent)\r\n}<\/pre>\n<p>Depending on the type of the object, you can simply use the respective properties in each when block.<\/p>\n<p>Note: For the\u00a0example above, the Expr class must be a sealed class with only these exact four subclasses.<\/p>\n<h2>5) Type Inference<\/h2>\n<p><strong>In Kotlin, you\u00a0don\u2019t have to specify the type of each variable explicitly, even though Kotlin <em>is<\/em> strongly typed<\/strong>. You\u00a0can choose to explicitly define a data type, for example if you\u00a0don\u2019t want a small integer to be stored in an Int variables but rather a Short or even a Byte. You\u00a0can do that using the colon notation where the data type stands behind the variable name:<\/p>\n<pre class=\"brush:java\">val list: Iterable = arrayListOf(1.0, 0.0, 3.1415, 2.718)  \/\/ Only need Iterable interface\r\n\r\nval arrayList = arrayListOf(\"Kotlin\", \"Scala\", \"Groovy\")  \/\/ Type is ArrayList<\/pre>\n<p>You can choose to use explicit types as in Java but you\u2019re also free to write more concise Python-like variable declarations. Explicit types are useful to reference the most general interface (which you should always do).<\/p>\n<h2>6)\u00a0Functional Programming<\/h2>\n<p>While Java evolved to incorporate several functional programming concepts since Java 8, Kotlin has functional programming baked right in. This includes <strong>higher-order functions, lambda expressions, operator overloading, lazy evaluation<\/strong> and lots of useful methods to work with collections.<\/p>\n<p>The combination of lambda expressions and the Kotlin library really makes your day easier when working with collections:<\/p>\n<pre class=\"brush:java\">val numbers = arrayListOf(-42, 17, 13, -9, 12)\r\nval nonNegative = numbers.filter { it &gt;= 0 }\r\nprintln(nonNegative)<\/pre>\n<p>Note that, when using lambda expressions with a single argument, Kotlin creates an implicit variable called \u201cit\u201d which refers to the lambda expression\u2019s only argument. So the second line above is\u00a0equivalent to:<\/p>\n<pre class=\"brush:java\">val nonNegative = numbers.filter { it -&gt; it &gt;= 0 }<\/pre>\n<p>Kotlin provides all essential functional facilities such as <strong>filter, map &amp; flatMap, take &amp; drop, first &amp; last, fold &amp; foldRight, forEach,\u00a0<\/strong>reduce<em>, <\/em>and anything else the pragmatic functional programmer\u2019s heart longs for:<\/p>\n<pre class=\"brush:java\">println(numbers.take(2))  \/\/ First two elements: [-42, 17]\r\n\r\nprintln(numbers.drop(2))  \/\/ List without first two elements: [13, -9, 12]\r\n\r\nprintln(numbers.foldRight(0, { a, b -&gt; a + b }))  \/\/ Sum of all elements: -9\r\n\r\nnumbers.forEach { print(\"${it * 2} \") }  \/\/ -84 34 26 -18 24\r\n\r\n---\r\n\r\nval genres = listOf(\"Action\", \"Comedy\", \"Thriller\")\r\nval myKindOfMovies: Iterable&lt;String&gt; = genres.filter { it.length &lt;= 6 }.map { it + \" Movie\" }\r\nprintln(myKindOfMovies)  \/\/ [Action Movie, Comedy Movie]<\/pre>\n<h2>7) Objects (aka Easily Create Singletons)<\/h2>\n<p>Kotlin has a keyword called <em>object<\/em> which allows us to define an object, similar to a class. But of course, that object then only exists as a <strong>single instance<\/strong>. <strong>This is a useful way to create singletons<\/strong> but the feature is not restricted to only singletons.<\/p>\n<p>Creating an object is as simple as this:<\/p>\n<pre class=\"brush:java\">object CardFactory {\r\n    \r\n    fun getCard(): Card {\r\n        \/\/ ...\r\n    }\r\n}<\/pre>\n<p>And you can then use that object like a class with static members:<\/p>\n<pre class=\"brush:java\">fun main(args: Array&lt;String&gt;) {\r\n    val card = CardFactory.getCard()\r\n}<\/pre>\n<p>You can even <strong>let your objects\u00a0have superclasses<\/strong>:<\/p>\n<pre class=\"brush:java\">object SubmitButtonListener : ActionListener {\r\n    \r\n    override fun actionPerformed(e: ActionEvent?) {\r\n        \/\/ Submit form...\r\n    }\r\n}<\/pre>\n<p>This concept is a powerful extension to the classes, interfaces &amp; enums available in Java because oftentimes, elements of the domain model are inherently objects (they exist only once and thus should always have at most instance at runtime).<\/p>\n<h2>8) Default\u00a0Arguments<\/h2>\n<p>Default arguments are a\u00a0feature I\u2019m dearly missing in Java because it\u2019s just so convenient, makes your code more concise, more expressive, more maintainable, more readable, more everything-that\u2019s-good.<\/p>\n<p>In Java, you often have to duplicate code in order <strong>define different variants of a method or constructor<\/strong>. Take a look at this:<\/p>\n<pre class=\"brush:java\">public class NutritionFacts {\r\n    private final String foodName;\r\n    private final int calories;\r\n    private final int protein;\r\n    private final int carbohydrates;\r\n    private final int fat;\r\n    private final String description;\r\n\r\n    public NutritionFacts(String foodName, int calories) {\r\n        this.foodName = foodName;\r\n        this.calories = calories;\r\n        this.protein = -1;\r\n        this.carbohydrates = -1;\r\n        this.fat = -1;\r\n        this.description = \"\";\r\n    }\r\n\r\n    public NutritionFacts(String foodName, int calories, int protein, int carbohydrates, int fat) {\r\n        this.foodName = foodName;\r\n        this.calories = calories;\r\n        this.protein = protein;\r\n        this.carbohydrates = carbohydrates;\r\n        this.fat = fat;\r\n        this.description = \"\";\r\n    }\r\n\r\n    public NutritionFacts(String foodName, int calories, int protein, int carbohydrates, int fat, String description) {\r\n        this.foodName = foodName;\r\n        this.calories = calories;\r\n        this.protein = protein;\r\n        this.carbohydrates = carbohydrates;\r\n        this.fat = fat;\r\n        this.description = description;\r\n    }\r\n}<\/pre>\n<p>Ugh, pretty bad. But it gets even worse if you want to provide more different constructors or add more possible attributes.<\/p>\n<p>The example above demonstrate the\u00a0so-called <em>telescoping anti-pattern<\/em> prevalent in Java. You can improve this design by using the Builder pattern instead.<\/p>\n<p>But with Kotlin, you can do this even easier using default values for arguments:<\/p>\n<pre class=\"brush:java\">class NutritionFacts(val foodName: String,\r\n                     val calories: Int,\r\n                     val protein: Int = 0,\r\n                     val carbohydrates: Int = 0,\r\n                     val fat: Int = 0,\r\n                     val description: String = \"\") {\r\n}<\/pre>\n<p>This makes each of the parameters with a default value an <em>optional parameter<\/em>. And it\u00a0actually gives you many more possibilities to invoke the constructor than the Java class above:<\/p>\n<pre class=\"brush:java\">val pizza = NutritionFacts(\"Pizza\", 442, 12, 27, 24, \"Developer's best friend\")\r\nval pasta = NutritionFacts(\"Pasta\", 371, 14, 25, 11)\r\nval noodleSoup = NutritionFacts(\"Noodle Soup\", 210)<\/pre>\n<p>Note that you may also want to make this sort of class a <em>data class<\/em> to have methods like equals() and toString() generated for you.<\/p>\n<p><strong>In short: You get more for less. And keep your code clean at the same time.<\/strong><\/p>\n<h2>9) Named Arguments<\/h2>\n<p>Default arguments become even more powerful in combination with named arguments:<\/p>\n<pre class=\"brush:java\">val burger = NutritionFacts(\"Hamburger\", calories = 541, fat = 33, protein = 14)\r\nval rice = NutritionFacts(\"Rice\", 312, carbohydrates = 23, description = \"Tasty, nutritious grains\")<\/pre>\n<p><strong>Anyone reading the code knows what\u2019s going on<\/strong> without having to look at what the parameters mean.\u00a0This increases readability and can make you more productive when used correctly. For example, this is especially useful when you have several boolean parameters like this:<\/p>\n<pre class=\"brush:java\">myString.transform(true, false, false, true, false)<\/pre>\n<p>Unless you\u2019ve implemented that function 10 seconds ago, there\u2019s no way you know what\u2019s going on here (there\u2019s no guarantee you know even<em> if<\/em> you\u2019ve implemented it 10 seconds ago).<\/p>\n<p>Make your life (and that of your fellow developers) easier by using named arguments:<\/p>\n<pre class=\"brush:java\">myString.transform(\r\n    toLowerCase = true,\r\n    toUpperCase = false,\r\n    toCamelCase = false,\r\n    ellipse = true, \r\n    normalizeSpacing = false\r\n)<\/pre>\n<h2>10) Bonus: Enforcing Best Practices<\/h2>\n<p>Generally, Kotlin enforces many of the best practices you should follow when using Java. You can read about them in Josh Bloch\u2019s book <a href=\"http:\/\/www.oracle.com\/technetwork\/java\/effectivejava-136174.html\" target=\"_blank\">\u201cEffective Java\u201d<\/a>.<\/p>\n<p>First of all, the use of <strong>val vs. var<\/strong> promotes making every variable final that is not supposed to change \u2014 while also providing a more concise syntax for it. This is useful when creating immutable objects for example.<\/p>\n<p>This way, beginners learning the language will also learn to follow this practice right from the start because you tend to think about whether to use <strong>val<\/strong> or <strong>var<\/strong> each time and learn to prefer val to var when possible.<\/p>\n<p>Next, Kotlin also supports the principle to either design for inheritance or prohibit it \u2014 because in Kotlin, you\u00a0have to explicitly declare a class as <strong>open<\/strong>\u00a0in order to inherit from it. That way, you have to remember to <em>allow<\/em> inheritance instead of having to remember to <em>disallow<\/em> it.<\/p>\n<h2>What Now?<\/h2>\n<p><strong>If this overview made you curious<\/strong> to learn more about Kotlin, you can <a href=\"http:\/\/petersommerhoff.com\/dev\/kotlin\/kotlin-beginner-tutorial\/\" target=\"_blank\">check out my 10 beginner tutorial videos for Kotlin<\/a> or <strong><a href=\"https:\/\/www.udemy.com\/kotlin-course\/?couponCode=AMAZINGREADERS25\">go straight to the full course<\/a> (with 95% reader discount)<\/strong>.<\/p>\n<p><strong>The course is beginner-friendly and starts completely from scratch.<\/strong> If you already know Java or a comparable language, you\u2019ll still find it a valuable resource to get to know Kotlin.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/petersommerhoff.com\/dev\/kotlin\/kotlin-for-java-devs\/\">Kotlin for Java Developers: 10 Features You Will Love About Kotlin<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/join-us\/jcg\/\">JCG partner<\/a>\u00a0<span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Peter Sommerhoff&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:6659,&quot;3&quot;:[null,0],&quot;4&quot;:[null,2,16777215],&quot;12&quot;:0,&quot;14&quot;:[null,2,2236962],&quot;15&quot;:&quot;arial, sans-serif&quot;}\">Peter Sommerhoff<\/span> at the <a href=\"http:\/\/petersommerhoff.com\/\">PeterSommerhoff<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Kotlin is a statically typed JVM language built by Jetbrains, the makers of the IntelliJ IDE. Kotlin\u00a0is built\u00a0upon Java and provides useful features such as null-safety, data classes, extensions, functional concepts, smart casts, operator overloading and more. This crash course into Kotlin for Java developers demonstrates the most important advantages that Kotlin has over Java &hellip;<\/p>\n","protected":false},"author":1030,"featured_media":13674,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[785],"tags":[],"class_list":["post-55969","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kotlin"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Kotlin for Java Developers: 10 Features You Will Love About Kotlin - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Kotlin is a statically typed JVM language built by Jetbrains, the makers of the IntelliJ IDE. Kotlin\u00a0is built\u00a0upon Java and provides useful features such\" \/>\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.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Kotlin for Java Developers: 10 Features You Will Love About Kotlin - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Kotlin is a statically typed JVM language built by Jetbrains, the makers of the IntelliJ IDE. Kotlin\u00a0is built\u00a0upon Java and provides useful features such\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-05-07T12:00:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-05-08T10:17:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/06\/jetbrains-kotlin-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Peter Sommerhoff\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Peter Sommerhoff\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2016\\\/05\\\/kotlin-java-developers-10-features-will-love-kotlin.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2016\\\/05\\\/kotlin-java-developers-10-features-will-love-kotlin.html\"},\"author\":{\"name\":\"Peter Sommerhoff\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/8f7f19c3d793c9db21144ffa9ea07a9b\"},\"headline\":\"Kotlin for Java Developers: 10 Features You Will Love About Kotlin\",\"datePublished\":\"2016-05-07T12:00:42+00:00\",\"dateModified\":\"2016-05-08T10:17:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2016\\\/05\\\/kotlin-java-developers-10-features-will-love-kotlin.html\"},\"wordCount\":2163,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2016\\\/05\\\/kotlin-java-developers-10-features-will-love-kotlin.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2013\\\/06\\\/jetbrains-kotlin-logo.jpg\",\"articleSection\":[\"Kotlin\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2016\\\/05\\\/kotlin-java-developers-10-features-will-love-kotlin.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2016\\\/05\\\/kotlin-java-developers-10-features-will-love-kotlin.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2016\\\/05\\\/kotlin-java-developers-10-features-will-love-kotlin.html\",\"name\":\"Kotlin for Java Developers: 10 Features You Will Love About Kotlin - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2016\\\/05\\\/kotlin-java-developers-10-features-will-love-kotlin.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2016\\\/05\\\/kotlin-java-developers-10-features-will-love-kotlin.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2013\\\/06\\\/jetbrains-kotlin-logo.jpg\",\"datePublished\":\"2016-05-07T12:00:42+00:00\",\"dateModified\":\"2016-05-08T10:17:41+00:00\",\"description\":\"Kotlin is a statically typed JVM language built by Jetbrains, the makers of the IntelliJ IDE. Kotlin\u00a0is built\u00a0upon Java and provides useful features such\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2016\\\/05\\\/kotlin-java-developers-10-features-will-love-kotlin.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2016\\\/05\\\/kotlin-java-developers-10-features-will-love-kotlin.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2016\\\/05\\\/kotlin-java-developers-10-features-will-love-kotlin.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2013\\\/06\\\/jetbrains-kotlin-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2013\\\/06\\\/jetbrains-kotlin-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2016\\\/05\\\/kotlin-java-developers-10-features-will-love-kotlin.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JVM Languages\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/jvm-languages\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Kotlin\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/jvm-languages\\\/kotlin\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Kotlin for Java Developers: 10 Features You Will Love About Kotlin\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/8f7f19c3d793c9db21144ffa9ea07a9b\",\"name\":\"Peter Sommerhoff\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/aadcfb3d351c8fa1fc217deb257c36dd8cebb1fc1bd7882081d467076700bf10?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/aadcfb3d351c8fa1fc217deb257c36dd8cebb1fc1bd7882081d467076700bf10?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/aadcfb3d351c8fa1fc217deb257c36dd8cebb1fc1bd7882081d467076700bf10?s=96&d=mm&r=g\",\"caption\":\"Peter Sommerhoff\"},\"description\":\"Peter is a software developer from Germany with a passion for software design. He teaches programming, web development and other techy stuff on Udemy and blogs over at PeterSommerhoff.com\",\"sameAs\":[\"http:\\\/\\\/petersommerhoff.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/peter-sommerhoff\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Kotlin for Java Developers: 10 Features You Will Love About Kotlin - Java Code Geeks","description":"Kotlin is a statically typed JVM language built by Jetbrains, the makers of the IntelliJ IDE. Kotlin\u00a0is built\u00a0upon Java and provides useful features such","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.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html","og_locale":"en_US","og_type":"article","og_title":"Kotlin for Java Developers: 10 Features You Will Love About Kotlin - Java Code Geeks","og_description":"Kotlin is a statically typed JVM language built by Jetbrains, the makers of the IntelliJ IDE. Kotlin\u00a0is built\u00a0upon Java and provides useful features such","og_url":"https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2016-05-07T12:00:42+00:00","article_modified_time":"2016-05-08T10:17:41+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/06\/jetbrains-kotlin-logo.jpg","type":"image\/jpeg"}],"author":"Peter Sommerhoff","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Peter Sommerhoff","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html"},"author":{"name":"Peter Sommerhoff","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/8f7f19c3d793c9db21144ffa9ea07a9b"},"headline":"Kotlin for Java Developers: 10 Features You Will Love About Kotlin","datePublished":"2016-05-07T12:00:42+00:00","dateModified":"2016-05-08T10:17:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html"},"wordCount":2163,"commentCount":4,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/06\/jetbrains-kotlin-logo.jpg","articleSection":["Kotlin"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html","url":"https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html","name":"Kotlin for Java Developers: 10 Features You Will Love About Kotlin - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/06\/jetbrains-kotlin-logo.jpg","datePublished":"2016-05-07T12:00:42+00:00","dateModified":"2016-05-08T10:17:41+00:00","description":"Kotlin is a statically typed JVM language built by Jetbrains, the makers of the IntelliJ IDE. Kotlin\u00a0is built\u00a0upon Java and provides useful features such","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/06\/jetbrains-kotlin-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/06\/jetbrains-kotlin-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2016\/05\/kotlin-java-developers-10-features-will-love-kotlin.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JVM Languages","item":"https:\/\/www.javacodegeeks.com\/category\/jvm-languages"},{"@type":"ListItem","position":3,"name":"Kotlin","item":"https:\/\/www.javacodegeeks.com\/category\/jvm-languages\/kotlin"},{"@type":"ListItem","position":4,"name":"Kotlin for Java Developers: 10 Features You Will Love About Kotlin"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/8f7f19c3d793c9db21144ffa9ea07a9b","name":"Peter Sommerhoff","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/aadcfb3d351c8fa1fc217deb257c36dd8cebb1fc1bd7882081d467076700bf10?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/aadcfb3d351c8fa1fc217deb257c36dd8cebb1fc1bd7882081d467076700bf10?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/aadcfb3d351c8fa1fc217deb257c36dd8cebb1fc1bd7882081d467076700bf10?s=96&d=mm&r=g","caption":"Peter Sommerhoff"},"description":"Peter is a software developer from Germany with a passion for software design. He teaches programming, web development and other techy stuff on Udemy and blogs over at PeterSommerhoff.com","sameAs":["http:\/\/petersommerhoff.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/peter-sommerhoff"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/55969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/1030"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=55969"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/55969\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/13674"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=55969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=55969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=55969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}