kotlin編程語言_Kotlin初學者編程基礎

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編程語言

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390418.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390418.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390418.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

記一個蒟蒻的絕望

感覺現在…… 怎么講&#xff0c;心挺冷的。 今天一月五號了。距離省選&#xff0c;時間好短啊。 我還有那么多東西不懂。甚至聽都沒聽說過。 等到真正去省選的時候&#xff0c;我可能跟現在一樣&#xff0c;什么都不會。 我的名字能不能被看到都不知道。哈&#xff0c;還進隊呢…

671. 二叉樹中第二小的節點

給定一個非空特殊的二叉樹&#xff0c;每個節點都是正數&#xff0c;并且每個節點的子節點數量只能為 2 或 0。如果一個節點有兩個子節點的話&#xff0c;那么該節點的值等于兩個子節點中較小的一個。 更正式地說&#xff0c;root.val min(root.left.val, root.right.val) 總…

CentOS查詢端口占用和清除端口占用的程序

1、查詢端口號占用&#xff0c;根據端口查看進程信息 [rootserver2 ~]# lsof -i:80COMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEhttpd 5014 root 3u IPv4 14346 TCP server2:http (LISTEN)2、根據進程號查看進程對應的可執行程序 ps -f -p 進程號# p…

Android基礎夯實--你了解Handler有多少?

概述 對于剛入門的同學來說&#xff0c;往往都會對Handler比較迷茫&#xff0c;到底Handler是個什么樣的東西。當然&#xff0c;可能對于一些有工作經驗的工程師來說&#xff0c;他們也不一定能很準確地描述&#xff0c;我們來看下API的介紹。 Handler是用來結合線程的消息隊列…

spring與springBoot不同之處

( 1&#xff09;遵循“習慣優于配置”的原則&#xff0c;使用Spring Boot只需要很少的配置&#xff0c;大部分的時候我們直接使用默認的配置即可&#xff1b; &#xff08;2&#xff09;項目快速搭建&#xff0c;可以無需配置的自動整合第三方的框架&#xff1b; &#xff08;3…

sketch-a-net_Adobe XD,Sketch,Figma,InVision-如何在2020年選擇最佳設計軟件

sketch-a-netComparing Adobe XD vs Sketch vs Figma vs InVision studio is a very common topic among designers who are looking for the best design software. 在尋求最佳設計軟件的設計師中&#xff0c;比較Adobe XD&#xff0c;Sketch&#xff0c;Figma和InVision Stud…

merge intervals(合并間隔)

Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 題目沒有說所有間隔的start是依次增加的。所以&#xff0c;為了方便討論&#xff0c;我們要將所有間隔按照start升序排列。因…

劍指 Offer 49. 丑數

我們把只包含質因子 2、3 和 5 的數稱作丑數&#xff08;Ugly Number&#xff09;。求按從小到大的順序的第 n 個丑數。 示例: 輸入: n 10 輸出: 12 解釋: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 個丑數。 說明: 1 是丑數。n 不超過1690。 解題思路 使用小根堆&#xf…

維護舊項目_為什么您的舊版軟件難以維護-以及如何處理。

維護舊項目Believe it or not, some organizations still rely on legacy software to carry out operations even though newer and more versatile options are available. We know that “old is gold”, but legacy applications cannot glitter forever. As such, these o…

python--內置函數

內置函數現在python一共為我們提供了68個內置函數&#xff0c;講述過程&#xff1a;一、其他中的12個 &#xff08;一&#xff09;執行 字符串 類型代碼的執行 1 eval執行有意義的字符串 ,有返回值 print(eval(12))print(eval("print(美麗)")) #美麗 2 ex…

Nancy簡單實戰之NancyMusicStore(四):實現購物車

原文:Nancy簡單實戰之NancyMusicStore(四)&#xff1a;實現購物車前言 上一篇&#xff0c;我們完成了商品的詳情和商品的管理&#xff0c;這一篇我們來完成最后的一個購物車功能。 購物車&#xff0c;不外乎這幾個功能&#xff1a;添加商品到購物車&#xff0c;刪除購物車中的商…

劍指 Offer 32 - I. 從上到下打印二叉樹

從上到下打印出二叉樹的每個節點&#xff0c;同一層的節點按照從左到右的順序打印。 例如: 給定二叉樹: [3,9,20,null,null,15,7], 3/ \9 20/ \15 7返回&#xff1a; [3,9,20,15,7] 提示&#xff1a; 節點總數 < 1000 解題思路 使用隊列實現層序遍歷 代碼 /*** …

數據庫表命名 單數復數_數據是還是數據是? “數據”一詞是單數還是復數?

數據庫表命名 單數復數Ill cut right to the chase: the word "data" is plural. Its the plural form of Latin word "datum." Many data. One datum.我將緊追其后&#xff1a;“數據”一詞是復數形式。 它是拉丁文“基準”的復數形式。 許多數據。 一個基…

《七步掌握業務分析》讀書筆記六

分析技術和呈現格式 詞匯表 強有力溝通的一個重要內容是一致地使用術語和慣用語。每次談話都涉及對術語的共同理解。 工作流圖&#xff08;也稱為流程圖、UNL活動圖和過程圖&#xff09; 工作流程把一個或多個業務過程的細節可視化地呈現出來&#xff0c;以澄清理解或提出過程改…

Mysql數據庫--語句整理/提升/進階/高級使用技巧

一、基礎 1、說明&#xff1a;創建數據庫CREATE DATABASE database-name 2、說明&#xff1a;刪除數據庫drop database dbname3、說明&#xff1a;備份sql server--- 創建 備份數據的 deviceUSE masterEXEC sp_addumpdevice disk, testBack, c:\mssql7backup\MyNwind_1.dat--- …

1104. 二叉樹尋路

在一棵無限的二叉樹上&#xff0c;每個節點都有兩個子節點&#xff0c;樹中的節點 逐行 依次按 “之” 字形進行標記。 如下圖所示&#xff0c;在奇數行&#xff08;即&#xff0c;第一行、第三行、第五行……&#xff09;中&#xff0c;按從左到右的順序進行標記&#xff1b;…

javascript 代碼_如何開始對JavaScript代碼進行單元測試

javascript 代碼We all know we should write unit tests. But, its hard to know where to start and how much time to devote to tests compared to actual implementation. So, where to start? And is it just about testing code or do unit tests have other benefits?…

個人作業——軟件工程實踐總結作業

一、請回望暑假時的第一次作業&#xff0c;你對于軟件工程課程的想象 1&#xff09;對比開篇博客你對課程目標和期待&#xff0c;“希望通過實踐鍛煉&#xff0c;增強計算機專業的能力和就業競爭力”&#xff0c;對比目前的所學所練所得&#xff0c;在哪些方面達到了你的期待和…

(轉)在阿里,我們如何管理代碼分支?

阿里妹導讀&#xff1a;代碼分支模式的選擇并沒有絕對的正確和錯誤之分&#xff0c;關鍵是與項目的規模和發布節奏相匹配。阿里協同研發平臺在經過眾多實踐歷練后&#xff0c;總結出了一套獨創的分支管理方法&#xff1a;AoneFlow&#xff0c;通過兼備靈活高效與簡單實用的流程…

WIN10系統 截圖或者某些程序時屏幕會自動放大怎么辦

右擊這個應用程序&#xff0c;兼容性&#xff0c;以兼容模式運行&#xff0c;同時勾選高DPI設置時禁止顯示縮放即可