 mobile track and we are having a lot of talks today on Kotlin.net and we will be having a workshop on Kotlin as well. So you guys have seen my talk yesterday maybe and now I'm the moderator and he's going to be moderating today itself. So I'd like to welcome you all to this track and hope you'll learn a lot. I'm the moderator for this track and I'm the speaker coincidentally. So yeah so the first talk it's going to be on Kotlin. This is a quick show of hands. How many of you guys are Java developers or are familiar with Java? How many of you guys hate Java? None? Okay so presumably after today's session we'll start to hate Java and that's in a good way. That's me speaking in a good manner because I'll introduce you to something which is more interesting than Java. It's more fun than Java. It's better than Java so it's a better Java. So I'm Harshit. I'm an Android developer and I'm also an Android instructor. I'm from New Delhi, India and I work at a startup that's coding blocks. We train people to build better Android web and backend stacks. So yeah just sit down. I'm an Android developer that loves to make better and beautiful apps. So having said that Android Dev Stack is the biggest shit stack that's available right now because a lot of things messed up. The APIs are broken. There's no set pattern that you can follow because let's face it. Any Android Devs here? Oh awesome. So I'm pretty sure you guys are familiar with Dart right? Dart and Flutter. So Google themselves they're competing with their own framework. First of all they have Android native then they have Flutter. I'm not sure what's going on. There are a lot of other frameworks in the market. So there's nothing like there's no set standard for developers to create apps and yeah that's the issue. So these words were said by Steve Yek. He's an ex-Googleer who works at Grab right now and awesome guy, a great developer. So yeah so this is the current status of the whole Android framework. And a lot of these things apply to Java as well if you are writing server in Java or if you are writing desktop apps in Java. So I just go through them one by one. So first of all the whole ecosystem is very fragmented. So you have OEMs like Samsung, LG, Sony, Xiaomi, Huawei, terms more like they have their own implementation of Android. So like if you write an app that works on 10 devices but it definitely won't work on the 11th device. Okay because just because the manufacturer decided okay oh we don't want this API in and they throw it out. So like your entire app breaks and you get a lot of one star every single okay this app does not work on my phone. You're like sitting there scratching your heads like thinking why it's not working but the reason is because it's not your fault. It's fault of the manufacturer who made that oh yes okay for their devices. So yeah so like the entire system is very much fragmented and it's overly complicated right. Next up is we have a lot of boilerplate garbage. So like if this thing looks familiar to you writing anonymous in classes right like so this code like this is the code in Android that you have to write just to handle a single button click. So if I have a screen I want to handle a button click to write all these things. It does not make any sense. Okay next up is null pointers. So I'm pretty sure everyone is familiar with null pointers okay. So like the language like it's not very forgiving Java in terms of exceptions. So if you make any slightest error like it'll throw this like a well-known like the famous null pointer exception and your app will crash. So that's it and yeah so also there are too many languages and too less time. So if today someone wants to start his career as an Android developer. So he can code in all these languages like so Java C++, Kala, Groovy, Kotlin, Dart, C sharp, JavaScript and basic yeah that's true that's not like something I just put in. So you can write Android apps in basic. Okay just like just let that sink in okay and the thing is like there's a lot of confusion because like these are just the languages I haven't included the frameworks that code over react native right there's Xamarin this flutter that just came out. So it's very confusing for in someone who wants to start his career as an Android developer today okay. So what should we do and what should I do as an Android developer who just wanted to start out okay. So when I started out with Android two years ago like I didn't face this issue back then like I was living in happy Java land okay. So there was just Java as Xamarin was there but like it was not that famous it was not yet acquired by Microsoft. So like it was not like that famous react native was nothing nowhere to be seen okay. So it was Android framework was highly dominated by Java like so like that's where Kotlin enters. So any any one of you guys heard about Kotlin or use Kotlin? So yeah so what is Kotlin? Kotlin is a language obviously. So again one more code. So Kotlin is better than whatever term language you're using. I don't mean that to be an offense because I'm not the one who said these things right. Also Kotlin is sort of like it's too Java what C++ was to see and maybe even better. So I met a few guys yesterday they said that okay they hate C++ because it's overly complicated. So I'm not talking about them but in general like all all of us know that okay C++ was an enhancement to C. Like it reduced many things that are that were not present in C. So like that's what Kotlin aims at like doing to Java. So it like aims at fixing Java's issues and tries to bring a lot of more things on the table. So like again these lines were taken from a blog load by CBA okay awesome that. So so why Kotlin? It's concise. It's organized. It's testable. It's logical okay the language makes sense. It's innovative and like not enough time to figure out sorry like I just wrote these things yesterday night okay maybe I I could have used nice yeah okay maybe I could have used nice but I just wrote these things like yesterday and I just added this slide in at like one day yesterday so like I'm not sorry for this. So yeah moving in benefits of Kotlin so it's it works like Java it's it's safer than Java. We'll come to that why that's the case it's interoperable with Java. So you can have 10 files written in Java if you add an 11th file written in Kotlin that won't make your build your app will still work your server will still work okay it's practical why why that's the case we'll come to it. It's concise and readable okay like people argue that okay like this 15 line of code in Java I can write it in a single line using Groovy but that's not readable if code is not human readable it's not maintainable people cannot contribute to it and being from an open-source organization we want people who can contribute to our products. So we want to ensure that okay our code is understandable by others right also it's evolving quickly it has an awesome IDE support and lastly it's fun yeah like I love programming in Kotlin like the Kotlin gives me like the same feeling when I started out recording like when I first wrote my first homework program like I was amazed oh wow what's happening so Kotlin gives me the same feeling like I get to learn something new every day so it's it's fun it makes programming fun once again so yeah so yeah so there are no more null pointer exceptions I mean if you want you can have null pointer exceptions but the language itself tries to reduce the null pointers to as like as little as it can because like I'll you'll see the later I'll you'll see this in the later parts of the slides so when we were using Kotlin in our app the crash reports went down by 43 percent right next up is no need for unnecessary first blocks I'll come to that and smart smart costs are there so like they exclude the need for nested null checks null checks I'll come to all these things in a bit I'm just giving the overview we'll come to the coding part later on like it works like Java and it's safer than Java okay you can write code without it having to bite you in the ass next up is it's interoperable with Java many languages are interoperable operable with Java but they are not perfectly they're not in perfectly okay there are some kings in there which can equip it but this is the like this is one of the latest flawless interoperable in change of Java file to Kotlin without messing up with the remaining code base and it has zero effect on the runtime performance the only performance drop you'll see it's in compile time and that's like very minor maybe in milliseconds that you won't even notice because under the hood the Kotlin code is converted to Java but like it's like it's the development time that's reduced okay so yeah uh it's practical right so it allows for things that makes sense so in Java you cannot have multiple top level classes so if I have a Java file I can only have one root class that's it if I want to have more classes I'd have to make them inner classes right but that's not the case with Java okay you can have global variables in in Java like there there's no concept of global variable if you want something to be accessible from anywhere you have to like create a final static class instead of to create a static variables right I hope that makes sense right java guys I hope that makes sense so yeah like in Kotlin you can have global variables you can have variables that are like that you can call directly from any class from anywhere from any function next up there's extension functions we'll come to them later on there's type inference you don't have to specify the type okay for example you don't need to specify okay this is an integer it will infer the value it will infer the type from the value that's assigned to it if you write if you write where a equal to five it'll automatically infer okay so the variable a is of the type integer all right lastly there's string templating okay so string templating is not like is not like does not sound that awesome at first but once you get to know it once you start using it it's pretty awesome okay now so it's concise and readable okay it does not sacrifice safety for readability and it's the like when you are writing Kotlin code especially in android land you'll end up with 30 to 40 percent less java code right so like if you are creating adapters in Kotlin like the recycling of your adapters generally java takes me around like 70 lines of code but with Kotlin it's only 20 lines right so like that's one of the best parts of Kotlin so it's also continuously evolving so they have like a sort of a one month release cycle for every new build okay the like maybe even less and yeah so like they are not ashamed in using features available in other languages so they recently introduced coroutines which sort of like use the async await from C sharp and they're open for it like they're open about it they don't like they don't have like a very high ego that okay we won't use this part of the language because like okay we are a different language so they don't do that the developers over there they don't do that okay by the way just like just a heads up Kotlin is written by the people the awesome guys over at JetBrains so JetBrains are the folks that write ID's they write awesome ID's so no matter which language you are developing in you probably be using JetBrains ID so they have ID's for Java for Kotlin for C for C sharp I think so yeah for Swift and for JS also yeah the ID coming to that so yeah so so I like I don't even have to like memorize the code anymore like the IDE does that for me like I was one time like I was taking like I was like teaching kids on how to you can like how you can write anonymous in classes in Java like I was using Atom I was not using the IDE like I was I just stood there thinking okay what should come next because I was spoiled by the IDE right like because IDE does everything for me the moment I write A it completes everything for me I don't have to memorize code anymore and that cuts down the developer time a lot I'll trust me if you have to write a Java line okay an activity class in a text editor that will probably take you 15-20 minutes but with the IDE that just takes two minutes that's it so yeah so it's like a free and open source that's the best part about it and also like it has building code completion and debugger so like that's an icing on the top and yeah it is fun Kotlin's fun as I said so it's awesome so like that's me okay so like let's get to the code because like no one likes talk I hope you guys are liking this talk like generally we don't like talk we like to see code we like to see things in action so first of all let's get to classes in Kotlin so I hope you guys are familiar with classes right like any one of you guys who don't know what classes are not your traditional classes but Java classes or maybe C++ classes everyone is good awesome so this is a typical Java class okay and this is a Kotlin class so like in Java class you have to create getters, setup constructors like all these things but in Kotlin the language does all these things for you so you can define a class in just a single line so this was like I minified all these things I minimize all these things but this code was around 75 lines long but in Kotlin it's just a single line now this one line and you are done okay so that's one thing so this was the part that like like that was the like moment for me when I was like looking out for Kotlin so like I just okay what's happening where are the getters at this where is the constructor nothing just need to like define the class and you are all done you are good to go okay also one more thing you can have multiple root classes so you can see like there's a packaging over here oh by the way no semicolons like you can skip the semicolons you don't need the semicolons so you have the like you have three classes in a single Kotlin file you cannot do this with Java if you want to like have multiple classes how to create the teacher class inside the student class need to have the inner classes but in Kotlin you can have multiple root classes okay there's like this this this will totally work you won't have any issues with this so whenever we are writing pojos like the Java objects you can like like like show us all your pojos in a single file like teacher, student all those things you can write them in a single file okay that will reduce the number of like number the number of files you have okay it'll make your code less clutter okay so button handling in Kotlin this is like this is a this will be very similar to the android guys so in Java you have to like do something like this you have to create an anonymous inner class and then you have to handle the click inside this anonymous class but with Kotlin you have lambdas out of the box so like just write this and you're good to go you don't have to write even this part so this is like just a hint that's given by the IDE so like skip this part just this button not set on click listener you're good to go whereas in Java you have to write you have to implement an anonymous inner class inside and then you are like able to handle clicks inside inside the on click method okay so that's the difference this will be very familiar to the android guys so think java 8 can do this like this thing is available in java 8 okay but then you have to sacrifice the number of devices that you are supporting because not every device runs on java 8 like that not every device has built in java 8 support not every android device right so if you are you doing if you're trying to do something like this in java you have to like sacrifice the number of devices you are going to support all right so null checks in Kotlin so in java you have to write this okay so if button no this is not equal to sign like okay so like this is a font that i use it automatically does this in i in delige idea but this is not equal to sign so in java you have to do something like this so if button is not equal to null then set text to the button if i want to do this thing in Kotlin you can do something like this button question mark dot text equal to change the text so what this will do is if button is null it will skip this line otherwise it will set the text to change the text okay it's intuitive right because like if the button is null i don't want to touch anything right because if the button is null and i try to modify anything on to it i'll get a null counter exception true right so if the button is null skip this skip this line otherwise set the text to this thing so no need for multiple nested if else if else if else you can simply use the question mark dot operator okay and good to go next is string templating this is what i was talking about so in java let's say we have two variables two integers num and come want to write a string that says okay button dot set text the count of number num is count in java we do it like this okay we append the integers to a string and in like internally it uses string builders okay to save the memory and all but in Kotlin you can do something like this so this is available in many of the languages out of the box but it's not present in java so using the dollar sign does it only work for values because you're using values instead of variables no it works for variables as well you can change it to where and it'll work also you can write if statements you can like write logical statement inside you can write like you can do curly brace and write if something then the value of num should be this else the value of the num should be this so you can write logical expressions also if you want like you can also like you can after dollar sign you can use a curly brace and write logical statement inside that will work okay so this is this was like this was very awesome as well because like it makes your code more beautiful it makes it understandable right because rather than having multiple plus signs you can simply use dollar and do all the things that you want global function and variables so okay so venturing where no java developer has gone before so outside the classes so this is my class okay and now what i'm doing is outside my class i have a variable and a function in java this will throw you a compilation of it but in cotton it works so like this api key variable and this global funk function i can access these two things from anywhere no matter what package i'm in no matter what class i'm in i can access these things from anywhere that i want okay and like the one reason i told you that cotton was fun because we define functions with the word fun okay yeah you can simply call api key and it work simply just go ahead like go into any java file just write in a capital a and you'll like see this auto suggestion this will work okay so this is like one more thing so internally what it does is internally it will like place this like when you compile the code it will create a final class it will place these variables inside the final class and then internally it will access them via that final class but like development time already is reduced right i don't have to create a final class anymore i don't have to create static variables or static functions anymore correct so yeah this is that this is not present in java also async tasks how many of you guys know about async tasks so async task is something that we use we don't generally use async tasks but okay like if you're a newcomer in android world you will be using async task to perform any async in a score like for example what is an asynchronous code asynchronous code is a code that i like want to run in the background for example i'm writing something to a file right so i don't want that thing to run on the main thread because if like because file writing will take let's say five to six seconds and if i do that on the main thread then my apple hang because like user cannot do anything else on the screen because in in that screen there's a file right happening so everything else is blocked so that's why we use asynchronous task asynchronous task or the task that run asynchronously in the background so in android a very common pattern to do async asynchronous work is using async tasks okay as the name suggests that async task asynchronous task so if you write async task and run it that runs on the background okay and your main thread or your ui thread or your foreground thread is free to perform any other things that you want so in android in java you have to create an async task like this this is very like this is very simpler version of this like if you like try to do more things here it will result in like around 150 lines of code but in kotlin just this this is like not done like this is not present in the language out of the box but there's a library called anko that's why i mentioned over here done using anko so anko is another library okay that's created by jet brings the guys who built the kotlin language and like it's a library specifically aimed if you are doing android development so kotlin can be used to do a lot of things you can write web apps in kotlin you can write backend servers in kotlin you can write android apps in kotlin so if you're writing android app in kotlin then this library anko this helps a lot okay so like if you're an android developer and you want to use kotlin like feel free to include this library in it will help you a lot so using anko you can do this in just two lines and there are a lot of memory issues so like this async task has a lot of memory leak issues okay like i won't go into the very depth talker because like it might not make sense to a lot of guys over here but like the android guys who use async task i believe they know right if you rotate the screen and you still have the access to the activity context here it leaks right you get a lot of like memory leak warnings and your app like keeps on like a eating up memory and your phone becomes slow and like all those things but that won't happen here right if you have like if you rotate your screen and the context that you have is null it is simply ignored it won't run anything yeah also singletons there's no what singletons are so singleton is something okay so let's say i want access to a class class object but i want like what i want is at a time only one instance of that class should be available let's say uh how many android have seen android users android users android users guys come on android users okay good so you guys have a notice you you guys can see notifications right notifications like you hold on you see notifications right so you display notifications using a notification manager okay i'm like giving a brief overview or on how singletons are using android okay so you display notifications using the notification manager so at a time so like there's only one instance of notification manager that's available after phone booths and all the apps use that same instance to display the notifications okay so that's implemented using a singleton pattern right so that only a single instance of your notification manager object will be accessible so no app can create a new instance of the notification object okay i cannot create a new object of the notification manager class i cannot do that because if i try to do that the voice will return me the existing instance that's what it will do so in java if i want so that's called a single that object is called a singleton object right because it's a singleton okay i'm not sure what ten means but single means that so yeah so this is how you implement singleton if you look at the logic it's very simple so like when i try to get an instance of that singleton object if it checks if the instance is null then it creates an instance and then returns it otherwise it simply returns the instance okay if this does not make sense no issues totally fine but in kotlin you just have to write the keyword object that is it if you write object singleton then anytime you create a new instance of this you'll be returned an existing instance if it's present so you don't need to have all these checks all these static variables you don't need to do that this will automatically handle it for you the object thing so like that's one more thing that's very good in kotlin that's unique in kotlin and there are much more things right there are extension functions functions so like if you want to create a new method inside a class okay but you don't want to extend that class and create a new class of that class right you can use extension functions so what i can do is i can go ahead and create a function in the math class in java that's called let's say quadruple that takes in a number and returns the quadruple of it so i don't need to extend the math class and create a new math class i can simply use extension functions can directly create a method that i can call on any math class okay so you'll see when you try to do the extension functions okay there are higher order functions so like it's like javascript but better we have like map flat map all those operators available in the box and lastly there's coroutines so this is a new addition to kotlin coroutines are like threads but on steroids so like there was a talk this time at kotlin conf so that guy like the speaker he spawned over one million threads and one million coroutines so the threads that like he spawned one million threads and that hung up the system but the coroutines they just like ran the ran the entire operation in just five seconds so like this is a this uses thread pool to like perform all the operations they only have six threads at a time okay i'm getting too technical but yeah like this is this is a better variant of thread you guys know about threads right threads in java if you want to do multi-threaded programming like we create a new thread right so that's that yeah so like there are a lot of things in kotlin i believe like the next speaker is speaking on kotlin native so i'm pretty sure he'll introduce a lot of these topics as well you have five minutes left for sure so like okay this this was me when i was like doing java and this is me when i'm doing kotlin right so yeah feels good yeah so lastly so uh so lastly about my project so this is open event android this is a false asia project okay uh this project allows you to build an android app for your event without writing a single line of java code or kotlin code so yeah you just go ahead submit us a link of your event and you click on the generate button and you have it we have the app for that event you can upload it to google play store you can distribute it amongst users all you can do and we recently shifted to kotlin not entirely but we are in the process of it and uh some interesting statistics using kotlin has led to the let's led to an increase in the number of lines of code we have a less number of crashes and we have reduced development and code review time by huge margin because naturally you write less code you have to review less code makes sense so yeah so we have uh 7000 less lines of code now code base so far and we are not even like done with the entire kotlin conversion we have 43 percent degrees in the number of reported crashes mainly the null pointer exceptions okay and we have 50 percent degrees in development time okay at least that's for me because i know kotlin like guys who don't know kotlin they are probably they are facing an increase in the review time because they have to go through the code and make sense of it okay what's happening here but for me there's a 50 percent decrease okay so yeah so like okay uh so if you saw if you like what you just saw the kotlin thing and the open event android thing uh we need you guys okay we need more developers who can help us out in making this app better and yeah if you feel if you like joining us feel free to do that uh we have the GitHub repo uh i think i have that in my next slide okay i don't know any so yeah i'll be sure to like leave a link to it on this white board and yeah so like feel free to join us to interview uh we have a lot of kotlin conversion that's left uh we have a lot of data classes or uh java classes that are left like you can go ahead and start converting them into kotlin we have a lot of utility methods you can start converting them into kotlin and yeah like we'll be more than happy to have you guys on board so this is me uh you can find one twitter and i often write some medium articles on kotlin recently jumped into flutter so i'm writing articles on flutter as well you can find one medium over here and yeah so any questions guys that's it on my side thank you any questions any questions i'll be around if you guys i have a question so i'm a jays developer but it inspired me to learn kotlin as well right so uh but uh can you suggest someone who does not know java uh to directly start with kotlin why not i mean you just won't be able to appreciate the language but yeah you you'll be able to work in it because like uh being a java developer i know the like i know the pain that kotlin tries to solve so i can relate to the things that's that are happening there if you start if you jump right into kotlin that's totally fine you can do that but you won't be able to like find the language like so fun or so interesting because like i know okay okay this thing breaks in java but this thing wasn't important but you never try java you won't be able to appreciate the language but still i'm not suggesting that okay don't jump into kotlin right away but yeah do jump into kotlin it's a fun language it's a very good language okay uh so uh for example the ios in obelisk the ships like it used to be like everybody is using obelisk the ship and now everybody is shifting to the switch and the switch is the base card right here i'm playing one two three four and in case of that they break the uh they're after right yeah do you think kotlin has been around since uh like this was first introduced in 2007 it's been 11 years and it's still working so i i i've been using kotlin in the past eight years uh so eight months eight months not years so like i didn't raise any like backward compatible issues right if i'm on beta versions definitely yes things break but on the stable release stable channel nothing breaks everything works flawless as i mentioned like swift breaks because they're after okay it's not open source is it open source is it open source okay okay but good but since they're after right so they break things kotlin yeah so like nothing will break don't worry about it any other questions yeah um if i have an egg right at written in java uh should i call the part by part of the company or i should start first conversion uh converted part by like like start like okay don't start from scratch don't start from scratch because you can convert the existing code base from java to kotlin you can do that okay go ahead and find a simple pojo class like java class that i just showed you so this is java class like you might be having many java classes like this start by converting this into kotlin because the syntax is very easy right i'm not telling you to write idiomatic kotlin like don't don't write idiomatic kotlin just start with very simple kotlin start with this and uh like just like keep converting the code code gradually right don't like start anything from scratch okay uh if i'm using a lot of live with the party live so do you face any issue like nothing written in kotlin there's any java code will work with kotlin no issues no issues over there so any library like even if it was written six years ago it'll work with kotlin don't worry what's the minimum jdk version i can use uh kotlin with android uh i'm calling from java web okay like i think uh i'm not sure about the minimum version but i think like the i think jdk six uh i think this i tried it on devices running six and work so like i'm not sure about the minimum version sorry for because i'm working on i mean working mainly on maintaining web apps can i use uh if i were to work with kotlin can i mix in my application using java classes and Kotlin classes why not no problem you have my you have my you have my guarantee you have my word on that okay uh thank you so much okay you have a okay um so i'm guessing um since the get on set are kind of throat i can still override you can do that um is there some like see override annotation or it's it's like it's a name type you can do that like uh i don't have the code snippet right now but i'll uh because we can chat after this talk like you i can come back there and i can show you you can definitely do that please you can also like you can create get a set over here you can do that you have the option to create a gator method over here yeah because i'm asking uh if you see compiler work is there some way for the compiler to enforce them overwriting and existing property you can do that yes that's possible okay okay thank you guys thank you very much so much for being an awesome audience