为什么要使用Kotlin开始Android应用开发?

Currently, there are lots of JVM languages one can try to use while developing Android apps. Since the last decade, JAVA has been the only language that supports Google’s official IDEs to develop Android apps. Since JAVA is an old language, you may have come across a few obstacles while developing Android apps. Sometimes you may have felt a limitation on your creativity due to the restraints exercised by the language. To overcome those limitations, a new language, KOTLIN, is developed by JetBrains, which is more powerful yet simpler than JAVA.

当前,开发Android应用程序时可以尝试使用许多JVM语言。 自上个十年以来,JAVA一直是唯一支持Google官方IDE开发Android应用程序的语言。 由于JAVA是一门古老的语言,因此您在开发Android应用程序时可能会遇到一些障碍。 有时,由于语言的限制,您可能会感到自己的创造力受到限制。 为了克服这些限制,JetBrains开发了一种新的语言KOTLIN,它比JAVA功能更强大但更简单。

Kotlin has been receiving attention since Google announced its official support as a programming language. Kotlin also allows the developers to make use of Android Studio, Google’s primary IDE for Android.

自Google宣布将其作为编程语言正式支持以来,Kotlin一直受到关注。 Kotlin还允许开发人员使用Google Studio for Android的主要Studio Android Studio。

Many android developers were not convinced in switching from Java because of their expertise in that field. But when they started looking into Kotlin, they changed their minds and started using it for developing applications.

由于他们在该领域的专业知识,许多android开发人员不相信从Java转换。 但是当他们开始研究Kotlin时,他们改变了主意并开始将其用于开发应用程序。

什么是Kotlin? (What Is Kotlin?)

Kotlin is a programming language for the JVM and Android that combines Object-Oriented and Functional Programming features, which is for a general-purpose, statically typed pragmatic, and open-source. Its main focus is on interoperability, safety, clarity, and tooling support.

Kotlin是JVM和Android的一种编程语言,结合了面向对象和函数式编程功能,适用于通用,静态类型的实用型和开源程序。 它的主要重点是互操作性,安全性,清晰度和工具支持。

Kotlin has been developed as a solution to the problems that Android developers have faced over some time. It allows you to overcome obstacles most easily and quickly.

开发Kotlin是为了解决Android开发人员一段时间以来遇到的问题。 它使您能够最轻松,快速地克服障碍。

Kotlin与Java的好处 (Benefits Of Kotlin Vs Java)

简洁 (Concise)

Kotlin is more concise than Java because it solves problems within fewer lines of code. Enables a change in the code effectively and efficiently. This improves code maintenance and readability.

Kotlin比Java更简洁,因为它可以用更少的代码行解决问题。 有效且高效地启用代码更改。 这提高了代码维护和可读性。

更安全 (Safer)

It prevents common programming mistakes such as design and application crashes. In Java, certain error causes are more likely to occur again. Using this, write more robust code.

它可以防止常见的编程错误,例如设计和应用程序崩溃。 在Java中,某些错误原因更有可能再次发生。 使用此代码,编写更健壮的代码。

更好地支持函数式编程 (Better Support For Functional Programming)

Functional programming is a development pattern that allows programmers to solve problems more consistently and easily. Performance can be improved using inlining which is not possible in Java.

函数式编程是一种开发模式,使程序员可以更一致,更轻松地解决问题。 使用内联可以提高性能,这在Java中是不可能的。

减少代码中的错误和错误 (Reduce Errors And Bugs In The Code)

Kotlin compilers’ main focus is to fail — fast whenever possible. Kotlin compiler performs many checks, the effort of error fixes place, and avoiding runtime errors.

Kotlin编译器的主要重点是失败-尽可能快地失败。 Kotlin编译器会执行许多检查,以更正错误的位置,并避免运行时错误。

