Kotlin vs Java

https://kotlinlang.org/
http://www.androidauthority.com/kotlin-vs-java-783187/
https://www.javacodegeeks.com/2016/05/kotlin-java-developers-10-features-will-love-kotlin.html
https://www.javacodegeeks.com/2016/04/10-features-wish-java-steal-kotlin-language.html

Most of the content below is from (https://gist.github.com/dodyg/5823184\ but a simplified version:

Basic

  • You do not need;to break statements
  • Unlike Java, you do not need to match your file name to your class name.
  • You can create functions outside classes. So there is no need to stuff your functions as static members of classes.
  • Kotlin has string templates, which is awesome. e.g."$firstName $lastName"for simple variable name or"${person.name} is ${1 * 2}"for any expressions. You can still do the string concatenation if you like e.g."hello " + "world", but that means being stupid

Variable

There are two keywords for variable declaration,

  • var and val.
  • Use var when the variable value is to be modified and val where the variable value will not change after first assigned.
  • This val is similar to final keyword in Java.
  • val variable must be initialized at declaration.
  • Unlike Java, you declare the type of a variable after the name, e.g.var firstName : String
  • Number primitive types are as follows: Double, Float, Long, Int, Short, Byte. There is no automatic conversion between types. You have to explicitly convert them.
  • The keyword voidcommon in Java or C# is calledUnit

Null Safety:

In Kotlin you have to decide whether a variable can be assigned null or not. This applies to both primitives or class types. A nullable variable is marked by assigning ? after the type, e.g.var firstName: String?

You can assign a value from not-nullable to nullable type without any problem.

fun main(args : Array<String>) { 
    val firstName : String = "Adam"
    val name : String? = firstName 
}

The other way around though requires that you declare that this nullable variable does not contain null at the point of assignment with !! operator (which pretty much declares : "I am sure this nullable variable is not null at this point")

fun main(args : Array<String>) { 
    val name : String? = "Adam" 
    val firstName : String = name!! 
}

Type inference

Kotlin is pretty smart about inferring what type a variable is, whether it is primitives or class.

fun main(args : Array<String>) { 
    val firstName = "Adam" 
    val middle = 'c' 
    val lastName = "Brown" 
    val age = 15 
}

Functions

  • Single expression function
  • Optional parameter.
  • Positional argument and named argument.
  • Variable argument.
  • Single expression function.
fun main(args : Array<String>) { 
    greet(englishGreeting()) 
    greet(italianGreeting())
} 

fun greet(msg : String){ 
    println(msg) 
} 

fun englishGreeting() : String = "Hello world" 

fun italianGreeting() : String{ 
    return "bon giorno" 
}

Functions can exists on their own.

  • It is marked by fun keyword.
  • If a function returns value, you declare it after the function name.
  • englishGreeting()is asingle expression function.
  • A void function such asgreet()returns Unit type but you are not required to declare it.
  • All parameters in a Kotlin function are read only. You are actually not allowed to mark it with either valorvarkeyword.

Function - Optional parameters

Kotlin allows you to assign default values for your parameters, making them optional.

fun main(args : Array<String>) {
  show()
  show("Good morning")
}

fun show (msg : String = "Hello World"){
    println("$msg") 
}

results matching ""

    No results matching ""