Created
July 31, 2019 13:50
-
-
Save skymansandy/225346d9b5e54d5aed0e7d96b7611472 to your computer and use it in GitHub Desktop.
Compiled and run in Kotlin.org website along with this article: https://proandroiddev.com/the-kotlin-guide-for-the-busy-java-developer-93dde84a77b7
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
/** | |
* You can edit, run, and share this code. | |
* play.kotlinlang.org | |
*/ | |
fun main() { | |
//Nullsafe operator | |
var nickname: String? =null | |
val uppercase = nickname?.toUpperCase()?:"Bor $nickname?" | |
println(uppercase) | |
//Type conversion explicit | |
val i = 10 | |
val k : Long = i.toLong() | |
println(k) | |
//Array | |
var intArr : Array<Int> = arrayOf(1,2,3) | |
println(intArr[0]) | |
//Any type | |
var nice = 10L; | |
var crazy : Any = nice | |
println(crazy) | |
//is Operator | |
val isOp : String = "isOp" | |
println(isOp !is String) | |
//Safe cast | |
var safeCast:String = "safeCast" | |
var safeCastLen :Int? = safeCast as? Int ?: 0 | |
println(safeCastLen) | |
//For loop | |
for(i in 0 until 10) | |
print("$i ") | |
println() | |
for(i in 10 downTo 0) | |
print("$i ") | |
println() | |
for(i in 10 downTo 0 step 2) | |
print("$i ") | |
println() | |
val array = arrayOf("Kotlin", "Java", "Gradle") | |
for(s in array) | |
print("$s ") | |
println() | |
for(index in array.indices) | |
print("array[$index] = ${array[index]}\t") | |
println() | |
for(c in "alphabet") | |
print("$c ") | |
println() | |
for(i in 0..10){ | |
if(i>5) break | |
print("$i ") | |
} | |
println() | |
for(c in 'a'..'z'){ | |
print("$c ") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment