Per me e startu programin e par ne kotlin njeki hapat ne kete Link!
Kotlin compiles in java byte code, it runs in Java Virtual Machine.
-Its fully interrupted with jvm.
-Supports immutability
-var: Variables - changeable.
-val: Values - are constants, like in java final attributes.
-Tries as much as possible to not use nulls.
-Its object oriented.
-Has classes, objects, object keyword...
-Doesnt have static.
-Has a very nice syntax.
-It has no getters or setters.
-It has to do something with lambdas like in java8.
Kotlin much less ceremony than java, not always specify types that its using in the language.
Offers first class support for function.
for example:
_____________________________________
val name:String = "kevin"
//val(value-constant) is never gonna change, but if it does, it will bring a compile time error!
//it will know if we write like this:
val name = "kevin" (still immutable)
var password:String
print("My name is $name")
Class Person(val name:String)
_____________________________________
interface Signatory{
fun sign()
}
class Person(val name:String) : Signatory {
override fun sign() = println("$name can sign documents")
}
fun main (args: Array<String>){
val p = Person()
p.sign()
}
console:
I can sign documents
_____________________________________
class Person(val name:String) //final by default(cant be derived)
{
fun main(args: Array<String>)
{
val kevin = Person("Kevin") // no new keyword
p.sign()
}
}
console-----------------------------
Kevin can sign documents
------------------------------------
____________________________________
open class Person(val name:String, var age:Int){
init{
if(name == "Kevin" && age < 54) throw Exception("Invalid age")
} //runs after object is constructed...
}
The keyword open means that it can be derived
class Student(name: String, age:Int) : Person(name, age)
In the class student constructor we dont need to specify that name is a val or age a var, it throws an error.
______________________________________
open class Person(val name:String, var age:Int){
var isMarried: Boolean
constructor(name:String, age:Int, isMarried: Boolean) : this(name, age)
{
this.isMarried =isMarried
}
}
Secondary constructors dont have var or val parameters...
open class Person(val name:String, var age:Int, var isMarried: Boolean = false) // means isMarried by default is false
______________________________________
______________________________________