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.

KotlinJetbrains开发的一种编程语言,该公司是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)

翻译自: https://www.freecodecamp.org/news/kotlin-programming-basics-for-beginners/

kotlin编程语言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值