Last active
February 19, 2016 16:06
-
-
Save nawada/b3a404a1e96bf23830d4 to your computer and use it in GitHub Desktop.
Kotlin basic syntax
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
import java.util.* | |
fun main(args: Array<String>) { | |
println("Hello, world") | |
basicTypes() | |
val c = cls("Accessible", "Unsettable") | |
c.publicMethod() | |
c.accessible = "Accessible value is updated !" | |
// c.unsettable = "ERROR!!!" | |
println(c.accessible) | |
println(c.unsettable) | |
println("--------------------") | |
c.publicMethod() | |
} | |
/** | |
* クラスでGetter/Setterの定義をする. | |
* varだとGetter/Setterともに定義されるが、 | |
* valだとGetterのみ定義される. | |
*/ | |
class cls(var accessible: String, val unsettable: String) { | |
val publicValue = "Public" | |
private val privateValue = "Private" | |
init { | |
// コンストラクタチックな処理はここで行う | |
} | |
fun publicMethod() { | |
println("public") | |
println(accessible) | |
println(unsettable) | |
println(publicValue) | |
println(privateValue) | |
privateMethod() | |
} | |
private fun privateMethod() { | |
println("private") | |
println(accessible) | |
println(unsettable) | |
println(publicValue) | |
println(privateValue) | |
} | |
} | |
fun basicTypes() { | |
// 変数宣言 | |
var foo: String = "Hoge" | |
foo = "Fuga" | |
// 定数宣言 | |
val bar: Int = 0 | |
// bar = 1 // Error | |
// 型推論 | |
val integer = 0 // Int | |
val dbl = 0.0 // Double | |
println(integer is Int) // true | |
println(dbl is Double) // true | |
// println(int is Double) // Error | |
// 基本型たち | |
//// 数値系 | |
val byte: Byte = 0x1 | |
val short: Short = 2 | |
val int: Int = 3 | |
val long: Long = 4L | |
val float: Float = .5f | |
val double: Double = 9.99 | |
//// 文字系 | |
val chr: Char = 'c' | |
val str: String = "string" | |
//// 論理系 | |
val bool: Boolean = true | |
//// 配列 | |
val array: Array<String> = Array(3, { "0" }) // "0", "0", "0" | |
val intArray: IntArray = intArrayOf(1, 2, 3) // 1, 2, 3 | |
// ヒアドキュメント | |
val hearDoc = """ | |
for (c in "foo") | |
print(c) | |
""" | |
println(hearDoc) | |
// 文字列テンプレート | |
val tmpl = "float is $float" | |
println(tmpl) // float in 0.5 | |
println("str length is \"${str.length}\"") // str length is 6 | |
val price = """ | |
${'$'}$double | |
""" // $を文字として使いたい場合、上記のようなエスケープが必要. \によるエスケープは無いとのこと | |
println(price) // $9.99 | |
// Nullable | |
var nullable: String? = null | |
println(nullable) // null | |
println(nullable?.length) // null | |
nullable = "hoge" | |
println(nullable) // hoge | |
println(nullable?.length) // 4 | |
println(nullable!!.length) // 4 | |
nullable = null | |
println(if (nullable != null) nullable.length else -1 ) // -1 | |
println(nullable?.length ?: -1) // ↑と同じ(エルビス演算子) | |
// println(nullable!!.length) // NullPointerExceptionがthrowされる | |
} | |
fun returnValue(x: Int, y: Int): Int { | |
return x + y | |
} | |
fun simpleReturnValue(x: Int, y: Int) = x + y | |
fun noReturn() { | |
} | |
fun noReturn2(): Unit { | |
} | |
fun exampleFor() { | |
val array = Array(5, { i -> i }) | |
for (i in 0..array.size - 1) print("$i, ") // 0, 1, 2, 3, 4, | |
} | |
fun exampleWhile() { | |
val array = Array(5, { i -> i }) | |
var i = 0; | |
while (i < array.size) print("${array[i++]}, ") // 0, 1, 2, 3, 4, | |
} | |
fun conditionalOperation(x: Int, y: Int) { | |
if (x > y) { | |
println("$x > $y") | |
} else { | |
println("$x <= $y") | |
} | |
if (x > y) println("$x > $y") | |
else println("$x < $y") | |
} | |
fun shortConditionalOperation(x: Int, y: Int) = if (x > y) x else y | |
fun whenExp(v: Any) { | |
when (v) { | |
is Int -> println("Int") | |
is String -> println("String") | |
else -> println("else") | |
} | |
} | |
fun usefulWhen(x: Int) { | |
when (x) { | |
in 0..9 -> println("$x is small") | |
in 10..20 -> println("$x is big") | |
else -> println("I can't count $x !") | |
} | |
} | |
fun checkRange(x: Int) { | |
println(x in 0..10) | |
println(x !in 4..6) | |
val y = 10 | |
println(x in 0..y - 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment