In this deck, I explain the basic concepts and syntax of Kotlin language along with basic concepts of android and how you can combine them to make beautiful apps.
// M et hod to find an equal index • static int findIndex(String str) • { • int len = st r.length(); • int open[] = new int[len+1]; • int close[] = new int[len+1]; • int index = -1; • • open[0] = 0; • close[len] = 0; • if (str.charAt (0)=='(') • open[1] = 1; • if (str.charAt (len-1) == ')') • close[len-1] = 1; • • // St ore t he number of opening bracket s • // at each index • for (int i = 1; i < len; i++) • { • if ( st r.charAt (i) == '(' ) • open[i+1] = open[i] + 1; • else • open[i+1] = open[i]; • } •
number of closing bracket s • // at each index • for (int i = len-2; i >= 0; i--) • { • if ( st r.charAt (i) == ')' ) • close[i] = close[i+1] + 1; • else • close[i] = close[i+1]; • } • • // check if t here is no opening or closing • // bracket s • if (open[len] == 0) • return len; • if (close[0] == 0) • return 0; • • // check if t here is any index at w hich • // bot h bracket s are equal • for (int i=0; i<=len; i++) • if (open[i] == close[i]) • index = i; • • return index; • } •
with var keyword. var name = "Kashif Mehmood" var profession = "Software Engineer" Values / Constants: Values that do not change or immutable are declared with val keyword or const val keyword. val datoOfBirth = "15-03-1998" const val gender = "Male" Type Inference: No need to specify types, types are infered by compiler.
that their values can be set at run time. However, once a values is set it can not be modified. Const Consts are compile time constants. Their value has to be assigned during compile time. They can not be assigned to any function or constructors.
with mutable variable [var] • Allowed with only non- nullable data types • It is a promise to compiler that the value will be initialized in future By lazy • Lazy initialization was designed to prevent unnecessary initialization of objects. • Your variable will not be initialized unless you use it. • It is initialized only once. Next time when you use it, you get the value from cache memory.