kotlin_Kotlin

kotlin

Ever since Google has made Kotlin an official language for Android, the programming language is becoming increasingly popular among developers all around the globe. Some of the questions are:

自从Google使Kotlin成为Android的官方语言以来,该编程语言就在全球开发人员中越来越受欢迎。 一些问题是:

  • What’s new in Kotlin?

    Kotlin有什么新功能?
  • Who introduced Kotlin programming language?

    谁介绍了Kotlin编程语言?
  • What’s so special in Kotlin that developers are migrating from Java to Kotlin quickly?

    Kotlin有什么特别之处,以至于开发人员Swift从Java迁移到Kotlin?
  • Why are Android Developers adopting it so quickly in their projects?

    为什么Android开发人员在他们的项目中如此Swift地采用它?

In this tutorial, we’ll be answering all these questions and discuss the nitty gritty details from the beautiful world of Kotlin.

在本教程中,我们将回答所有这些问题,并讨论来自美丽的Kotlin世界的细节。

Kotlin (Kotlin)

  • Kotlin came up in 2011 from a team of developers at JetBrains.

    Kotlin于2011年从JetBrains的一个开发团队中诞生。
  • It’s a statically typed language for modern multi-platform applications.

    它是用于现代多平台应用程序的静态类型语言。
  • Kotlin runs on the Java Virtual Machine and compiles to the JVM Bytecode. It can also be compiled to the Javascript source code. Thus, Kotlin is 100% interoperable with Java. This means that you can call Kotlin code in Java and vice-versa.

    Kotlin在Java虚拟机上运行,​​并编译为JVM字节码。 也可以将其编译为Javascript源代码。 因此,Kotlin可与Java 100%互操作。 这意味着您可以用Java调用Kotlin代码,反之亦然。
  • Interoperability feature is one of the major reasons that’s led to the wide adoption of the kotlin programming language.

    互操作性功能是导致广泛采用kotlin编程语言的主要原因之一。

为什么要从Java迁移到Kotlin? (Why migrate from Java to Kotlin?)

  • Kotlin has a smart concise syntax that helps in reducing the boilerplate code and improving the readability. On contrast, Java is more verbose.

    Kotlin的语法简洁明了,有助于减少样板代码并提高可读性。 相反,Java更冗长。
  • Kotlin is type safe compared to Java. It provides a built-in nullability checker for its types (Similar to Optional in Swift).

    与Java相比,Kotlin是类型安全的。 它为其类型提供了内置的可空性检查器(类似于Swift中的Optional )。
  • Kotlin has a lean, intuitive and familiar syntax which is easy to master quickly (No semicolons. That’s a blessing!).

    Kotlin具有精简,直观和熟悉的语法,易于快速掌握(无需分号。这真是太幸运了!)。
  • Kotlin is easy to adopt. All the existing Java frameworks are available for use.

    Kotlin易于采用。 所有现有的Java框架都可以使用。

为什么Android开发人员会迁移到Kotlin? (Why are Android Developers migrating to Kotlin?)

Besides the above-mentioned pros of Kotlin over Java, two major reasons that are causing the adopting of Kotlin for Android Development are:

