Which is a new programming language that is a pragmatic, safe,concise, and interoperable alternative to Java.With Kotlin, you can implement your projects with less code, a higher level of abstraction, and fewer annoyances
Kotlin is concise, safe, pragmatic, and focused on interoperability with Java code. It can be used almost everywhere Java is used today - for server-side development,Android apps, and much more. Kotlin works great with all existing Java libraries and frameworks and runs with the same level of performance as Java.
This allows for shorter code and greater flexibility in creating data structures.But the downside is that problems like misspelled names can’t be detected during compilation and lead to runtime errors.
The ability of the compiler to determine types from context is called type inference.
Performance - Calling methods is faster because there’s no need to figure out at runtime which method needs to be called.
Maintainability-Working with unfamiliar code is easier because you can see what kind of objects the code is working with.
The most important of those is Kotlin’s support for nullable types.Which lets you write more reliable programs by detecting possible null pointer exceptions at compile time.
Kotline is functional programming language.
First-class function: You work with functions as values. You can store them in variables,pass them as parameters, or return them from other function.
Immutability: You work with the immutbale objects, which guarantees that there state can't change after their creation.
No side effects: You use pure functions that return the same result given the same inputs and don't modify the state of other object or interact with outside world.
What benefits can you gain from writing the code in the function style?
1.Conciessness: Working with functions as value gives you much more power of abstraction, which lets you avoid duplication in your code. Imagine that you have two similar code fragment that implement similar task but different in details. You can easily extract the common part of logic into a function and pass the differing parts as parameters. Those parameters are themselves functions but you can express them using a concise syntax for functions called lambda expressions:
fun findAlice() = findPerson{ it.name= 'Alice' }
fun findBob() = findperson{ it.name='Bob'}
2.Safe Multithreading:One of biggest sources of errors in multithreaded programs is modification of same data from multiple threads without proper synchronization. If you use pure function and immutable data structure, you can sure that such unsafe modifications won't happen.
3.Easier Testing: Code without side effects is usually easer to test. Functions can be tested in isolation without requiring a lot of setup cpde
Constructor In Kotlin
In this topic we will look about constructor. In kotlin we can create a class same as java. See below example we have create a class Human with method think and we call it in main method. We got the output calling this method. Now question is how to call constructor?. In java we have constructor which is same as class name. But in kotlin we can't do like that.
In above we see we have call a method in main. Now we want to pass a parameter in constructor.
Kotlin is awesome because if you don't want to use keyword constructor then it is also fine. But if you want to use modifier then a keywords become compulsory.
Below I call constructor without passing a value now in this case as we have seen previously we can set default value which is provided by kotlin. Refere in Name and Parameter chapter
Can I create a constructor inside a class? First whatever we use in above example it is called primary constructor. The thing we want to create it is second constructor. Alternative you can call init which is called when a class is load see that example first.
Now this time we will see how to create a secondary constructor. Why we need a secondary constructor as we seen in case if you want to more than one parameter in constructor for that we can use it.
Note: If we call/create secondary constructor then must have expect to call primary constructor. for do that we can use it constructor chaining mechanism using this keyword.