Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,547 members, 7,812,735 topics. Date: Monday, 29 April 2024 at 06:16 PM

Why You Should Use Kotlin Language (java Alternative) For Android Development - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Why You Should Use Kotlin Language (java Alternative) For Android Development (2118 Views)

Java Or Kotlin For Android? / Kotlin For Android Developers By Antonio Leiva" Ebook(pdf). / Kotlin / JAVA For Android Development (2) (3) (4)

(1) (Reply) (Go Down)

Why You Should Use Kotlin Language (java Alternative) For Android Development by junkle: 9:12am On Aug 01, 2017
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 bypassing this, and it is possible that this wonderful language is a good initiative.

Learn kotlin programming - Complete leassons tutorials here

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.


(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"wink // return the print
    }

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

    Fun maxOf (a: Float, b: Float) = if (a > b) a else b

}

Learn kotlin programming - Complete leassons tutorials here

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}"wink
// the sum of 4 and7 is 11

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

5  Smart Casts

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

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

6  Intuitive Equals

You can stop using:

equals()

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

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

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

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

fun build(title: String, width: Int = 800, height: Int = 600) {
Frame(title, width, height)
}

8  Named Arguments.

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"wink // equivalent

9  Using the expression "When"

The switch box is replaced by a more readable expression:



when (x) {
1 -> print("x is 1"wink
2 -> print("x is 2"wink
3, 4 -> print("x is 3 or 4"wink
in 5..10 -> print("x is 5, 6, 7, 8, 9, or 10"wink
else -> print("x is out of range"wink
}
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

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@coding180.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"wink
print("Value: $value"wink
}

Learn kotlin programming - Complete leassons tutorials here

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"wink
str.capitalize()
str.substringAfterLast("/"wink
str.replaceAfter(":", "classified"wink
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 String - 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

Learn kotlin programming - Complete leassons tutorials here

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:

Method parentheses can be moved or omitted if the lambda is the last or only argument of a method.
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) }



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 using IntelliJ which comes bundled with Kotlin preinstalled. These are the same person who have designed the language and the FDI.


Learn kotlin programming - Complete leassons tutorials here

source: https://coding180.com/blog/features-advantages-kotlin-language-use-kotlin/

1 Like

Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by Jocent101(m): 11:56am On Aug 01, 2017
wow!!!! no need to learn java
Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by Nobody: 2:07pm On Aug 01, 2017
Google made Golang, Google made Android. Why didnt they make golang android's official language

1 Like

Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by Desyner: 3:41pm On Aug 01, 2017
I read about the language some weeks back. It was rated as one of the fastest rising languages.
Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by seunny4lif(m): 8:19pm On Aug 01, 2017
Jocent101:
wow!!!! no need to learn java
Bro you still need java but Kotlin just make code easy

Java:

Class Demo {
public static void main (String []agrs)
{
System.out.println ("Hello, World "wink;
}
}



Kotlin:

Main(agrs: Array <String>wink{
Println ("Hello World "wink
}

1 Like

Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by Olyboy16(m): 11:11pm On Aug 01, 2017
justanALIAS:
Google made Golang, Google made Android. Why didnt they make golang android's official language

Golang is inoperable with java, its a totally different ecosystem. So google will need to re-build most of the Android API in Go to really make it usable. Whereas, kotlin has first class support for java; it even works on JVM! so, Google's first class support also fell on kotlin.

I see oracle loosing a lot of application developers here; only hardcore JVM and system level developers will remain in 7 years time. Except for leagcy apps developers though.
Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by seunny4lif(m): 10:40am On Aug 02, 2017
Olyboy16:


Golang is inoperable with java, its a totally different ecosystem. So google will need to re-build most of the Android API in Go to really make it usable. Whereas, kotlin has first class support for java; it even works on JVM! so, Google's first class support also fell on kotlin.

I see oracle loosing a lot of application developers here; only hardcore JVM and system level developers will remain in 7 years time. Except for leagcy apps developers though.
Oracle non open source make Android to develop Kotlin
Kotlin is really cool and it's less of code line
Few lines and you get your result
Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by Olyboy16(m): 10:54pm On Aug 02, 2017
seunny4lif:

Oracle non open source make Android to develop Kotlin
Kotlin is really cool and it's less of code line
Few lines and you get your result
the execution speed though, it seems kotlin might be practically slow. Talking from hands-on experience
Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by Nobody: 11:02pm On Aug 02, 2017
We need to rewrite nairaland with kotlin
Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by melodyogonna(m): 1:59am On Aug 03, 2017
donjayzi:
We need to rewrite nairaland with kotlin
lol
Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by seunny4lif(m): 5:54am On Aug 03, 2017
Olyboy16:

the execution speed though, it seems kotlin might be practically slow. Talking from hands-on experience
Yes
But I'm still learning it sha
Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by edicied: 12:18pm On Aug 03, 2017
seunny4lif:

Bro you still need java but Kotlin just make code easy

Java:

Class Demo {
public static void main (String []agrs){
System.out.println ("Hello, World "wink ;
}
}

Kotlin:

Main(args: Array <String>wink{
Println ("Hello World "wink
}

Just like javascript and the Library JQuery Trust JQuery may use fewer words but it's harder to understand xame with this Kotlin
Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by seunny4lif(m): 12:23pm On Aug 03, 2017
edicied:

Just like javascript and the Library JQuery Trust JQuery may use fewer words but it's harder to understand xame with this Kotlin
Broda
Kotlin look simple but like you said its confusing.
I have be trying to do some exercise but i no come understand again
Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by Nofuckgiven: 7:14pm On Aug 03, 2017
Hmmmm. I never learn java finish na kotlin,OK oh! undecided

1 Like

Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by LordRahl001: 12:56pm On Aug 05, 2017
@OP, this is pretty nice! More strength to ur fingers and deeper understanding of ur trade. Thi just gingered me to try out kotlin after swift!
Thanks a lot.
Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by seunny4lif(m): 1:16pm On Aug 05, 2017
Nofuckgiven:
Hmmmm. I never learn java finish na kotlin,OK oh! undecided

Lol grin grin grin
Bros no be small thing grin grin
Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by Nofuckgiven: 6:59pm On Aug 05, 2017
seunny4lif:


Lol grin grin grin
Bros no be small thing grin grin
I no be bros oh. I be babe grin

1 Like

Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by seunny4lif(m): 7:01pm On Aug 05, 2017
shocked
My bad oooooh
kiss kiss
Nofuckgiven:

I no be bros oh. I be babe grin
Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by Nofuckgiven: 7:14pm On Aug 05, 2017
seunny4lif:
shocked
My bad oooooh
kiss kiss
Its ait cool

1 Like

Re: Why You Should Use Kotlin Language (java Alternative) For Android Development by DevDenky: 12:56am On Aug 06, 2017
Already good with java for mobile/android development. If i should pick up another language tomorrow, its going to be js or php to target the webspace.

1 Like

(1) (Reply)

Convert My Cms From Cfm To Php / Ccna Certifications / How To Create A Small Gui(graphical User Interface) Using Jframe In Java Program

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 45
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.