|
On May 17 Google announced official support for the Kotlin language for Android in their "Google I/O 2017" conference from then all developers are curious about the language. We all know that from beginning Google Android accepted Java as primary programming language but after a long time they changed to Kotlin.
Why Kotlin (As mentioned in Kotlin website)
- Concise :
In this they talk about boilerplate code in Java™. We have to write unnecessary boilerplate codes to do a simplest thing in Java because of it's strict OOP nature. But in Kotlin they solved this problem by adopting C++ like structure(main function without a class).
- Data classes :
Kotlin also solved the problem of boilerplate getters and setters methods for data classes with its "data class" concept.
Kotlin
data class Customer(val name: String, val email: String, val company: String)
Java
<pre lang="java">class Customer {
String name;
String email;
String company;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
}
- Lambda functions :
In Java8 lambda functions were introduced it's great but in Kotlin they have further simplified it and made simple and it's also available at the core of the language.
Kotlin
fun main(args: Array<String>) {
val numbers = listOf(-10, -9, -8, -7, 2, 3, 4, 5);
val positiveNumbers=numbers.filter { it>=0 }
println(positiveNumbers);
}
Java
import java.util.Arrays;
import java.util.List;
class Sample {
public static void main(String args[]) {
Integer[] numArray = new Integer[] { -10, -9, -8, -7, 2, 3, 4, 5 };
List<Integer> numbers = Arrays.asList(numArray);
List<Integer> positiveNum = (List<Integer>) numbers.stream().filter((item) -> (item >= 0));
System.out.println(positiveNum);
}
}
- Singelton classes :
The class that can have only one instance is called singleton object in Java we don't have any implementation for this with a trick we can achieve this. But in Kotlin they have provided this out of the box
- Safe :
Java is also a safe language but Kotlin took this into another level with some additional features.
- Null check :
Kotlin supports two types of Objects namely Nullable (value can be null) and Non Nullable (value cannot be null). Programmer has to tell at the time of object declaration.
- Auto Cast :
Casting of object is a pain in java in Kotlin it's made simple and effective.
- Interoperable :
If your a Java developer thinking of converting your full code to Kotlin but it's not necessary because Kotlin run on the JVM both can share code with each other. Means you can use Java classes in Kotlin as well as Kotlin classes in java (in Kotlin one can access all Java packages).
- Tooling :
because it's developed by JetBrains the company developed InteliJ IDE so the Kotlin language has full IDE support in Android Studio 3.0, InteliJ IDE and eclispe.
Now let's look at some sample code in Kotlin
Simplest version
Kotlin
fun main(args: Array<String>) {
println("Hello World!")
}
Java
<pre lang="java">public class SampleJava {
public static void main(String args[]) {
System.out.println("Hello World!");
}
}
Object Oriented Version :
Kotlin
class Greeter(val name: String) {
fun greet() {
println("Hello, $name")
}
}
fun main(args: Array<String>) {
Greeter(args[0]).greet()
}
Java
<code class="language-java">
class Greeter {
private String name;
public Greeter(String name) {
this.name = name;
}
void greet() {
System.out.println("Hello, " + name);
}
public static void main(String args[]) {
new Greeter(args[0]).greet();
}
}
</code>
CodeProject
Ganesh Poojary
modified 14-Jun-17 1:57am.
|
|
|
|