使用Kotlin开始Android应用开发的其他主要优势 (Other Key Advantages To Start Android App Development With Kotlin)

  • Open Source and provides an easier way to develop android apps.

    开源,并提供了一种开发Android应用程序的简便方法。
  • Kotlin Programs use JAVA frameworks and libraries.

    Kotlin程序使用JAVA框架和库。
  • Very easy to learn and understand.

    非常容易学习和理解。
  • Adopting Kotlin is low risk.

    采用Kotlin的风险很低。
  • Fewer Errors.

    错误更少。
  • Null Safety.

    空安全性。
  • Kotlin supports multiple platforms.

    Kotlin支持多种平台。
  • Kotlin is having very crystal & intuitive syntax.

    Kotlin具有非常清晰和直观的语法。
  • Sophisticated language with great IDE support.

    具有强大的IDE支持的复杂语言。
  • It makes developers more versatile

    它使开发人员更加灵活

Kotlin的目标平台 (Target Platforms For Kotlin)

Kotlin is used for the following types of development: server-side, client-side web, Android, and native code. A recent survey conducted after the release of Kotlin recorded the response from developers who counted Kotlin among the top three programming languages. It indicated that 66% of them used Kotlin for Android, 57% used Kotlin for JVM, 8% used Kotlin for Native, and 5% used Kotlin for JavaScript.

Kotlin用于以下类型的开发:服务器端,客户端Web,Android和本机代码。 Kotlin发布后进行的一项最新调查记录了将Kotlin列为编程语言前三名的开发人员的反馈。 结果表明,其中66%的人将Kotlin用于Android,57%的人将Kotlin用于JVM,8%的人将Kotlin用于本机,5%的人将Kotlin用于JavaScript。

The developers who used Kotlin for Native said they targeted Linux, Android, macOS,watchOS, tvOS, Windows, iOS, WebAssembly, and embedded systems.

使用Kotlin作为Native的开发人员表示,他们的目标是Linux,Android,macOS,watchOS,tvOS,Windows,iOS,WebAssembly和嵌入式系统。

在Kotlin中编程 (Programming In Kotlin)

To begin with, Kotlin allows top-level functions. It supports higher-order functions, inline functions, generics, closures, anonymous functions, and tail recursion. All the features and functions of the programming languages are combined in Kotlin.

首先,Kotlin允许使用顶级功能。 它支持高阶函数,内联函数,泛型,闭包,匿名函数和尾递归。 编程语言的所有特性和功能都在Kotlin中进行了组合。

As an alternative programming style, it preserves most of the object-oriented nature of Java, which is very easy while converting Java code. Kotlin has classes with constructors.

作为一种替代编程风格,它保留了Java的大多数面向对象的特性,这在转换Java代码时非常容易。 Kotlin的类具有构造函数。

Kotlin has a single inheritance from the named superclass, and all Kotlin Classes are with default superclasses, which are not the same as Java classes.

Kotlin从命名的超类继承一个继承,并且所有Kotlin类都具有默认的超类,这与Java类不同。

To allow other classes to inherit with kotlin classes, Kotlin classes have to be marked with an open keyword.

要允许其他类继承kotlin类,必须用open关键字标记Kotlin类。

变量声明 (Variable Declaration)

In Kotlin you can declare a variable as var x : Int = 10, or you can use the shorter but just as clear version var x = 10

在Kotlin中,您可以将变量声明为var x:Int = 10,或者可以使用较短但与之相同的版本var x = 10

For read-only variables, Kotlin has something called val declaration that is equivalent to Java variables. Once those variables are final, they cannot be reassigned.

对于只读变量,Kotlin有一个称为val声明的东西,它等同于Java变量。 一旦这些变量是最终变量,便无法重新分配它们。

字符串模板 (String Templates)

Strings contain something called template expressions; these are basically expressions which are evaluated with results inserted into the string.

字符串包含称为模板表达式的内容。 这些基本上是表达式,将结果插入字符串中进行评估。

A template expression consists of a simple name or probably an arbitrary expression that is enclosed in curly brackets. The code begins with a dollar sign ($).

模板表达式由一个简单名称或可能包含在大括号中的任意表达式组成。 该代码以美元符号($)开头。

println(“Name: $name, Department: $dept”)

println(“名称:$ name,部门:$ dept”)

扩展和实施 (Extends And Implements)

In JAVA, a class can extend another class and implement one or more interfaces. That concept is the same in Kotlin just syntax is different

在JAVA中,一个类可以扩展另一个类并实现一个或多个接口。 Kotlin中的概念相同,只是语法不同

class Study : Book, Comparable<Book>

课堂学习:书籍,可比<书籍>

‘if’ Statements And Expressions

“如果”的陈述和表达

In Kotlin, if statements can be used for control flow with Java, but it can also be used as an expression. Java’s cryptic ternary operator (?:) is replaced by the clearer but somewhat longer if expression.

在Kotlin中,if语句可用于Java的控制流,但也可以用作表达式。 Java的隐式三元运算符(?:)由更清晰的表达式代替,但更长。

val max = if (x >= y) then x else y

val max = if(x> = y)然后x else y

‘When’ Statement

“何时”声明

Kotlin allows a replacement of switch statements with a when statement. You can include ranges of values without Break Statements.

Kotlin允许将when语句替换为switch语句。 您可以在不使用Break语句的情况下包括值范围。

when (x) { in 1..10 -> print(“x is in the range”) in validNumbers -> print(“x is valid”) !in 10..20 -> print(“x is outside the range”) else -> print(“none of the above”) }

当(x){在1..10-> print(“ x在范围内”)在有效数字中-> print(“ x在有效范围内”)!在10..20-> print(“ x在范围之外”)else-> print(“以上都不是”)}

Kotlin和Java之间的区别 (Difference Between Kotlin And Java)

  • The main question of the developers about choosing Kotlin or Java as a programming language for Android. The shortest answer is that the Kotlin code is safer and more concise than Java code.

    开发人员有关选择Kotlin或Java作为Android编程语言的主要问题。 最简短的答案是Kotlin代码比Java代码更安全,更简洁。
  • Both Kotlin and Java files can coexist in Android Apps, so Kotlin is not only for new applications but also for expanding existing Java applications too.

    Kotlin和Java文件都可以共存于Android Apps中,因此Kotlin不仅适用于新应用程序,还适用于扩展现有Java应用程序。
  • The only argument I have ever seen for choosing Java over Kotlin would be for the case of complete Android development newbies.

    对于选择Java而不是Kotlin而言,我见过的唯一论点是针对完整的Android开发新手。
  • For anyone doing Android development, the advantages of Kotlin are compelling. The time for Java developers to learn Kotlin is a few hours. The basic estimate indicates approximately 40% cut in the numbers of lines in code in Kotlin than Java.

    对于任何从事Android开发的人来说 ,Kotlin的优势都是引人注目的。 Java开发人员学习Kotlin的时间是几个小时。 基本估计表明Kotlin中的代码行数比Java减少了40%。

Kotlin使用Java (Kotlin Working With Java)

You might be wondering about the ways in which Kotlin handles the results of Java calls, considering the differences in null handling and checked exceptions. Kotlin reliably infers on a platform type that behaves exactly like a Java type, it means that is it nullable, but it can generate null-pointer exceptions.

考虑到null处理和检查异常之间的差异,您可能想知道Kotlin处理Java调用结果的方式。 Kotlin可靠地推断出行为完全类似于Java类型的平台类型,这意味着它可以为空,但可以生成空指针异常。

Kotlin may also inject an assertion into the code at compile time to avoid aiming actual null pointer exceptions. There is no confirmed language notation for a platform type, but in the event, Kotlin has to declare a platform type such as an error message, it happens ! to the type.

Kotlin也可以在编译时将断言注入代码中,以避免针对实际的空指针异常。 平台类型没有确定的语言符号,但是在这种情况下,Kotlin必须声明平台类型,例如错误消息,它会发生! 的类型。

In most cases, calling Java code from Kotlin works in the way you might expect. Anytime both getters and setters exist in a Java class, Kotlin treats them as priorities with the same name. Similarly, Boolean accessor methods are treated as priorities that have the same name as the getter methods.

