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
Default Name and Parameters.
This is something new and that is not present in java.
In the same example 0.40 is interest rate of amount. In case of to make it dynamic we can take it as parameter.
Now a magic happen here you can see in below code the parameter has default value hook so incase we are not passing the value from calling place it will take as default value.
Now the same concept in java we call it method overloading because name of function remain same but it is different by its parameter. The same logic in java we need to create a two function with single and double argument but in kotlin by default function have default value as parameter.
Now talk about the Java what happen when we call this function in java class.
But question is, if we want to pass only amount instead of amount and interest rate for that we need to use annotation in our kotlin file @jvmoverloads which is create a two function for us.
This is amazing feature of kotlin. Now kotlin has also provide us the named parameter. Means sometime we are using the function of class which is not design by us and when us we does't know the sequence of parameter. To simplify this kotlin allow as to write a name with parameter value.
Now we got idea what is named parameter. When we call this function we can pass a parameter in any order but we can used the named parameter to pass it. This is new feature which is not there in java