除了上述Kotlin相对于Java的优点外,导致Kotlin被Android开发所采用的两个主要原因还包括:

  1. 功能编程 (Functional Programming)

  2. It’s time that the Android Developers start reaping the benefits of Functional Programming.

    是时候让Android开发人员开始收获函数式编程的好处了。

    Hold on, Java 8 supports Functional Programming through lambda expressions. So why use Kotlin instead?

    等等, Java 8通过lambda表达式 支持功能编程 。 那为什么要用Kotlin代替呢?

    As of writing this tutorial, Android fully supports Java 6 only. Java 7 is used since Android API 19. But still more than 50% devices currently are API<19. We’re unsure when would Java 8 support 100% Android devices. This is where Kotlin comes to our rescue. It’s independent of the Android OS. Hence, Kotlin can be updated anytime and the latest features can be used anytime unlike Java.

    在撰写本教程时,Android仅完全支持Java 6。 从Android API 19开始使用Java7。但是,目前仍有50%以上的设备使用API​​ <19。 我们不确定Java 8何时会支持100%的Android设备。 这就是Kotlin进行救援的地方。 它独立于Android OS。 因此,与Java不同,可以随时更新Kotlin,并且可以随时使用最新功能。

  3. 简化的锅炉板代码 (Reduced Boiler Plate Code)

  4. Thanks to the short and concise syntax in Kotlin, it reduces our boilerplate code in Android.

    由于Kotlin中语法简洁明了,因此减少了我们在Android中的样板代码。

    Sooner you’ll see that the following Java code can be reduced to a much shorter Kotlin code.

    很快您就会发现,以下Java代码可以简化为更短的Kotlin代码。

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //handle click code goes here
        }
    });
    view.setOnClickListener({//handle click code goes here})

Kotlin编程入门 (Getting Started with Kotlin Programming)

There are a few ways to run your Kotlin program:

有几种方法可以运行您的Kotlin程序:

Download the IntelliJ Community version and get started with your Hello World Kotlin Program below.

下载IntelliJ社区版本,并从下面的Hello World Kotlin程序开始使用。

kotlin program type

Select Kotlin/JVM

选择Kotlin / JVM

kotlin filename

Goto src in the project structure. Right click and select New Kotlin File

在项目结构中转到src。 右键单击并选择“新建Kotlin文件”

Having created the HelloWorld.kt file, let’s write our first program.

创建HelloWorld.kt文件后,让我们编写第一个程序。

fun main(args: Array<String>)
{
    println("Hello World Kotlin")
}

There’s the Kotlin syntax. Don’t worry, we’ll be covering each of the elements of Kotlin at length in the following tutorials. For now, following are important points to remember.

有Kotlin语法。 不用担心,我们将在以下教程中详细介绍Kotlin的每个元素。 目前,以下是要记住的重要点。

  • fun keyword is used to define a function.

    fun关键字用于定义函数。
  • The parameter name is followed by a colon and its type.

    参数名称后跟一个冒号及其类型。
  • println() function is used to print the input on the console.

    println()函数用于在控制台上打印输入。
  • To run the code, goto the Run dropdown menu. Alternatively, you can click the icon besides the function to run the program too as shown below.
    kotlin hello world program run

    Click on the Kotlin icon

    Once the output gets printed, a folder out gets automatically created in our project structure as shown below.

    HelloWorldKt.class gets created

    要运行代码,请转到“运行”下拉菜单。 或者,您也可以单击功能旁边的图标来运行程序,如下所示。
    Kotlin你好世界程序运行

    单击Kotlin图标

    一旦输出获取打印,文件夹out获取自动在我们的项目结构如下图所示创建的。

    创建HelloWorldKt.class

  • The Kotlin Compiler (kotlinc) converts the .kt file into a .class file. The .class file is then run by the JVM.

    Kotlin编译器( kotlinc )将.kt文件转换为.class文件。 然后,.class文件由JVM运行。
  • Notice that the HelloWorldKt.class file gets automatically created by the Kotlin Compiler (You can find the same from Run->Edit Configurations too).

    The above executed kotlin code is equivalent to the Java code below.

    public class TestJava{
        public static void main(String[] args)
        {
            System.out.println("Hello World Java");
        }
    }

    注意, HelloWorldKt.class文件由Kotlin编译器自动创建(您也可以从“运行”->“编辑配置”中找到相同的文件)。

    上面执行的kotlin代码等效于下面的Java代码。

  • A striking difference between the Kotlin main function and the Java one is: You don’t need to explicitly define the main function inside a class in Kotlin. Kotlin covers that stuff for you, unlike Java.

    Kotlin主函数和Java函数之间的一个显着区别是: 您不需要在Kotlin的类中显式定义main函数。 Kotlin为您提供了与Java不同的东西

Isn’t the Kotlin code so short and concise!
Note: We haven’t imported the Array clss in the HelloWorld.kt file. That’s because the Kotlin standard library comes up with a bunch of default imports.

Kotlin代码不是那么简短!
注意 :我们尚未在HelloWorld.kt文件中导入Array clss。 那是因为Kotlin标准库附带了一堆默认导入。

The following section demonstrates setting up a Kotlin application in Android Studio.

下一节演示了如何在Android Studio中设置Kotlin应用程序。

Android Studio Kotlin (Android Studio Kotlin)

对于Android Studio版本<3.0 (For Android Studio version < 3.0)

You’ll need to install the Kotlin plugin. Goto Android Studio Preferences > Plugins > Browse Repositories > search for “Kotlin”.

您需要安装Kotlin插件。 转到Android Studio偏好设置>插件>浏览存储库>搜索“ Kotlin”。

Once the plugin is installed, goto File > Create a New Module > Choose Java library.

安装插件后,转到文件>创建新模块>选择Java库。

kotlin android studio create new module

Select Java Library

选择Java库

Create a new Kotlin file/class in the newly created Module. Your Project Structure should look like this:

在新创建的模块中创建一个新的Kotlin文件/类。 您的项目结构应如下所示:

Your Android Studio IDE should show a Configure option, since the Kotlin plugin hasn’t been configured in our module yet. Alternatively goto Tools -> Kotlin -> Configure Kotlin in Project would also do.

您的Android Studio IDE应该显示“配置”选项,因为尚未在我们的模块中配置Kotlin插件。 另外,转到Project-> Kotlin->在Project中配置Kotlin也可以。

Your Module build.gradle file would look like this:

您的Module build.gradle文件如下所示:

apply plugin: 'java'
apply plugin: 'kotlin'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"
buildscript {
    ext.kotlin_version = '1.1.60'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
repositories {
    mavenCentral()
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

Now that Kotlin is configured. You need to Edit the Build configurations as shown below:

现在,已经配置了Kotlin。 您需要编辑构建配置,如下所示:

Add a new Application configuration from the symbol +. Enter the relevant class and module names as shown below.

从符号+添加新的应用程序配置。 输入相关的类和模块名称,如下所示。

kotlin android studio set configurations

Select Class and Module Name

选择类和模块名称

Our Kotlin application is now ready to run. You can perform the same operation to create a build configuration for the Java application.

我们的Kotlin应用程序现在可以运行了。 您可以执行相同的操作来为Java应用程序创建构建配置。

Kotlin适用于Android Studio 3.0 (Kotlin for Android Studio 3.0)

Kotlin plugin is already present in Android Studio 3.0. You just need to select “Include Kotlin Support” whilst creating a new project to have Kotlin configured throughout the project.

Kotlin插件已经存在于Android Studio 3.0中。 您只需在创建新项目时选择“包括Kotlin支持”,即可在整个项目中配置Kotlin。

This brings an end to brief introduction of Kotlin programming language. There’s a lot of exciting stuff to do in Kotlin. We’ll be taking them one at a time. Stay tuned!

这样就结束了Kotlin编程语言的简要介绍。 Kotlin有很多令人兴奋的事情要做。 我们一次要带他们一个。 敬请关注!

翻译自: https://www.journaldev.com/16944/kotlin

kotlin

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值