0% found this document useful (0 votes)
70 views

Kotlin 1 1 Hello Kotlin

The document appears to be slides from a presentation on Kotlin basics. It introduces top-level functions, if expressions, string templates, and null safety in Kotlin by providing code examples and explanations.

Uploaded by

Bebeto Silva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Kotlin 1 1 Hello Kotlin

The document appears to be slides from a presentation on Kotlin basics. It introduces top-level functions, if expressions, string templates, and null safety in Kotlin by providing code examples and explanations.

Uploaded by

Bebeto Silva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

“Hello, World!


example
“Hello, Kotlin” example

package intro

fun main(args: Array<String>) {


val name = if (args.size > 0) args[0] else "Kotlin"
println("Hello, $name!")
}
Hello, Kotlin!
New keywords: fun, val
package intro

fun main(args: Array<String>) {


val name = if (args.size > 0) args[0] else "Kotlin"
println("Hello, $name!")
}
New keywords: fun, val
package intro

fun main(args: Array<String>) {


val name = if (args.size > 0) args[0] else "Kotlin"
println("Hello, $name!")
}
No class
package intro

fun main(args: Array<String>) {


val name = if (args.size > 0) args[0] else "Kotlin"
println("Hello, $name!")
} Hello.kt

You can define functions at the top level


Arrays
package intro

fun main(args: Array<String>) {


val name = if (args.size > 0) args[0] else "Kotlin"
println("Hello, $name!")
}

Array looks like a regular class


if expression
package intro

fun main(args: Array<String>) {


val name = if (args.size > 0) args[0] else "Kotlin"
println("Hello, $name!")
}

if is an expression
String templates
package intro

fun main(args: Array<String>) {


val name = if (args.size > 0) args[0] else "Kotlin"
println("Hello, $name!")
}

Hello, Kotlin!

String templates: easy way to insert a value into a String


String templates

"...$variable..."

"...${functionCall(...)}..."

fun main(args: Array<String>) {


println("Hello, ${args.getOrNull(0)}!")
}
? What will be printed
if no arguments are passed?
fun main(args: Array<String>) {
println("Hello, ${args.getOrNull(0)}!")
}

1. Hello, null!
2. Hello, !
3. NPE will be thrown
?
? What will be printed
if no arguments are passed?
fun main(args: Array<String>) {
println("Hello, ${args.getOrNull(0)}!")
}

1. Hello, null!
2. Hello, !
3. NPE will be thrown
Works as in Java

public static void main(String[] args) {


System.out.println("Hello, " + null + "!");
}

Hello, null!

fun main(args: Array<String>) {


println("Hello, ${null}!") Hello, null!
}
? What will be printed?
fun foo(): String {
println("Calculating foo...")
return "foo"
}

fun main(args: Array<String>) {


println("First ${foo()}, second ${foo()}")
}

Calculating foo... Calculating foo...


Calculating foo... First foo, second foo
First foo, second foo
?
? What will be printed?
fun foo(): String {
println("Calculating foo...")
return "foo"
}

fun main(args: Array<String>) {


println("First ${foo()}, second ${foo()}")
}

Calculating foo... Calculating foo...


Calculating foo... First foo, second foo
First foo, second foo

You might also like