Starting journey with Kotlin

in #kotlin7 years ago


During Google I/O 17, the Android team announced that they will be officially supporting Kotlin in Android ecosystem! That’s great news for Android community and additional motivation to start learning this modern programming language that runs on the Java Virtual Machine. Please keep in mind that currently the Kotlin can be compiled to JavaScript source code and we can expect that in future we’ll be able to run Kotlin also on iOS platform! So bright future in here. Another cool thing is that we can mix Kotlin with Java in the same project without a hassle, as the integration is seamless.
Taking all above into account I’ve decided to start learning Kotlin on daily basis from today. I’m Android Developer with about 4 years of commercial experience using Java and currently I’m working as a freelancer so don’t hesitate to contact me if you’re looking for a solid developer.
I will be posting things I’ve learned much about once a week so you can learn Kotlin with me! All comments and suggestion are more than welcome.
Talk is cheap. Show me the code.
.. as Linus Torvalds said and that’s fair. Let’s first say hello.
fun main(args: Array< String>) {
println(“Hello, world!”)
}
As we can see code looks similarly to Java but let’s jump into differences:
Keyword fun indicates that main is a function
Unlike in Java, first we declare variable name, then put : character and finally the type of that variable
Semicolons ; are optional in Kotlin
Wrapper method println which uses standardSystem.out.println
Above was just a warm up. Next we have the max method in Java:

public final int max(int a, int b) {
return a > b ? a : b;
}

I belive there’s not need to describe that so let’s see that written in Kotlin:

fun max(a: Int, b: Int) = if (a > b) a else b

There are some new concepts here:
we could skip brackets {}
no need to define return type
if returns a value so there is no need to write ? and : to handle returning values
Int instead of Java’s primitive int
Running Kotlin
For beginners it’s nice that we can play with Kotlin in web browser: https://try.kotlinlang.org
But that’s not good enough for us after a while. With currently available Android Studio 3.0 Canary 1 we have needed plugin for Kotlin pre-bundled so let’s open AS 3.0 and create a new project.

As you can see there is new option called “Include Kotlin support” which I’ve already enabled. Next steps are the same as in previous version of Android Studio.
I created empty Activity and that what I’ve seen:
package pl.adamchodera.learningkotlin

import android.os.Bundle
import android.support.v7.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
}

}

The first few lines with package and import keywords are very standard to Java expect missing (and not needed) semicolons.
Next there is class keyword followed by our Activity name, then : character instead of extends we used to write when extending classes in Java. Please note that right now we’re writing code in Kotlin and also extending class written in Java! How mighty is that? — you can ask and I will tell that it’s just a begging to our journey with Kotlin.
Instead of @Override annotation we should simply inline override keyword with method declaration like in example above.
The last difference I want to tackle today is ? character next toBundle parameter. That indicates that this particular variable parameter can be null.
Thank you for reading all along and I hope you enjoyed it and will check out my next blog post about Kotlin in Android development!
Next week we’ll learn more details about language syntax and some cool features we can use to ease our busy life.

Sort:  

Congratulations @adam.chodera! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You published your First Post
You made your First Comment

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @adam.chodera! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @adam.chodera! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.19
TRX 0.16
JST 0.033
BTC 64039.14
ETH 2755.32
USDT 1.00
SBD 2.70