Java Makes Kotlin Fun! 88 of 100 Days of…
I’m having a blast learning how to use Kotlin!
You can find a video of this at the bottom of the page.
For starters, I finally figured out how to write a method, or function, in Kotlin. They’re pretty much the same. They’re called the same thing in C++. In order to specify a function, it must be prefaced with the fun keyword (click below).
I tried to create a class the last time. I was messing around using my experience with Java as reference. Now I see that the fun keyword is meant to tell the Kotlin compiler where functions begin in the code. Either way, I fixed that function from a couple weeks ago and was able to output some text on screen (click below).
Similar to Java, Kotlin uses the print and println methods so this part was relatively straightforward. However, I found some new ways to play around with printing text. I’ll come back to this later.
Variables are declared using the val keyword. No data type required! While Java uses various keywords for different types of numbers, Strings, etc, Kotlin simplifies this by using val for all variables.
You don’t need to learn a variety of data types since the Kotlin compiler discerns between the types of data based on the value stored in the variable!
Remember what I said about playing around with text output? Let’s say you’re using Java and want to add a String variable to the output. You separate the String literals to the left and right of the variable using the concatenation operator (+) Your code would look something like this (click below):
It makes sense, right? You’re combining a series of String literals to form some sort of output. But what if I told you Kotlin has a better way? All you need to do is insert the variable in curly braces prefaced by the $ symbol. It goes directly into the String literal! (Or string template as it’s called in Kotlin.) (click below)
This reminds me of the escape sequences in Java. Inserting them into a String literal adds punctuation or augments the output in some way. Here, the $ symbol is used to reference a variable or expression.
One last thing. The Kotlin Playground IDE uses version 1.4.1 even though 1.4.2 was recently announced (click below).
It should still prove useful for learning Kotlin syntax, but be sure to use tools locally and update to the latest version so your setup stays up to date.
I’ll keep working through the basics and update you as I go along!
Join the mailing list to see updates like this every week!
Coding Fanatic
Android Basics: Introduction to Kotlin
https://developer.android.com/courses/pathways/android-basics-kotlin-one#codelab-https://developer.android.com/codelabs/basic-android-kotlin-training-kotlin-birthday-message
IMG android_kotlin
http://www.androidguys.com/news/smart-tips-to-develop-android-apps-using-kotlin-a-guide-for-beginners/