Features – Properties – Advantages of Kotlin Programming Language over other languages

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

This is the big announcement of the Google I/O 2017, the Kotlin language is now officially supported for the development of Android applications. This choice is partially acclaimed by the Android developer community but it is also a strategic choice of Google. Indeed because of the series of trials on the use of the Java language for Android, Google is looking for a means of bypass and it is possible that this wonderful language is a good initiative.

Topic: Features Advantages of Kotlin Programming Language

Originally, language was developed by JetBrains . It is to this company that we owe especially IntelliJ, the environment that serves as the basis in its community version to Android Studio. Moreover, it was already used by some large company developers for several years, despite the lack of official support. It can be compiled in Javascript or in machine code, it is compatible with the JVM.

I will try through this article to explain the reasons why Kotlin is a first choice language for application development:

1 Kotlin Interoperability with Java

Kotlin Is 100% interoperable with Java . It is possible to mix Java projects in your projects and use them with Kotlin. All your favorite Java frameworks are still available. Your Kotlin learning time will be even quicker.

2  A concise and concise syntax in Kotlin Programming language

Kotlin is not a language born in the academic world. Its syntax is familiar to any Java programmer who knows the basics of object-oriented programming. It can be learned quickly.There are of course some differences with Java like the reworked builders and the declaration of the variables.

Topic: Features Advantages of Kotlin Programming Language

(Val and var). Here is a classic “Hello World” to explain the basics:

Class foo {// declaration of a class
                              
    val b: String = "b" // val means not editable
    var i: Int = 0 // var means editable
  //coding180.com
    fun hello () {// declaring a function with fun
        val str = "Hello"
        Print ("$ str World") // return the print
    }

    fun sum (x: Int, y: Int): Int {
        Return x + y
    }

    Fun maxOf (a: Float, b: Float) = if (a & gt; b) a else b

}

3  String Interpolation in Kotlin Programming language

It is like a more intelligent and more readable version of the Java function:String.format()This function is directly integrated in the language:

val x = 4
val y = 7
print("sum of $x and $y is ${x + y}")  
// the sum of 4 and7 is11

4  Type Inference with Kotlin Programming language

val a = "abc"                         // this type corresponds to a String
val b = 4                             // this type corresponds to an Int

val c: Double = 0.7                   // this type is declared explicitly with:
val d: List = ArrayList()     // this type is declared explicitly

Topic: Features Advantages of Kotlin Programming Language

5  Smart Casts in Kotlin Programming language

The Kotlin compiler tracks your logic and auto-cast types, which means more than:instanceofHere’s an explicit cast:

If (obj is String) {
    print (obj.toUpperCase ()) // obj is now known as a String
}

6  Intuitive Equals with Kotlin Programming language

You can stop using:

equals()

Explicitly because the == operator now checks the structure equality:

val tom1 = Person ("Tom")
val tom2 = Person ("Tom")

tom1 == tom2 // true (equality of structure)
tom1 === tom2 // false (reference equality)


Tom1 == tom2 // true (equality of structure)
Tom1 === tom2 // false (reference equality)

7  Arguments by default with Kotlin Programming language

No need to define several similar methods with a multitude of arguments:

8  Named Arguments in Kotlin Programming language.

Combined with arguments (arguments) by default, named arguments eliminate the need for constructors:

build("PacMan", 400, 300)                           // equivalent
build(title = "PacMan", width = 400, height = 300)  // equivalent
build(width = 400, height = 300, title = "PacMan")  // equivalent

9  Using the expression “When” in Kotlin Programming language

The switch box is replaced by a more readable expression:

Topic: Features Advantages of Kotlin Programming Language

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")
}

It works both as an expression or a state and with or without arguments:

val res: Boolean = when {
    obj == null -> false
    obj is String -> true
    else -> throw IllegalStateException()
}

10 Properties” in Kotlin Programming language

Accessors (get and set) can be added to public attributes which means less redundant code (boiler plates).

class Frame {
    var width: Int = 800
    var height: Int = 600

    val pixels: Int
        get() = width * height
}

11 . Data Classes in Kotlin Programming language

This is a full POJO with toString (), equals (), hashCode () and copy (), and unlike Java it will not have 100 lines of code:

data class Person(val name: String,
                  var email: String,
                  var age: Int)

val john = Person("John", "admin@coding.com", 112)

12  The Overloading Operator in Kotlin Programming language

A predefined set of operators can be overloaded to improve readability:

data class Vec(val x: Float, val y: Float) {
    operator fun plus(v: Vec) = Vec(x + v.x, y + v.y)
}

val v = Vec(2f, 3f) + Vec(4f, 1f)

13 Destructuring Declarations in Kotlin Programming language

Some objects can be destructured, which is for example useful for iterating on maps:

