標籤:
kotlin是一個新生的基於jvm的語言,可以看成java的擴充語言,由JetBrains開發,初次見到他是之前在公司的安卓團隊看到他們使用kotlin開發android,頓時覺得好奇,後來慢慢的研究了這個預言。
為什麼要開發和使用kotlin這門語言,官方的說法是:
Concise SafeVersatileInteroperable
並且舉了一個例子:
Get rid of those pesky NullPointerExceptions, you know, The Billion Dollar Mistakevar output : Stringoutput = nullAnd of course, Kotlin protects you from mistakenly operating on nullable types, including those from Javaprintln(output.length())And if you check a type is right, the compiler will auto-cast it for youfun calculateTotal(obj: Any) { if (obj is Invoice) { obj.calculateTotal() }}
看到這裡我大概明白了,然後我想說:滾
安裝:
一:在 點擊下載kotlin 下載kotlin的二進位檔案或者在github上拖源碼,都可以,我是使用的源碼編譯,但結果都是產生如下結構:
二:接下來開始配置環境變數:
export KOTLIN_HOME=XXXXXXX/kotlincexport PATH=${PATH}:${KOTLIN_HOME}/bin
很基本的環境變數,linux的,不多說了。
三:寫HelloWorld(hello.kt):
fun main(args: Array<String>) { println("Hello, World!")}
四:開始編譯和運行:
kotlinc-jvm hello.kt -include-runtime -d hello.jarjava -jar hello.jar
到這一步,我們已經使用kotlin的文法,寫出了一個helloworld的代碼,並且已經編譯產生一個jar,並且是包含main的可以直接運行,接下來開始分析下這個jar:
我們看到它產生的jar,也是包含一個main的java類,java裡面的main方法是可以被final修飾的。
(原)java擴充語言:.Kotlin實踐-基本安裝與使用