Last active
October 7, 2018 21:51
-
-
Save ViksaaSkool/5aca74111dd7c4fd4384ddf3499eab07 to your computer and use it in GitHub Desktop.
KotlinBasics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Declaring variables */ | |
//[val/var] name_of_variable : [type] = value | |
//no need for ; at the end of the line | |
val a: Int = 1 | |
var b: String = "NullPointerException is dead" | |
var c: Double? = 0.4 | |
//val - read only value, constant | |
//var - mutable variable | |
//the '?' after the type means that c can also be null, if this is not added then c won't be able to get null as value | |
//Built-in types types: Int, Long, Double, Short, Float, Long | |
//Also: String, Boolean, Char | |
val x: String? = "Null Safety" | |
x.length // won't compile | |
x?.length // will compile | |
val y: String = null // won't compile | |
/* Literals and expressions */ | |
val i = 10 //don't have to explicitly state :Int | |
println("i = $i") // prints "i = 10" | |
val s = "kotlin" | |
println("$s.length is ${s.length}") // prints "abc.length is 6" | |
//best use: for logging and assiging value when fetching from servise or some data source | |
/* Expressions */ | |
//replace the switch with 'when' | |
when (x) { | |
1 -> print("x == 1") | |
2 -> print("x == 2") | |
else -> { // Note the block | |
print("x is neither 1 nor 2") | |
} | |
} | |
//the result of if/else block can be directly assigned to val/var | |
val max = if (a > b) a else b | |
//Ranges defined as in [range_start]..[range_end] | |
if (i in 1..10) { // equivalent of 1 <= i && i <= 10 | |
println(i) | |
} | |
//ranges and iteration | |
for (i in 1..3) { | |
println(i) //1,2,3 | |
} | |
/* Break and Continue Labels */ | |
//Any expression in Kotlin may be marked with a label. Labels have the form of an identifier followed by the @ | |
fun foo() { | |
listOf(1, 2, 3, 4, 5).forEach lit@{ | |
if (it == 3) return@lit // local return to the caller of the lambda, i.e. the forEach loop | |
print(it) | |
} | |
print(" this point is reachable, done with explicit label") // if it wasn't for the lit@, this line in the code | |
//wouldn't have been reachable | |
} | |
//to be honest, I don't think there's market for this, but people always will find a way to fancy up their code. | |
/* Functions */ | |
//[fun]function_name(argument:ArgumentType):returnType{} or = | |
//if void just ommit the return type or Unit | |
fun double(x: Int) = x * 2 | |
//same as | |
fun double(x: Int):Int{return x * 2} | |
//for variable number of arguments 'vararg' modifier is used | |
fun <T> asList(vararg ts: T): List<T> { | |
val result = ArrayList<T>() | |
for (t in ts) // ts is an Array | |
result.add(t) | |
return result | |
} | |
/* Classes */ | |
//constructor is in the declaration of the class, which is known as primary constructor and cannot contain any code, | |
//Initialization can be placed in blocks that are prefixed with 'init' | |
//by default all classes are public if not specified | |
class Customer(name: String) { | |
init{ | |
val customerKey = name.toUpperCase() | |
} | |
} | |
//secondary constructor - has to be noted with the 'constructor' keyword, has to delegate the primary constructor | |
//either directly or indirectly | |
class Customer(name: String) { | |
init { | |
val customerKey = name.toUpperCase() | |
} | |
constructor(name: String, surname: String, age: Int) : this(name){ | |
println("customer $name $surname, aged $age is always right") | |
} | |
} | |
//also it can be done like this | |
class MyView : View { | |
constructor(ctx: Context) : super(ctx) | |
constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) | |
} | |
//Inheritance is done in the following manner | |
open class Base(p: Int) | |
class Derived(p: Int) : Base(p) | |
/* Instancing object - no 'new' keyword required */ | |
val rectangle = Rectangle(5.0, 2.0) | |
//this means that you'll have to bend the rules when instancing new click listener with -> | |
//object:[the object implementation] | |
var view.setOnClickListener(object : View.OnClickListener { | |
override fun onClick(v: View?) { | |
toast("Kotlin is the best") | |
} | |
}) | |
/* Class properties */ | |
class Address { | |
private val LOG_TAG: String = Address::class.java.simpleName | |
var name: String = "Jon" | |
var street: String = "Malkovich" | |
var city: String = "Copenhagen" | |
} | |
//properties and fields are by default public, no need to use getters and setters - yes I know it's more complex than that, | |
//just believe me. | |
//private - means visible inside this class only (including all its members); | |
//protected — same as private + visible in subclasses too; | |
//internal — any client inside this module who sees the declaring class sees its internal members; | |
//public — any client who sees the declaring class sees its public members. | |
/* Interfaces */ | |
//nothing special regarding the interfaces in Kotlin compared to Java | |
interface MyInterface { | |
fun bar() | |
fun foo() { | |
// optional body | |
} | |
} | |
class Child : MyInterface { | |
override fun bar() { | |
// body | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment