Placeholder Image

Subtitles section Play video

  • ROMAIN GUY: Good morning, everyone.

  • [APPLAUSE]

  • Wait, don't cheer us.

  • Cheer the next speakers.

  • So I'm Romain Guy, representing the Platform team on Android,

  • and this is Tor Norbye representing the Tools team.

  • But you're not here for us.

  • You are here for very special speakers.

  • TOR NORBYE: Yeah, so Romain and I

  • have been incredibly excited about this official

  • announcement of Kotlin.

  • We both love Kotlin, in case you hadn't noticed already.

  • And we don't take any more time from the speakers of honor.

  • So without further ado, here they are.

  • [APPLAUSE]

  • HADI HARIRI: Hello, everyone.

  • Aw, that's nice.

  • So I was thinking the other day, the first time that I ever

  • did a talk on Kotlin, it was like 4 and 1/2 years ago.

  • And they gave me a room for 900 people and 7 people showed up.

  • So it's kind of nice to see more of you show up today.

  • Thank you.

  • [APPLAUSE]

  • Anyway I've got roughly around 30 minutes.

  • Actually, a little bit less.

  • 28 minutes to give you a whirlwind tour of Kotlin,

  • so I'm not going to cover the entire language.

  • I'm going to try show you as much as I possibly can

  • and how you can use it and where it provides you

  • with some benefits, OK?

  • So I'm going to try and do it all with live coding,

  • so if it all goes terribly wrong, there's a Google video--

  • I mean there's a YouTube video of this somewhere as well.

  • So you can watch that.

  • OK, so we're going to start with something very simple

  • that you've already seen, data class.

  • And I'm in a file called main.

  • So data class, I'm going to create a new type called money,

  • and it's going to happen amounts which is of type Int.

  • I know, don't say anything for now.

  • And it's going to have a currency of type string.

  • OK?

  • And this is both properties, and they're

  • going to be read only properties.

  • So this is something that you're already familiar with.

  • It's essentially a data class.

  • Let's go ahead and compare that to a Java one

  • that I have done earlier.

  • So split vertically.

  • And let's get the Java money up-- actually,

  • let's go down this side.

  • Java money-- so there you go.

  • That's kind of the equivalent of what I've just written, right.

  • A JavaBean essentially, a data class

  • that provides two getters.

  • They're immutable, so they're read only.

  • And this data modifier over here, what that's doing

  • is essentially giving me a whole bunch of things,

  • like the to string, it's giving me the clone,

  • which in Kotlin it's called copy, it's doing the equals,

  • it's doing the hashCode.

  • Now you say, OK, well that's great.

  • Brilliant, but why do I need that?

  • Because any good IDE is going to generate that for me.

  • It is true, it does.

  • But the problem is that that's code you're

  • going to have to maintain.

  • And every time you add a new property,

  • you're going to have to go back and change that code.

  • And the other issue that you're going to have is you

  • don't know, in fact, if that code is

  • the standard stuff that Kotl--

  • that IDE has generated for you, or you've

  • tweaked it a little bit.

  • OK?

  • So it's not just about saving on typing on the first line.

  • Right.

  • Now what I'm going to do is go ahead

  • and create a function main.

  • Let's close this guy over here.

  • And this is the top entry point of Kotlin,

  • so you have a public static void main in a class.

  • You don't need that in Kotlin.

  • So in Kotlin, you can put everything in the top level.

  • It's kind of like JavaScript in a good way.

  • And so you don't have to--

  • and I want to reiterate, every function and everything

  • that I'll show you today, I'm going to just add it

  • in the file as a top level thing,

  • but that doesn't mean that you don't have any more member

  • functions.

  • Anything, any class, even a data class

  • can have member functions.

  • But for brevity and for the purpose

  • of what I'm showing you, I'm just

  • going to put it as top level, OK?

  • So I'm going to create a new instance of money.

  • We're going to call it, for example, tickets.

  • And I'll say Money 100, and it's going to be dollars, right?

  • And I'll get into the explicit type here,

  • but essentially here what I'm doing is type inference.

  • So Kotlin is very strong in type inference,

  • and as much as it can infer, it will for you.

  • So you don't have to explicitly tell the type.

  • And then I'll say, for example, popcorn.

  • Let's say tickets, copy, right?

  • So what that's going to do is basically

  • copy the previous one for me, and all of the properties.

  • So if I don't pass any parameters in,

  • it's going to just take the same values as it has before, OK.

  • So I can pass in a new parameter and say,

  • you know, in Spain, for example, popcorn

  • is seven times the price of the entry of the cinema,

  • so that's going to be like 500 euros.

  • Right?

  • And now I can do things like, for instance,

  • if tickets is not equal to popcorn,

  • then println "they are different!"

  • So what this is doing is a property comparison one by one.

  • It is not doing a point of comparison.

  • For point of comparisons, we have the triple equal, right?

  • Different to JavaScript, there isn't

  • a chart of 600 different positions you have to remember.

  • And this, by the way, is font ligature,

  • so don't get confused with that, we

  • didn't introduce a new symbol.

  • OK.

  • So I can go ahead and just run this,

  • and I get they are different.

  • And if I change this to 100 and I put this as dollars

  • and I run this, it's going to say to you that nothing,

  • because they are the same.

  • OK?

  • Now one of the features we keep boosting about

  • is the interop between Java and Kotlin.

  • So we have this Java money one over here,

  • so I'm going to go ahead and create an instance of it.

  • I'll say javaMoney equals javaMoney, and it will be 100

  • and it'll be dollars.

  • And then if I do javaMoney, you can see

  • that I don't have any getters.

  • Well, I do have, actually, a getter, so I can do get amount,

  • but if I write getAmount, complete it,

  • the IDE's already going to replace that for property,

  • because we don't have really like getters and setters,

  • we just have properties.

  • So that's consuming Java from Kotlin.

  • And if I go over to the Java over here, and let's

  • go ahead and create a public statics void main,

  • and I'll do--

  • so let's see, I've got to declare the type.

  • Money equals new money, 100, and dollars, and--

  • OK.

  • That's the other thing.

  • Semicolons in Kotlin are optional.

  • And the reason they're optional is

  • so that you can have endless arguments over

  • whether you should use them or not.

  • We are trying to compete with JavaScript there.

  • OK, so money.getAmount.

  • So now I'm using a Java type-- sorry, a Kotlin type from Java,

  • so I'm getting the getters and the setters, right?

  • So idiomatic depending on how you are using it.

  • And that's just different Java files, different Kotlin files

  • in a single project, working seamlessly without any issues.

  • Right, so now let's go ahead and create some functions.

  • So I'm going to create a function called sendPayment.

  • That takes a money--

  • and Money-- and it's going to println the money out.

  • And of course we have string interpolation,

  • so I could say, sending money.amount.

  • And you don't have to put these curly braces if it's just

  • a single property that you're passing in,

  • as we'll just see in a moment.