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.
#Start learning Kotlin for Kotlin Multi-Platform.
1 messages · Page 1 of 1 (latest)
<@&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>.
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:
-
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. -
Practice writing idiomatic Kotlin:
Convert simple Java classes to Kotlin by hand. This helps internalize differences rather than relying on automatic converters. -
Get comfortable with Kotlin tooling:
Use IntelliJ IDEA or Android Studio. Explore their Kotlin-specific inspections and quick fixes. -
Explore Kotlin for Android:
Once fundamentals are solid, start learning Android-specific Kotlin extensions, Jetpack Compose basics, and Android coroutines usage. -
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. -
Follow tutorials and build sample projects:
Practical projects that combine Kotlin basics with Android and KMP concepts solidify your skillset.
Useful links:
- AI generated response using gpt-4.1-mini model
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
Yes, that's my preferred way of learning to as it clarifies a lot. I have learned java in the same way where authors often point out why this thing isn't good in c/c++ and we changed it on java and how well it works this way. Thanks I'll go through the book.
the majority of features in kotlin feel like convenience-sugar
so they are easy to learn
ah, i see. Well, i have seen and used it some times but never dig in what and why it is. Guess time to learn it all now haha.
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)
I see. any examples?
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() }
Oh yeah, Flows have a lot of similar methods to stream but the main different is that Flows have a coroutine context right?
u can also ommit return iin many cases because the last line of a .let{...} and similar is implicitly returned
so for example
Oh yeah, convinient.
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
wow, this looks sick.
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
I see.
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
Yes, i have seen those.
sth else that might look confusing at first is how kotlin wants u to write loops
it has something like in 5...100 for the number of iterations.
repeat(10) {
...
}
for (i in 1..10) { ... }
for (i in 6 downTo 0 step 2) { ... }
this will have 94 iterations ig
yeah, so what u have there is more syntax sugar that is actually just ordinary java/kotlin
ahh, yes.
I feel like it may lead to errors if not done properly. I wouldn't say Kotlin is as beginner friendly as java.
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
I feel like I am gonna be going to docs for revision very often lol.
I am good at forgetting syntaxes. Uff.
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()
ah, yes I have used them.
and that they use operators instead of methods, so persons += john
but they are ofc just methods under the hood
Also, it's said to be null safe language i guess. where you don't get nullpointer like exceptions very often.
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
ah, why does it remind me of operator overloading.
bc it is 😄
Oh i see lol
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
Oh yea, the ? has saved the day a lot of times.
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"
Ah, i see.
(latter is called the evlis operator)
and there is also "i expect this to not be null" person!!
its called the double-bang
Yes i have used it!
btw, kotlin has no ternary operator. they want u to write if-else
if (person.age > 18) foo() else bar()
No issue, i am not accustomed to write ternary anw
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
yeah it's convinient enough.
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
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.
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
that's why we don't write loops in inline function.
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
looks weird.
aaha! yess that was something cool as you can skip the class name, not sure about the criteria where you can do that.
kotlin also has destructoring-declarations. for example
for ((key, value) in map) { ... }
min, h, meters works how it works in real world? kotlin will understand that 5.min like how we understand it?
its just custom methods u can add. and kotlin added some of them already for u
I see.
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
wow. It'll be helpful indeed.
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]));
}
Hmm Hmm, consice and beautiful code. I mean yea you do write beautful code.
that kinda gives u the idea how kotlin is supposed to be used
the code should just flow as-u-write
😄
But yeah, a long way to go, I'll prolly be in #1051826284008853505 more often now haha.
what i also like is their built-in to method
Oh well, the second half of my question, Kotlin is Kotlin whether you write for android or anything else right? No extra efforts needed?
ohh, more like mapTo()
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
so this nameAndAge will become a key value pair type variable rather than a string or integer.
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
Ohh,
?
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
I see.
and at least for the important stuff (datastructures etc) they have their own wrappers
so that will stay
well, we can define our convenient classes as per our needs just gotta learn kotlin properly ig
Oh ok.
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.
👌 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"
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
