Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Kotlin design decision :
- 1. Replace java with modern programming construct (like high order function, lambda, etc)
- 2. Concise, Expresive, Toolable, Interopbale, and Pragmatic (most important)
- 3. Statically typed language, targeted on JVM (primary) (expanded to javascript and native (under investigation)
- 4. Similar with other modern language (quick ramp-up time).
- 5. Interopable with Java (100% interopability)
- ## Introduction to JVM :
- JVM :
- - Abstract machine running Java application
- - Single specification (multiple implementation : Open JDK most common -> based on Sun Java)
- - An instance is what runs a Java application
- - Application compiled into Bytecode
- - JRE (for running application) and JDK (for development)
- - Polyglot language platform (compile to bytecode or transpile to Java first)
- ## Kotlin REPL (kotlinc)
- ```kotlin
- fun hello(name: String) {
- println("Hello $name")
- }
- ```
- ## Kotlin Application
- to compile (produce MainKt.class)
- ```
- >> kotlinc Main.kt
- ```
- to run (need kotlin-runtime.jar)
- ```
- >> java -cp {path to kotlin runtime} MainKt
- ```
- to compile into jar with kotlin-runtime
- ```
- >> kotlinc Main.kt -include-runtime -d hello.jar
- >> kotlin hello.jar
- ```
- ## Kotlin Basic
- Declaring variable
- ```kotlin
- var streetNumber: Int // mutable variable with type int
- var streetName: "High Street" // use type inferrence
- val zip = "10210" // immutable variable
- ```
- Type Variable
- ```kotlin
- val myLong = 10L
- val myFloat = 20.0F
- val myHex = 0x0F
- val myBinary = 0xb01
- val myInt = 100
- ```
- Conversion
- ```kotlin
- val someInt = 100
- val someLong: Long = someInt.toLong() // no implicit conversion
- ```
- String
- ```kotlin
- val oneLine = "a line.\n"
- val multipleLine = """
- This is a string
- with multiple line
- """
- val name = "abcde"
- val interpolation = "$name have ${name.length()} characters"
- ```
- Loop and ranges
- ```kotlin
- for (a in 1..100) print(a)
- for (a in 100..1) print(a)
- for (a in 100 downTo 1) print(a)
- for (a in 100 downTo 1 step 5) print(a)
- for (capital in listOf("London", "Paris", "Madrid")) {
- println(capital)
- }
- // also support java loop like : while, do while, break label, continue
- ```
- Conditional
- ```kotlin
- val myString = "string"
- if (myString != "") println("not empty")
- val result = if (myString != "") 10 else 0
- when (result) {
- is "string" -> println("is string")
- is String -> println("not string")
- }
- ```
- Packages and import
- ```kotlin
- // package and import similar in java
- ```
- ## Functions
- ```kotlin
- fun hello(): Unit { // Unit is void in java
- println("Hello")
- }
- fun throwException(): Nothing { // Nothing represent value that never exist
- throw Exception("This function throws an exception")
- }
- fun sum(x: Int, y: Int) = x + y // single expression function (with type inference)
- ```
- Default & named parameter
- ```kotlin
- fun printProfile(name: String, email: String, phone = "555") { // use default parameter for phone
- println("name : $name, email : $email, phone : $phone"
- }
- printProfile(name = "nobita", phone = "444", email = "nobita@rcti.com") // using named argument
- ```
- Unlimited argument (vararg)
- ```kotlin
- fun printAll(vararg strings: String) {
- for (string in strings) println(string)
- }
- printAll("ab")
- printAll("ab", "cd", "ef")
- ```
- ## Classes in Kotlin
- ```kotlin
- class Customer( { //in kotlin class don't have field, only property
- var id = 0
- var name: String = ""
- }
- class Person(var name: String, var age: Int) // don't need class body
- // Using constructor property (must use var/val,
- // if not it will be treated as value passed into constructor)
- // use val : for read-only property
- // use init block for property initialization
- class People(var name: String) {
- init {
- name = name.toUpperCase()
- }
- // create secondary constructor
- constructor(name: String, email: String): this(name) {
- name = "$name, $email"
- // this block behave like init block
- // will be executed after first init block
- }
- }
- fun main(args: Array<String>) {
- val customer = Customer()
- customer.id = 10
- customer.name = "coco"
- val person = Person("suneo", "9")
- println("name: ${person.name}, age: ${person.age}")
- }
- ```
- Custom Getter and Setter
- ```kotlin
- class People(val name: String, val birthYear: Int) {
- val age: Int
- get() = Calendar.getInstance().get(Calendar.YEAR) - birthYear
- var securityNumber: String = ""
- set(value) {
- if (!value.startWith("SN")) {
- throw IllegalArgumentException("security number must start with SN")
- }
- field = value
- }
- }
- ```
- Class Function
- ```kotlin
- class People(val name: String, val birthYear: Int) {
- fun info(): String {
- return "$name, ($birthYear)"
- }
- }
- ```
- Visibility Modifier
- 1. Have 4 visibility modifier : public, private, protected, internal.
- 2. Top level declarations
- - public : default visibility modifier and anywhere accessible
- - private : available inside file containing declaration
- - internal : anywhere inside the module.
- 3. Classes
- - public : any client who sees the declaring class sees its public members
- - 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
- Data classes
- ```kotlin
- data class Customer(var id: Int, var name: String, var email: String)
- fun main(args: Array<String>) {
- val customer1 = Customer(1, "Hadi", "hadi@gmail.com")
- val customer2 = Customer(1, "Hadi", "hadi@gmail.com")
- val customer3 = Customer(2, "Hadi", "hadi@gmail.com")
- val equal = customer1 == customer2 // true
- val notEqual = customer1 != customer3 // true
- customer4 = customer1.copy() // copy all value
- customer4 = customer1.copy(name = "Hodi") // copy with modified name
- }
- ```
- you can also override hashcode() and toString() in data class
Add Comment
Please, Sign In to add comment