Kotlin

Kotlin is statically typed programming language for modern multiplatform applications created by JetBrains.

Kotlin works side by side with Java and C++ on Android. So you can keep your existing code, continue to use the various Android libraries, and incrementally add Kotlin code to your project.Kotlin is a drop-in replacement you can use bi-directionally.You can call into the Java language from Kotlin, and you can call into Kotlin from the Java language.

 

Why Kotlin?

Reduce the boilerplate code.

Create a POJO with getters, setters,equals()hashCode() toString() and copy() in a single line:

Avoid NullPointerException.

Get rid of those pesky NullPointerExceptions, you know, The Billion Dollar Mistake.

Leverage existing libraries for JVM and Android.

Use any existing library on the JVM, as there’s 100% compatibility, including SAM support.

Once you start using the language you really go to appreciate it and that’s because it’s built by this company that has these millions of lines of Java.They Intimately know the pains and the things that cause them problems.The whole language is designed very programmatically to try and fixed those things.

Basic Syntax

So mostly we focus on understanding the syntax of Kotlin to start with and kind of the constructs that it provides. If you’ve done Java development there’s sort of this understanding of languages that this curve of understanding or maybe appreciation that you go through in almost every language.

Kotlin basic syntax

That there is no class notice that. So the great thing about kotlin is that you can just open a file and put function it so you have top-level functions. It’s not like Java that you need to have a static class and static method.You can just have a file called helper and you can distribute your function into.

Variable

We’re going to look at local properties. We don’t really talk about variables we talk about properties.There is a keyword in front of our property or local property val this denotes it is read-only.

We have the type and the name reversed is that because of a lot of time you can infer the type based on the context on the right side of the equal sign. So usually we’re calling a method referencing a field or referencing a constant here.

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.

Create Function

Default return type of function is Unit which is kind of like void but is not.It actually an object which is essentially a singleton, a single instance of an object.If it is a Unit, You don’t have to put it there.

We have functions that are really easy, like essentially returning a single value, you can just do single expression functions.

So I omit the return type explicitly, I omit curly braces and just return the actual function that you wanna do.

Null Values

You can’t assign null in Kotlin because Kotlin tries to get rid of the nullPointerException by saying that type isn’t nullable by default, But there are times when you want to have a type that is nullable for whatever reason.

You can add a question mark(?), and then that will give you the ability to assign null to that type.Now normally when you’re working with kotlin, you probably don’t want to do this.You probably don’t want to have nullable types, But Java can be null.So if I create a function here that is for instance,

 

 

name can be null.So there are two ways you can solve this.

1 name, not null.if condition.

2 The shorter way is just to use the safe operator or Elvis Operator.

When we have a nullable reference name, we can say “if name is not null, use it, otherwise use some non-null value x“:

Class

I can create classes.

Create a class that has two properties and these properties are immutable val means immutable.var means mutable that means i can read and write to it .I’ve also created a constructor so every time i create an instance of this class i have to pass in the two values.

There is no getId or setId we have properties you don’t really use the conventions of gatter or setter.

Now notice that I don’t have any open colon, open brackets, close brackets that are as simple as I can do create a class.

Create object

There is no need for the new keyword

Data Classes

The compiler automatically derives the following members from all properties declared in the primary constructor:

  • equals()/hashCode() pair,
  • toString() of the form "Person(id=1, name=Homen)"
  • componentN() functions corresponding to the properties in their order of declaration,
  • copy()

You’ll say not big deal any good IDE that’s can generate for you but does it also maintain it for you no because when I adding a new property I’ve got to go and regenerate and do I know that the code that I’ve generated is the standard or is it something that I’ve tweaked right.

Inheritance

All classes in Kotlin have a common superclass Any, that is a default super for a class with no supertypes declared

Any is not java.lang.Object  in particular, it does not have any members other than,equals() hashCode() and toString().

By default in Kotlin classes are final so, you  can not be inherited from class unless you explicitly mark it as open

Kotlin with Java Interoperability

My code in Java can reference thing in code in Kotlin, but also the code in Kotlin can reference things back in Java.Both talk to each other and it’s sort of interesting how that actually works in practice? So we have a Java code in the module. we have Kotlin code in the module.The Kotlin compiler is going to run first both source types are fed into the Kotlin compiler and the compiler is going to parse them both. 

Kotlin with Java Interoperability

If it were just a class files coming in on the class path doesn’t know anything about Where they come from and then it sees the Java source file coming and compiles that into the class files for the Java code when you then take those.

Two class file outputs and combine them. you get the combined output from both the Java and Kotlin files and you can see now why this allows you to reference in both ways

Because any Kotlin code referencing Java will compile because Kotlin Compiler is parsing both source formats. Java code referencing Kotlin will work because from Java compiler side it’s just like a library that you’re calling. it’s just class file that you referencing and then Kotlin has a standard library which is a just ton of kind of helpers.

if your developer in an Organization and potentially with tones of modules if you’re using Kotlin nobody else really has to know or care because the output of this project is just class files and transitive dependencies.

There’s no like weak implementation detail about the language and use because of that strong interoperability.

 

Leave a Reply

Your email address will not be published. Required fields are marked *