#Start learning Kotlin for Kotlin Multi-Platform.

1 messages · Page 1 of 1 (latest)

covert niche
#

As a java developer, how would you start learning Kotlin?
My main goal is to learn it as a language and be confident and then make it more inclined to android development. As, KMP is Android development with Compose framework that extended it's support to other platforms: ios, desktop and web.
For starters, I went through the basic syntax as I used to think java and kotlin are alike unlit i found out there is whole new area to explore and it's a different language ofc. i'll need some tips on how to proceed with kotlin.

ashen shoalBOT
#

<@&1008423204219531294> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
As a java developer, how would you start learning Kotlin? My main goal is to learn it as a language and be confident and then make it more inclined to android development. As, KMP is Android development with Compose framework that extended it's support to

As a Java developer diving into Kotlin, especially with the goal of Kotlin Multi-Platform (KMP) and Android development, here’s a focused approach:

  1. Start with Kotlin fundamentals:
    Since you already know Java, begin by understanding Kotlin’s core syntax and features that differ or improve upon Java — null safety, data classes, extension functions, coroutines, type inference, and lambdas. Official Kotlin documentation and JetBrains’ Kotlin Koans are great for this.

  2. Practice writing idiomatic Kotlin:
    Convert simple Java classes to Kotlin by hand. This helps internalize differences rather than relying on automatic converters.

  3. Get comfortable with Kotlin tooling:
    Use IntelliJ IDEA or Android Studio. Explore their Kotlin-specific inspections and quick fixes.

  4. Explore Kotlin for Android:
    Once fundamentals are solid, start learning Android-specific Kotlin extensions, Jetpack Compose basics, and Android coroutines usage.

  5. Then move to Kotlin Multi-Platform:
    Understand KMP architecture — shared code versus platform-specific code. Try writing library modules to share code between Android and JVM/Native.

  6. Follow tutorials and build sample projects:
    Practical projects that combine Kotlin basics with Android and KMP concepts solidify your skillset.


Useful links:

pastel elk
#

theres a goto book called Effective Kotlin

#

its written by the authors of the language afaik

#

its great imo

#

that said, kotlins website has good tutorials as well

#

the website is great but its a bit on the surface

#

and it attempts to teach kotlin isolated

#

without java context

#

but thats kinda how kotlin is best learned imo, with java context

#

the effective kotlin book approaches it as follows:

#

"hey intermediate java dev. ill expect u to know java, so lets learn kotlin now. we didnt like x about java, thats why we made y in kotlin, see this example: ..."

#

so u learn why and how

#

and on top they teach u the thinking/mindset for writing kotlin

#

so u stop writing "java in disguise"

#

so yeah, my recommendation is that book

#

i think what confused me the most at first was that there is no static

#

instead they have a concept called companian object

covert niche
pastel elk
#

the majority of features in kotlin feel like convenience-sugar

#

so they are easy to learn

covert niche
pastel elk
#

yeah. the idea is that ur class is divided into two sections: the normal stuff and the "static stuff" (static stuff being the companion object then)

covert niche
pastel elk
#

they have these flow-methods .let, .run, .with, .apply, .also (they are called scope functions).
ull see that beiing used a lot and it makes writing code just a bit more convenient

#

it feels like an endless .stream() from java

#

example:

#
return foo.also { log.info("blabla") }
#

or

#
return foo.let{ ... }.let{...}.apply{...}.let{...}
#

another convenience is that within these {...} (they are essentially lambdas), u implicitly have the argument called it

#

like in java u would have to write persons.stream().map(p -> p.getName())

#

in kotlin that p is implicitly called it so u can ommit it

#

persons.map{ it.name() }

covert niche
#

Oh yeah, Flows have a lot of similar methods to stream but the main different is that Flows have a coroutine context right?

pastel elk
#

u can also ommit return iin many cases because the last line of a .let{...} and similar is implicitly returned

#

so for example

covert niche
pastel elk
#
var name = person.let {
  it.marry(bob)
  it.name()
}
#

they also have a lot of convenience around the getter/setter topic and constructors

#

they have string interpolation:

#
println("hello $name, you are ${p.age()} years old")
#

they dont have void but instead call it Unit

#

lambdas can feel a bit confusiing at first if u dig into it. the reason being is that they iintroduced this feature before java added them in java 8. so they designed it slightly different

#

like, kotlin lambdas dont compile agaiinst functional interfaces

#

another syntax thing is that u can do curly braces like a block if the lambda is the last parameter in a method

#

