- Forum posts: 6
Sep 3, 2018, 11:37:52 AM via Website
Sep 3, 2018 11:37:52 AM via Website
Hi,
Kotlin For Android Developers
Kotlin is a new programming language from JetBrains, the maker of the world’s best IDEs. Now Kotlin supported by Google For Android.
Kotlin comes from industry, not academia. It solves problems faced by working programmers today. As an example, the type system helps you avoid null pointer exceptions. Research languages tend to not have null at all.
1. Why Kotlin
1.1 Java Interoperability
Kotlin language is inter-operable with java. It means we can user previous java project with new kotlin classes also and all java frameworks are still available. We have a one click java to kotlin converters also.
1.2 Familiar syntax
[Kotlin][1] Language syntax is very familiar to any programing language who came from OOP domine. There are some differences compare to Java such as val, var
Class Example{
val a:String= “Kosmik” //val means unmodifiable
Var i:int=1 // var means modifiable
}
Fun hellworld(){
Val str=”hello”
print(“$str World”)
}
1.3 String Interpolation
It’s as if a smarter and more readable version of Java’s String.format() built into the Kotlin language
var x=4
var y=3
print(“sum of $x and $y is ${x+y}”) //sum of 4 and 7 is 11
1.4 Open Source
It is open source under apache 2.0
1.5 Compatibility
Kotlin is fully compatible with JDK6 and kotlin applications can run on older android devices also
1.6 Performance
Kotlin application will run fast as an equal to java
1.7 Support
Kotlin is fully supported from Android Studio 3.0
1.8 Some Syntaxes
No more semicolons ;
Variable declaration is different in kotlin like var is mutable and val is immutable
Functions in kotlin (methods)
fun sum (x: Int ,y: Int): Int{
return x+y
}
when function
Now switch changed into when a condition, which is very easy to understand like below example
when (x) {
1 -> print(“x is 1”)
2 -> print(“x is 2”)
3, 4 -> print(“x is 3 or 4”)
in 5..10 -> print(“x is 5, 6, 7, 8, 9, or 10”)
else -> print(“x is out of range”)
}
6.creating a class in kotlin
val rectangle = Rectangle(5.0, 2.0) //no ‘new’ keyword required
val triangle = Triangle(3.0, 4.0, 5.0)
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.