Important Notes about basic concent of Kotlin

By | October 9, 2017

#Basics

    • You do not need ; to break statements.
    • Comments are similar to Java or C#, /* This is comment */ for multi line comments and // for single line comment.
    • Unlike Java, you do not need to match your file name to your class name.
    • Like JavaScript, you can create functions outside classes. So there is no need to stuff your functions as static members of classes like what you do in C# or Java.
    • 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.
    • It has no tuple although Kotlin’s data classes is an option to use in place of tuple.

#Variables

    • 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 readonly keyword in C# or final keyword in Java.
    • val variable must be initialized at declaration.
    • Unlike Java or C#, 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.
    • More primitive types: Char, String, Boolean.
    • All variable declarations in Kotlin must be initialized.
    • The keyword void common in Java or C# is called Unit in Kotlin.

Leave a Reply