so if u write a method that wants, lets say, a string and then a lambda, u can call it as:

#
foo(name) {
  lambda body
}
#

instead of foo(name, lambda)

#

thats why it looks like
person.let { it.name() }

#

where .let is just a method taking a lambda as parameter

covert niche
pastel elk
#

person.let(lambda) so to speak

#

this is also what allows people to do fancy shit called DSL

#
html {
  div(id = "greeting") {
    span { "hello world" }
  }
}
#

so people can create methods that allow to write sth like that

#

its all just methods taking lambdas and whatnot

covert niche
pastel elk
#

u might have seen that for example arleady in java if uve worked with gradle

#

gradles build files often have a bit of that stuff

covert niche
#

Yes, i have seen those.

pastel elk
#

sth else that might look confusing at first is how kotlin wants u to write loops

covert niche
#

it has something like in 5...100 for the number of iterations.

pastel elk
#
repeat(10) {
  ...
}

for (i in 1..10) { ... }

for (i in 6 downTo 0 step 2) { ... }
covert niche
pastel elk
#

yeah, so what u have there is more syntax sugar that is actually just ordinary java/kotlin

covert niche
pastel elk
#

that .. is a magic function called rangeTo()

#

so u can for example make ur own as well

#

like, make a class, give it a rangeTo method

#

and then u can write foo..bar

#

it spits out a Range class

#

and that supports iteration via in

#

in is a shorthand for contains

#

so it all makes sense if u dig a bit

#

its just syntax sugar for normal java

covert niche
#

I feel like I am gonna be going to docs for revision very often lol.

#

I am good at forgetting syntaxes. Uff.

pastel elk
#

another key difference is that kotlin differentiates between mutable and immutable collections

#

List is immutable

#

MutableList is mutable

#

there are conversion methods between both

#

such as list.asMutable()

covert niche
#

ah, yes I have used them.

pastel elk
#

and that they use operators instead of methods, so persons += john

#

but they are ofc just methods under the hood

covert niche
#

Also, it's said to be null safe language i guess. where you don't get nullpointer like exceptions very often.

pastel elk
#

which u can also add to ur own classes, plus in this case

#

yeah so the thing is that kotlin differentiates between nullable types and non-nullable types

covert niche
#

ah, why does it remind me of operator overloading.

pastel elk
#
fun foo(p: Person) {

}
#

that p for example cant be null

pastel elk
covert niche
#

Oh i see lol

pastel elk
#

for a type that allows null, u need to write Person?

#

and kotlin gives u a lot of sugar to deal with these in a convenient way. it feels similar to javas Optional, just less verbose

covert niche
#

Oh yea, the ? has saved the day a lot of times.

pastel elk
#

example:

#
findPersonById(id)
  ?.let { it.name() }
  ?.let { println("hello $it") }
  ?: println("no one there")
#

?. means "call this method, but only if the object is not null"

#

?: means "if its null, do this"

covert niche
#

Ah, i see.

pastel elk
#

(latter is called the evlis operator)

#

and there is also "i expect this to not be null" person!!

#

its called the double-bang

covert niche
pastel elk
#

btw, kotlin has no ternary operator. they want u to write if-else

#

if (person.age > 18) foo() else bar()

covert niche
#

No issue, i am not accustomed to write ternary anw

pastel elk
#

since kotlin supports if-expressions this can be used in the same way as ternary:

var name = if (person.age > 18) person.name else "unknown"
#

there is another concept kotlin tends to use a lot and thats called infix functions

#

that .. is also using that

covert niche
pastel elk
#
class Person {
  infix fun greet(other: Person) ...
}

instead of now writing john.greet(bob) u can write john greet bob

#

thats what that .. uses

#

this is also very interesting for unary methods

#

so like

covert niche
#

I forgot what inline function was, i learned it last in c++, I know how to define it and all but i forgot the main task it did. It was similar to write the statements as is during compilation. for example if we define a while loop in an inline function, every iteration of the while loop will be written in the compiled code of the program and it'll execute while running.

pastel elk
#
class Kilogram {
  infix fun gram() ...
}

now u can write weight.gram, omitting that ()

#

together with another kotlin feature called extension functions this leads into more interesting stuff

covert niche
pastel elk
#

extension functions means u can add functions to existing classes

#
fun String.bla(other: String) { ... }

"hello".bla("world")
#

that together with infix functions allows kotlin to add stuff like

pastel elk
#
5.min
10.h
#

or for (t in 5.s..24.h)

#

if length < 5.meters