在大多数情况下,从Kotlin调用Java代码可以按您期望的方式工作。 每当Java类中同时存在getter和setter时,Kotlin会将它们视为具有相同名称的优先级。 同样,布尔访问器方法被视为优先级,与getter方法具有相同的名称。

For example

例如

import java.util.Calendar

导入java.util.Calendar

fun calendarDemo() {

有趣的calendarDemo(){

val calendar = Calendar.getInstance()

val calendar = Calendar.getInstance()

if (calendar.firstDayOfWeek == Calendar.SUNDAY) { // call getFirstDayOfWeek()

如果(calendar.firstDayOfWeek == Calendar.SUNDAY){//调用getFirstDayOfWeek()

calendar.firstDayOfWeek = Calendar.MONDAY // call setFirstDayOfWeek()

calendar.firstDayOfWeek = Calendar.MONDAY //调用setFirstDayOfWeek()

}

}

if (!calendar.isLenient) { // call isLenient()

如果(!calendar.isLenient){//调用isLenient()

calendar.isLenient = true // call setLenient()

calendar.isLenient = true //调用setLenient()

}

}

}

}

This trick break-down for the case of the Java set only properties, which are not supported in Kotlin. If Java class has only a setter properties, it will not be visible as a property in Kotlin.

对于仅Java设置属性的情况,此技巧细分,但Kotlin不支持。 如果Java类仅具有setter属性,则在Kotlin中它不会显示为属性。

It offers a bunch of useful features and provides null safety through nullable and non-nullable types. If you already know Java, you will be an expert in Kotlin very quickly.

它提供了许多有用的功能,并通过可空和不可空类型提供了空安全性。 如果您已经了解Java,那么您将很快成为Kotlin的专家。

将Java转换为Kotlin (Converting Java To Kotlin)

As Kotlin is receiving unstaggered growth, it may be possible that you will not get the solution / third party libraries developed with Kotlin easily as compared to JAVA. But not to worry about that, it’s very easy to convert JAVA code to Kotlin.

由于Kotlin的发展势头迅猛,因此与JAVA相比,您可能不会轻易获得使用Kotlin开发的解决方案/第三方库。 但不必担心,将JAVA代码转换为Kotlin非常容易。

Even if you are having applications developed with JAVA, and thinking to migrate JAVA

即使您正在使用JAVA开发应用程序,并考虑迁移JAVA

Kotlin, You Can Convert That Code By Following Just Simple Steps:

Kotlin,您可以按照以下简单步骤转换代码:

Image for post
Image for post

把它们加起来 (To Sum It Up)

Kotlin is a developer-friendly language that allows breathing space to developers to come up with unique solutions to regular problems.

Kotlin是一种对开发人员友好的语言,它为开发人员提供了喘息的空间,可以针对常规问题提出独特的解决方案。

With Google turning Kotlin-oriented, major developers and multinational companies are moving towards adopting it, since many Java apps are now being rewritten in Kotlin.

随着Google面向Kotlin的发展,主要的开发人员和跨国公司正朝着采用它的方向发展,因为现在许多Java应用程序已在Kotlin中进行了重写。

As above, with the tremendous features of Kotlin, at ZealousWeb, Mobile Application developers have started to Android app development with Kotlin. If you are an Android developer and passionate about your profession, this is a great chance for you to fire up creativity and develop innovative Android applications without any hurdles of the programming language.

如上所述,借助Kotlin的强大功能,在ZealousWeb上,移动应用程序开发人员已开始使用Kotlin进行Android应用程序开发 。 如果您是一位Android开发人员并且对您的职业充满热情,那么这是您激发创造力并开发创新的Android应用程序的绝佳机会,而无需任何编程语言的障碍。

Originally published at https://www.zealousweb.com on May 8, 2020.

最初于 2020年5月8日 发布在 https://www.zealousweb.com 上。

翻译自: https://medium.com/@ZealousWeb/why-should-you-start-android-app-development-with-kotlin-90e5416b4ac8

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值