We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

What I Miss In Java (The Perspectives Of A Kotlin Developer)

00:00

Formal Metadata

Title
What I Miss In Java (The Perspectives Of A Kotlin Developer)
Title of Series
Number of Parts
542
Author
License
CC Attribution 2.0 Belgium:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor.
Identifiers
Publisher
Release Date
Language

Content Metadata

Subject Area
Genre
Abstract
Java has been my bread and butter for almost two decades. Several years ago, I started to learn Kotlin; I never regretted it. Though Kotlin compiles to JVM bytecode, I sometimes have to write Java again. Every time I do, I cannot stop pondering why my code doesn’t look as nice as in Kotlin. I miss some features that would improve my code’s readability, expressiveness, and maintainability. This talk is not meant to bash Java but to list some features I’d like to find in Java.
14
15
43
87
Thumbnail
26:29
146
Thumbnail
18:05
199
207
Thumbnail
22:17
264
278
Thumbnail
30:52
293
Thumbnail
15:53
341
Thumbnail
31:01
354
359
410
Software developerGateway (telecommunications)Java appletSoftware developerJava appletLevel (video gaming)Android (robot)Formal languageConnectivity (graph theory)Multiplication signServer (computing)Insertion lossPublic key certificateFront and back endsCodeComputer animation
Object (grammar)Social classJava appletSoftware frameworkSpring (hydrology)String (computer science)Formal languageJava appletRow (database)Multiplication signSocial classLibrary (computing)Goodness of fitParameter (computer programming)Object (grammar)Default (computer science)Error messageCoprocessorType theoryWritingReading (process)Compilation albumImplementation2 (number)Computer animation
String (computer science)Operator (mathematics)Interior (topology)Element (mathematics)Glass floatRepresentation (politics)Object (grammar)NumberType theoryFunctional (mathematics)Library (computing)WritingSystem callFormal languageRun time (program lifecycle phase)CompilerSheaf (mathematics)Extension (kinesiology)Pointer (computer programming)Computer animation
Extension (kinesiology)Social classSpacetimeSocial classDivisorFormal languageUtility softwareExecution unitLibrary (computing)Java appletObject (grammar)Software developer1 (number)Computer animation
Extension (kinesiology)Social classSpacetimeString (computer science)Price indexDesign by contractNormed vector spaceMeta elementFluid staticsSocial classFluid staticsString (computer science)Computer animation
String (computer science)BytecodeDot productLevel (video gaming)Functional (mathematics)Constructor (object-oriented programming)Social classType theoryState of matterSoftware developerObject (grammar)CodeRootFormal languageDefault (computer science)Java appletString (computer science)Computer animation
Generic programmingData typeGeneric programmingRight angleJava appletMultiplication signComputer animation
Generic programmingData typeInterface (computing)Demo (music)String (computer science)Wide area networkExecution unitTwin primeMultiplication signType theoryFunctional (mathematics)Electronic mailing listRight angleLine (geometry)CodeSocial classElectronic signatureBytecodeOvalProduct (business)WritingBitBootingFluid staticsLambda calculusJava appletContext awarenessExecution unitSpring (hydrology)Parameter (computer programming)CompilerString (computer science)Video gameComplete metric spaceResolvent formalismImplementationGeneric programmingComputer animation
TwitterProcess (computing)Multiplication signComputer animation
Program flowchart
Transcript: English(auto-generated)
Thanks to be here. Thanks for missing your lunch and let me rant about Kotlin, I'm Nicola Frankel. I've been a developer for like now more than 20 years. Perhaps. I'm not a developer anymore I'm a developer advocate, but I still see myself as a developer. I love developing right now
I work on the Apache API 6 Gateway, it has nothing to do with Java. It's just an infrastructure component But since they allow me to be here, here is the slide. Anyway, Why am I talking about Kotlin in a Java room? Well, they didn't want me to talk about Kotlin in the Kotlin room
So that might be one of the reasons and also I've mentioned I have like two decades of coding experience and Those two decades were spent in Java. So I started with Java
1.3, 1.4, 1.3 perhaps and well there were like big wins and small losses and I tried Scala and I didn't like it at all. I still have the certificate Saying hey, you are like Scala certified
I didn't like it for multiple reasons. Then I went to a couple of conferences I had a friend and she made also like talks about Kotlin and I said we don't need Kotlin we have Java, it's enough. Then I Wanted to teach myself Android developments and
Android developments at the time was only in Java and the API was super low level and I said no I cannot write such like codes that it brings me back 15 years ago when I was writing Java on the server side and I looked for solutions and I found Kotlin And I thought wow, that's cool
And I stopped learning androids and now I write Kotlin on the backend so just a disclaimer If Java is the best language in the world for you just leave the room I'm not trying to bash Java, but depending on your culture, I'm pretty straightforward. You might feel offended
So better leave now, it's up to you Okay, let's start slow immutable references. I'm telling you. Hey It's better in Kotlin and you can tell me hey Java has it, right? immutable references Okay, let's try to check how it works
immutable references in Java fun stuff Immutable references in Java So Here this is my immutable reference class so I have To put final here. I have to put final here and I have to put final here now
I have immutable references Who puts final everywhere? Couple of people No, really We we add this this idea 15 years ago, and then we decided no, it just makes this like the reading so much harder
So perhaps not even worse like if you don't put final on the parameter you can actually relocate stuff Which I believe is one of the worst thing you can do like really really really bad So I'm not saying that
Java doesn't have it, but I'm saying that in Kotlin it's From the beginning you need to decide whether it's a vowel Which means it cannot be reassigned or it's a vowel which means it can be reassigned if you are using IntelliJ I don't know about Eclipse or NetBeans. The good thing is that If I say it's a vowel so it can be reassigned. It tells me hey like
There is something fishy. It's not an error per C, but at least it's visually like pleasing And of course by default Yeah for who's who don't know any is the equivalent of object. So it's not very important
I could write object by default. You cannot reassign parameters any language that allows you to reassign parameters Should be taken with like at most caution It's actually not a great design ID So, of course Java was designed a long time ago doesn't have it
But Kotlin takes the lesson and if you think that Val and Vore are coming from Scala because you are a Scala fanboy That's completely true. Kotlin has stolen every good ID So don't pretend otherwise, that's fine. Okay Seconds immutable classes. Well, that's fine. Now we have Java records. We've got them. That's fine. Let's continue now safety
Now safety in Java is not that fun, right? How many ways do you have to implement null safeties in Java?
yeah, that's a good diversity is a good thing because we work in IT and if you if you want to really Like a fun you might check like nullable nullable nullable. No, no, no, no, no, no, no, no Well, I'm sorry, sorry
Yeah, exactly, I'm not sure it's an error I think I just copy pasted so I'm not sure yeah That's really fun stuff. And of course, they won't work with one another. So you needs to have the processor Sorry up You need to have the compile time processor and you need to choose which library you will be using and then you hope that somehow
It will work Good in Kotlin, what do we have? Well, it's baked into language So basically here I was too lazy to write it in Java But basically if you write something in Kotlin, you have additional types. So basically for every like
normal type This is a non nullable type and this says it's a nullable type It means that if you are calling something on A non nullable type you can call whatever you want
Plus whatever and If you call something on the nullable type Kotlin will say oh it was saying something and now it doesn't say it Yes, because plus plus is smart
plus knows how to operate on like nullable types But let's do something that it's not safe Which is it's also very smart The library is too smart for me
Yeah empty that and We've reversed Yeah, finally things And it tells you hey, you know, this might be nullable. So please don't call it like this
Because there is a chance you might encounter a new pointer at at runtime and yeah, you should take care and well Afterwards, it's quite easy. You can do okay this stuff I mean if you have been doing groovy or I think Scala does it to the it's But the compiler tells you you should be careful about this
So and it's again it's baked into the language So like for every type There are like two real types one that might be nullable. The other might not be nullable the good thing we've I will show you after where a section function you can write extension function that work on nullable type, which is really really crazy
Good so second stuff better The utils classes Who has not written a single unit utils classes in their life?
Nobody so even people younger than 30 have written them In general there might be a divide because like yeah all the developers they have written them the younger ones They are smart enough to use the library But the thing is well at our age. There was no library
So so basically we say that Java is is a lot in the object oriented language And then we put everything in a class. We put static methods in the class and we pretend that it's object oriented Right
Well, let's not pretend it's object oriented if it's no object a static class Class with static methods is not object oriented. So here I have created my my amazing String utils class and of course, I need to remember because the users of my class Well, they might instantiate it
So I will just remove the constructor make it private again Very object oriented and then I create these Capitalized stuff and I do whatever I want and then I can call the capitalized method Good Scala and Kotlin have I think like center stuff
Let's not pretend We can we are An object oriented language we can just add methods and states but mostly methods On existing classes
That's crazy Yeah, of course and the bytecode level it boils down to a static method That's not the problem. The problem is the user experience is a developer experience We can see now that we are really doing Here, we actually are doing object oriented development. So through this functional stuff
Because here we create a function That is at the root level We are able to write better object oriented code Which is mind-blowing even better
As I mentioned we can say hey this only applies so here we can have vor F of Type string might be null. So it's this is a nullable type And here we cannot say F dot capitalize
Because it's it's now right It's a nullable type here. It only applies to real strings, but we can do something like this we can say hey, it might be a nullable type and we can check if this Equals null
Then we return the default which might be an empty string Yeah, I see like what the hell? That And in the end, that's a static method But how you call it is like object oriented which in my opinion makes like costing
Much more object oriented than Java will ever be well will ever be no, I'm not sure Let's see what we have in the future. So that's already good stuff and then Oof Ray-fied generics, right
Who has been bitten by the lack of ray-fied generics already? Yeah, so I have a collection of thingy and a collection of foo and well at one time you have nothing So here the trick when you do Java
Is to pass the class so here is taken from spring when you get a bean you say hey I want a bean of class whatever and then you will get the whatever Okay, how can we do it in Kotlin?
Kotlin has this ray-fied T So when you call a get bean you can pass The type that you want and It will get you that it's a string if we are a bit
Like tricky we can do this and it's a list of string And if you tell it because it's all about compiling because in the end, of course, the byte code is still the same
The byte code must be compatible with Java byte code. There is no ray-fied generic in the byte code So it's just about compiling and here we can say Either we set it here in the signature when we call it or we tell that type Of that S is of type string and we will get a string and I think that's pretty amazing
The only thing that we need to do Is do this We need to tell it's ray-fied Sorry
Again we need to tell it's ray-fied and for ray-fied you need to have inline Why because as I mentioned there is no trick At compile time it will actually replace the code It won't be a call. It will be just copy pasted and so it knows which type you are and in the end
So I still have a bit of time we can do like really really fun stuff I will Yes fun stuff
okay, I will create a Function beans and For the moment, I will return any okay, so some syntactic sugar. I don't think it's really important I don't think it makes me want to use Kotlin. It's just easier to use it like
Either you return the type like this Here any idiots can understand it returns any
So the Kotlin compiler is not an idiot, but the Java compiler is On one-liner, it doesn't make any sense to specify the type explicitly every time but still the Java compiler requires you to do it Makes no sense
Okay Sure sure if if for whatever reason if for whatever reason you want to specify what it is because it might be complex But then if it's complex, perhaps it doesn't belong to one-liner then you can still are a lot to Especially if you are using if you return a concrete implementation and you want your signature to be an interface. That's perfect
Okay And now I will have a class which I will call Let's say beans DSL, okay there I want this to return
Beans DSL Okay, so now I can write something like this. I can have a main function main Main, okay private static void name. Okay, and I can call the beans function beans function great
I have nothing mind-blowing Now what I can do is I want to write something like this Okay, so I want to write something like this. I will just use the compiler Okay, so I create
Here I accept a parameter that takes nothing and returns units Here what I can do is normally I would write something like this But in Kotlin if the last argument is a lambda you can move it outside the parentheses
So that's what I did before and then here if there is no arguments and there is a lambda I can remove the parentheses Good Now I can add The bean method inside and I can say that I can actually
Call the bean method on the bean DSL. So here I can do something like this and What is it telling me and resolve method beans, so I still have an issue
Yes up little trick Yeah, sorry, that's a life coding completely. I thought I would be less fast. Okay, and
Methods is generic, right so we can say it's of it accepts the type T and I don't remember how it's written. So I will be doing my stupid stuff
line functions or here in line fun raffite T Here and it returns it and if you continue like this you can have
this kind of stuff So here this is the spring boots Kotlin DSL so you say this will create beans each been you can define either through a lambda or directly and Through the refined stuff
here you see the product handler actually requires two dependencies and At compile time it knows that it requires. I don't know a foo and a bar So because those ref method are raffite For generics it knows it needs to care the ref to call the ref method
Calling getting a foo and a bar and it will inject them so here you have the magic at compile time and at runtime, it's the usual spring boot stuff and I believe it makes my Code much easier to read. Of course, you need to understand the trick
Every time you need to be very explicit in the beginning because you like a lot of context when you have the context Then it makes your stuff much much easier and That's all I don't want to bore you with more details You can follow me on Twitter You can follow me on masterdom because well, you don't know what will happen to Twitter and the other talk was not about Apache
API 6 you can Check Apache API 6 which makes my job so much easier and I can come back here to talk about unrelated stuff Is there any time for questions there is