#

and so on

covert niche
pastel elk
#

kotlin also has destructoring-declarations. for example

for ((key, value) in map) { ... }
covert niche
pastel elk
#

its just custom methods u can add. and kotlin added some of them already for u

covert niche
#

I see.

pastel elk
#
infix fun Int.min() = Duration(5, TimeUnit.MINUTES)
#

it might for example look like that

#

so they extended Int, adding a min() method to it

#

and its infix so the () can be dropped

#

just a normal function, so to speak

#

after all the syntax sugar is gone

covert niche
#

wow. It'll be helpful indeed.

pastel elk
#

here is a quick example from some of my AoC code that shows a bit of the general convenience

fun String.toPos() = split(",").let { Pos(it[0].toInt(), it[1].toInt()) }

data class Pos(val x: Int, val y: Int) {
  infix fun distTo(other: Pos): Int {
    val xDiff = kotlin.math.abs(x - other.x)
    val yDiff = kotlin.math.abs(y - other.y)
    return kotlin.math.sqrt((xDiff * xDiff + yDiff * yDiff).toDouble()).toInt()
  }
}
#

example:

var pos1 = "12,8".toPos()
var pos2 = "18,12".toPos()

var dist = pos1 distTo pos2
#

in java u would have to do all that Integer.parseInt and u would have to write it in multiple lines bc u dont have that .let

#
static Pos toPos(String s) {
  String[] args = s.split(",");
  return new Pos(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
}
covert niche
#

Hmm Hmm, consice and beautiful code. I mean yea you do write beautful code.

pastel elk
#

that kinda gives u the idea how kotlin is supposed to be used

#

the code should just flow as-u-write

covert niche
#

Oh, i feel like my poet will emerge as I'll start writing good kotlin.

pastel elk
#

😄

covert niche
#

But yeah, a long way to go, I'll prolly be in #1051826284008853505 more often now haha.

pastel elk
#

what i also like is their built-in to method

covert niche
#

Oh well, the second half of my question, Kotlin is Kotlin whether you write for android or anything else right? No extra efforts needed?

pastel elk
#
var nameAndAge = "John" to 24
#

to is again just an infix method to make Pairs

covert niche
#

ohh, more like mapTo()

pastel elk
#

yeah

#

it spits out Pair<A, B> instances

#
fun isAccessible(x: Int, y: Int) = listOf(
  x - 1 to y + 1,
  x to y + 1,
  x + 1 to y + 1,

  x - 1 to y,
  x + 1 to y,

  x - 1 to y - 1,
  x to y - 1,
  x + 1 to y - 1,
).filter { isValidIndex(it.first, it.second) }
  .count { isPaperRoll(it.first, it.second) } < 4
covert niche
pastel elk
#

yeah its a Pair<String, Int>

#

it just comes really in handy if u want to group some things quickly

#

and ofc also to create maps

#

the above is an example of some AoC

#

where i had to check some neighboring tiles

#

so pairs of x and y coords

covert niche
#

Ohh,

pastel elk
#

yeah, kotlin is kotlin.
however, ull feel differences in practice depending on which underlying base for ur kotlin u use

#

the language stays the same in all cases though

#

but the default classes and methods available will change

#

for example kotlin/jdk can use all those java classes

#

like Instant, ArrayList, ...

#

kotlin/js has its own classes

#

kotlin/native has its own classes

#

and so on

#

but the language itself is the same in all cases

covert niche
#

I see.

pastel elk
#

and at least for the important stuff (datastructures etc) they have their own wrappers

#

so that will stay

covert niche
#

well, we can define our convenient classes as per our needs just gotta learn kotlin properly ig

covert niche
#

Well thanks zabu, it was a lot to take in at once but it unlocked a broad view on kotlin as a language and how not to write java in kotlin haha.

pastel elk
#

👌 the book helped me a lot. took me around 2 weeks to be comfortable with it and then 1-3 months of applied practice to fully feel at home

#

u might not agree with all their decisions ofc. but ultimately it feels like "java but more convenient"

lapis tusk
#

Just one thing to note @covert niche is that KMP does not expose the entire SDK so you'll notice some stuff might be missing because it's not supported. In those instances, you do have to work around it a little. A common one used to be Base64 until they added support later

#

Don't let that deviate your Kotlin learning but when you find that you can't import something that definitely exists in Kotlin, the reason will be because it wouldn't work cross platform

covert niche
#

Ohh, okay I'll keep that in mind.

#

I'll learn how to do it without that.