for ((key, value) in map) {
    print("Key: $key")
    print("Value: $value")
}

14  The Ranges in Kotlin Programming language

For even more readability:

for (i in 1..100) { ... } 
for (i in 0 until 100) { ... }
for (i in 2..10 step 2) { ... } 
for (i in 10 downTo 1) { ... } 
if (x in 1..10) { ... }

15 . Extension Functions in Kotlin Programming language

Remember the first time you had to sort a List in Java? You could not find a sort function () you got a tutorial on Google to learn:

Collections.sort()

And later when you had to capitalize a String, you ended up writing your own help function because you Did not know:

StringUtils.capitalize()

If only there was a way to add new functions to old classes; This way your IDE could help you find the function in the continuity of your code. In Kotlin you can do just that:

fun String.format(): String {
    return this.replace(' ', '_')
}

val formatted = str.format()

The standard library extends the functionality of the original types of Java, which has been particularly useful for String:

str.removeSuffix(".txt")
str.capitalize()
str.substringAfterLast("/")
str.replaceAfter(":", "classified")

Topic: Features Advantages of Kotlin Programming Language

16  Null Safety in Kotlin Programming language.

Java is what we should call an almost statically typed language. In this sense, we can not guarantee that a String variable refers to a Strin – it could also refer to:

null

Although we are obliged to do so, it denies the security of verifying static types and as a result Java developers must live in constant fear of the NullPointerExceptions .

Kotlin solves this problem by making a distinction between non-zero types and possibly null ( nullable) types . Types are non-null by default and can be nullable by adding:

?

 

For example

var a: String = "abc"
a = null                // compile error

var b: String? = "xyz"
b = null                // no problem

Kotlin forces you to guard against NullPointerExceptions when you have access to a nullable type:

val x = b.length        // compile error: b might be null

And while this might seem cumbersome, it’s really a good idea to have some of its features.We always have smart shots (chic), which cast types from nullable to non-null as far as possible:

if (b == null) return
val x = b.length        // no problem

We could also use a secure call

?.

 

Which evaluates to the null instead of throwing a NullPointerExceptions:

val x = b?.length       // type of x is nullable Int

Safe calls can be chained together to avoid redundant testing: “if not the null” we sometimes write in other languages ​​and if we want a default value other than the null we can use the elvis operator:

?:

 

As for example:

val name = ship?.captain?.name ?: "unknown"

If none of these improvements satisfy you and you absolutely need a NullPointerException, you will need to explicitly request it:

val x = b?.length ?: throw NullPointerException()  // same as below
val x = b!!.length                                 // same as above

17. Kotlin Programming language has Better Lambdas

Is this a good lambda system? Perfectly balanced between legibility and being laconic, thanks to some intelligent design choices. The syntax is really very simple:

val sum = { x: Int, y: Int -> x + y }   // type: (Int, Int) -> Int
val res = sum(4,7)                      // res == 11

And here come the intelligent parts:

  1. Method parentheses can be moved or omitted if the lambda is the last or only argument of a method.
  2. If we choose not to declare the argument of an argument lambda alone it will be implicitly declared under the name ” it” .

Here are three equivalent lines:

numbers.filter({ x -> x.isPrime() })
numbers.filter { x -> x.isPrime() }
numbers.filter { it.isPrime() }

And this allows us to write concise functional code? – Just look at this beauty:

persons
    .filter { it.age >= 18 }
    .sortedBy { it.name }
    .map { it.email }
    .forEach { print(it) }

Topic: Features Advantages of Kotlin Programming Language

The Kotlin lambda system combined with extension functions makes it an ideal tool for creating DSL. Quickly look at Anko for an example of a DSL that aims to improve development on Android:

verticalLayout {
    padding = dip(30)
    editText {
        hint = “Name”
        textSize = 24f
    }
    editText {
        hint = “Password”
        textSize = 24f
    }
    button(“Login”) {
        textSize = 26f
    }
}

18. Kotlin Programming language IDE Support

You have a number of options if you intend to start with Kotlin, but I highly recommend usingIntelliJ which comes bundled with Kotlin preinstalled. These are the same person who have designed the language and the FDI.

Topic: Features Advantages of Kotlin Programming Language

Just to give you a minor but smart example, this window arose when I first tried to copy some Java code

Of Stack Overflow:

Features - Properties - Advantages of  Kotlin Programming Language over other languages - Features - Properties - Advantages of  Kotlin Programming Language over other languages - Features - Properties - Advantages of  Kotlin Programming Language over other languages - Features Advantages of Kotlin Programming Language

 

IntelliJ will offer you to convert Java code directly into Kotlin.

 

Topic: Features Advantages of Kotlin Programming Language

About The Author

Related posts

Leave a Reply