Commits

Zemian Deng committed d67e918

Added functions and shutdownhook demos.

Comments (0)

Files changed (2)

kotlin-demo/src/main/kotlin/FunctionsDemo.kt

+package functionsdemo
+fun main(args : Array<String>) {
+	println(double(99))
+	println(double2(99))
+	println(double3(99))
+	double4(99)
+	double5(99)
+
+	walk("/test")
+
+	//println(singletonArray(1001))
+
+	println(asList("aaa", "bbb", "ccc"))
+}
+
+fun double(n : Int) : Int {
+	return n * 2
+}
+fun double2(n : Int) : Int = n * 2
+fun double3(n : Int) = n * 2
+fun double4(n : Int) : Unit {
+	println(n * 2)
+}
+fun double5(n : Int) {
+	println(n * 2)
+}
+
+fun walk(path : String) {
+	fun walk(path : String, maxDepth : Int) {
+		println("Walking $path at depth: $maxDepth")
+	}
+	for (i in (1..10))
+		walk(path, i)
+}
+
+/*fun singletonArray<T>(item : T) : Array<T> { 
+  val result = Array<T>(1)
+  result[0] = item 
+  return result 
+}*/
+
+fun asList<T>(vararg ts : T) : List<T> { 
+  val result = java.util.ArrayList<T>()
+  for (t in ts) // ts is an Array 
+    result.add(t) 
+  return result 
+}

kotlin-demo/src/main/kotlin/ShutdownHookDemo.kt

+package shutdownhookdwmo
+fun main(args : Array<String>) {
+	val action = runnable{ println("Shutdown now.") }
+	Runtime.getRuntime().addShutdownHook(Thread(action))
+
+	println("Program is running. Press Ctrl+C to bring it down.")
+	//while(true)
+	//	Thread.sleep(java.lang.Long.MAX_VALUE)
+	val waiter = java.lang.Object()
+	synchronized(waiter) { waiter.wait() }
+}