kotlin編程語言
什么是Kotlin? (What is Kotlin?)
Kotlin is a programming language developed by Jetbrains, the company behind some of the world’s most popular IDEs like IntelliJ and Pycharm.
Kotlin是Jetbrains開發的一種編程語言,該公司是IntelliJ和Pycharm等世界上最流行的IDE的背后的公司。
It serves as a replacement for Java and runs on the JVM. It has been in development for close to 6 years and it hit 1.0 just a year ago.
它可以替代Java并在JVM上運行。 它已經開發了將近6年,一年前就達到了1.0。
The developer community has embraced Kotlin to such an extent that Google announced first class support for the language for Android Development at Google I/O 2017.
開發人員社區對Kotlin的接受程度如此之高,以至于Google在2017年I / O大會上宣布了對Android開發語言的一流支持。
版 (Version)
As of this writing, the latest stable release of Kotlin happens to be version 1.2.71
在撰寫本文時,Kotlin的最新穩定版本恰好是1.2.71版。
安裝 (Installation)
Before proceeding with the installation instructions for Kotlin, you need to make sure that you have set up JDK (Java Development Kit) set up on your system.
在繼續進行Kotlin的安裝說明之前,您需要確保已在系統上設置了JDK(Java開發套件) 。
If you do not have JDK installed on your computer, head over to the Installation section on this link to learn how to set it up.
如果您的計算機上尚未安裝JDK,請轉到此鏈接上的“ 安裝”部分 以了解如何進行設置。
Kotlin works with JDK 1.6+ so make sure you get the correct version installed. Once you are done setting up JDK, proceed with the following steps.
Kotlin與JDK 1.6+一起使用,因此請確保您安裝了正確的版本。 設置完JDK后,請繼續以下步驟。
IntelliJ IDEA (IntelliJ IDEA)
The quickest way to get Kotlin running on your machines is by using it alongside IntelliJ IDEA. This is the recommended IDE for Kotlin because of the tooling support that is provided by Jetbrains. You can grab the Community Edition of IntelliJ from JetBrains.
使Kotlin在您的機器上運行的最快方法是將其與IntelliJ IDEA一起使用。 這是Kotlin推薦的IDE,因為Jetbrains提供了工具支持。 您可以從JetBrains獲取IntelliJ 社區版 。
Once you have installed IntelliJ you can basically get started with your first project in Kotlin without any further configurations.
一旦安裝了IntelliJ,您就可以基本開始在Kotlin中的第一個項目,而無需進行任何其他配置。
Create a New Project and make sure you select the Java Module. Select the Kotlin checkbox on that screen
創建一個新項目 ,并確保選擇Java模塊。 選中該屏幕上的Kotlin復選框
Give your project a name and click Finish.
為您的項目命名,然后單擊“完成”。
You will now be taken to the main editor where you will see your project files organized in the following manner.
現在,您將被帶到主編輯器,您將在其中看到以以下方式組織的項目文件。
In order to verify your installation, create a new Kotlin file in the src folder and name it app (or anything else that suits you)
為了驗證您的安裝,請在src文件夾中創建一個新的Kotlin文件并將其命名為app (或其他適合您的名稱)
Once you have the file created, type out the following cremonial Hello World code. Don’t worry if it doesn’t make sense right away, it will be dealt with in detail later on in the guide.
創建文件后,鍵入以下常用的Hello World代碼。 不用擔心,如果這沒有意義,它將在本指南的后面部分中詳細介紹。
fun main (args: Array<String>) {println("Hello World!")
}
You can now run this program by either clicking on the Kotlin icon on the gutter (left side of your editor with line numbers)
現在,您可以通過單擊裝訂線上的Kotlin圖標(帶有行號的編輯器左側)來運行此程序。
If everything goes fine, you should see the message Hello World! in your Run window as shown below
如果一切正常,您應該看到消息Hello World! 在運行窗口中,如下所示
蝕 (Eclipse)
While IntelliJ is the recommended IDE for developing with Kotlin, it is definitely not the only option out there. Eclipse happens to be another popular IDE of choice among Java developers and Kotlin is supported by Eclipse as well.
盡管IntelliJ是建議使用Kotlin開發的IDE,但絕對不是唯一的選擇。 Eclipse恰好是Java開發人員中另一個受歡迎的IDE,并且Eclipse也支持Kotlin。
After setting up the JDK on your system, follow the instructions below.
在系統上設置JDK后 ,請按照以下說明進行操作。
Download Eclipse Neon for your operating system and once you have successfully installed it on your system, download the Kotlin Plugin for Eclipse from the Eclipse Marketplace.
下載適用于您的操作系統的Eclipse Neon ,并在將其成功安裝到系統上之后,從Eclipse Marketplace下載適用于Eclipse的Kotlin插件 。
NOTE: You can also do the same by going into Help -> Eclipse Marketplace and then search for Kotlin Plugin
注意:您也可以通過進入“幫助”->“ Eclipse市場”,然后搜索“ Kotlin插件”來執行此操作。
Once, the plugin is installed you are pretty much done but it would be a good idea to take the IDE for a spin with a quick Hello World sample.
插件安裝完成后,您已經完成了很多工作,但是最好通過快速的Hello World示例來嘗試一下IDE。
Create a new Kotlin Project by clicking on File -> New -> Kotlin Project
通過單擊文件->新建-> Kotlin項目創建一個新的Kotlin項目
An empty project will be created with a directory structure quite similar to a Java project. It would look something like this
將創建一個空項目,其目錄結構與Java項目非常相似。 看起來像這樣
Go ahead and create a new Kotlin file in the src folder
繼續,在src文件夾中創建一個新的Kotlin文件
Once that is done go ahead and type out the following code. Don’t worry if it does not make sense right now, it will be covered later in the guide.
完成后,繼續并鍵入以下代碼。 不用擔心,如果現在沒有意義,它將在本指南的后面部分介紹。
fun main (args: Array<String>) {println("Hello World!")
}
Now that you are done typing out the Hello World code, go ahead and run it. To run the file, right click anywhere inside the editor and click on Run As -> Kotlin Application
現在您已經完成了輸入Hello World代碼的操作,請繼續運行它。 要運行文件,請右鍵單擊編輯器內的任何位置,然后單擊運行方式-> Kotlin應用程序
If all goes well, the console window would open to show you the output.
如果一切順利,將打開控制臺窗口以顯示輸出。
在終端上使用獨立編譯器 (Using the standalone compiler on the terminal)
If you are someone who prefers doing things in a more manual way and do not want to tie yourself down to an editor/IDE you might wanna use the Kotlin compiler.
如果您喜歡以一種更手動的方式進行操作并且不想將自己束縛于編輯器/ IDE,那么您可能想使用Kotlin編譯器。
下載編譯器 (Downloading the compiler)
With every release of Kotlin, Jetbrains ship a standalone compiler which can be downloaded from the GitHub releases. Version 1.1.51 happens to be the latest at the time of this writing.
對于Kotlin的每個發行版,Jetbrains都提供了一個獨立的編譯器,可以從GitHub發行版中下載。 在撰寫本文時,版本1.1.51恰好是最新版本。
手動安裝 (Manual Installation)
Once you have downloaded the compiler you need to unzip it and proceed with the standard installation using the installation wizard. Adding the bin directory to the system path is an optional step. It contains the scripts that are necessary to compile and run Kotlin on Windows, Linux and macOS.
下載完編譯器后,需要解壓縮該編譯器,然后使用安裝向導進行標準安裝。 將bin目錄添加到系統路徑是可選步驟。 它包含在Windows,Linux和macOS上編譯和運行Kotlin所需的腳本。
通過自制軟件安裝 (Installation via Homebrew)
You can install the compiler on macOS using Homebrew which is a package manager for macOS. Launch the Terminal app and issue the following commands
您可以使用Homebrew(這是macOS的軟件包管理器)在macOS上安裝編譯器。 啟動終端應用程序并發出以下命令
$ brew update
$ brew install kotlin
通過SDKMAN安裝! (Installation via SDKMAN!)
Another simple way of installing the Kotlin compiler on macOS, Linux, Cygwin, FreeBSD and Solaris is by using SDKMAN!. Launch the terminal and issue the following commands
在macOS,Linux,Cygwin,FreeBSD和Solaris上安裝Kotlin編譯器的另一種簡單方法是使用SDKMAN! 。 啟動終端并發出以下命令
$ curl -s https://get.sdkman.io | bash
$ curl -s https://get.sdkman.io | bash
Follow the instructions on screen and once SDKMAN! is setup issue the follwoing command inside terminal
按照屏幕上的說明進行操作,然后按一下SDKMAN! 是安裝程序發出終端內的follwoing命令
$ sdk install kotlin
$ sdk install kotlin
As with all previous installation options, it would be a good idea to test run the installation.
與以前的所有安裝選項一樣,最好對運行安裝進行測試。
Open a text editor of your choice and write a basic Kotlin program given below
打開您選擇的文本編輯器,并編寫以下基本的Kotlin程序
fun main(args: Array<String>) {println("Hello, World!")
}
Save this file with a .kt extension. You are now ready to compile it and see the results. To do so, issue the following command
使用.kt擴展名保存此文件。 現在您可以對其進行編譯并查看結果了。 為此,發出以下命令
$ kotlinc hello.kt -include-runtime -d hello.jar
$ kotlinc hello.kt -include-runtime -d hello.jar
the -d
option tells the compiler what you want the output to be called. The -include-runtime
option makes the resulting .jar file self-contained and runnable by including the Kotlin runtime library in it.
-d
選項告訴編譯器要調用輸出的內容。 -include-runtime
選項通過在其中包含Kotlin運行時庫,使生成的.jar文件可獨立運行。
If there were no compilation errors, run the application using the following command
如果沒有編譯錯誤,請使用以下命令運行應用程序
$ java -jar hello.jar
$ java -jar hello.jar
If all goes well, you should see Hello World! printed on your terminal screen
如果一切順利,您應該會看到Hello World! 打印在您的終端屏幕上
$ java -jar hello.jar
Hello, World!
Congratulations you have successfully set up the Kotlin compiler and development environment on your system. We will cover all of the intricacies and fun parts of Kotlin in this guide, but you can get a head start if you want by going to the Try Kotlin website and going through the exercises there.
恭喜,您已成功在系統上設置Kotlin編譯器和開發環境。 我們將在本指南中介紹Kotlin的所有復雜之處和有趣的部分,但是如果您愿意,可以直接訪問Try Kotlin網站并在那里進行練習,從而搶先一步。
文獻資料 (Documentation)
One of the greatest things about Kotlin is it’s comprehensive and well structured documentation. Even if you are new to programming, you will find yourself right at home with the docs. They do a pretty amazing job at laying it all out in a well structured manner. You can check out the official documentation at this link.
關于Kotlin的最大優點之一就是它的內容全面且結構合理。 即使您不熟悉編程,也可以在文檔中找到適合自己的地方。 他們以結構良好的方式將所有內容布置得非常出色。 您可以在此鏈接中查看官方文檔。
有關Kotlin的更多信息 (More info on Kotlin)
Develop native Android apps with Kotlin - Full Course
使用Kotlin開發本機Android應用-完整課程
Why you should try Kotlin instead of Java
為什么您應該嘗試Kotlin而不是Java
How to build an Android messenger app with Kotlin
如何使用Kotlin構建Android Messenger應用
翻譯自: https://www.freecodecamp.org/news/kotlin-programming-basics-for-beginners/
kotlin編程語言