Overview of Kotlin & Comparison Between Kotlin and Java

Kotlin M14 发布,此版本主要改进如下:

by Navdeep Singh Gill

  • 支持文件类注解

  • 标准库新增 Java API

  • 操作符新增 operator modifier

  • 可以通过合成 field 变量访问 Backing fields

Kotlin is a new programming language from JetBrains. It first appeared
in 2011 when JetBrains unveiled their project named “Kotlin”. Kotlin is
an Open-Source Language.

语言改进

  • Backing fields

  • Operators

  • Compile-time constants

  • Annotate file classes

  • 其他语言改进

    • private on the top level is now private to file

    • internal is checked in the compiler (not only IDE)

    • private in interfaces is truly private now

    • equals in data classes compares arrays by calling their
      .equals() method (which works by identity)

    • lateinit val‘s are prohibited

    • many cases of inheritance and other degrees of freedom are
      prohibited for data classed (see this blog
      post)

    • protected and internal members are prohibited in interfaces

    • _, __, ___ are forbidden as in identifiers, i.e. we can
      use _foo, but not _ alone (reserved for future use)

    • identityEquals() function is deprecated in favor of ===

Basically like Java, C and C++ – Kotlin is also “statically typed
programming language”. Statically typed programming languages are those
languages in which variables need not be defined before they are used.
This means that static typing has to do with the explicit declaration or
initialization of variables before they are employed.

标准库改进

  • ArraysKt — operations on arrays, extensions for arrays, array
    factory methods

  • CharsKt — extensions for Char and Char.Companion, most of them
    should be hidden

  • CollectionsKt — operations on iterables, collections and lists, list
    factory methods

  • ComparisonsKt — operations on comparators, comparator factory
    methods, and functions for performing comparisons

As Earlier said that Java is an example of a statically typed language,
similarly C and C++ are also statically typed languages.

IDE 改进

As usual, the IDE helps you migrate seamlessly from M13 via Code
cleanup
. Also there are several new handy features in M14:

  • As mentioned above, there in some cases we need private backing
    properties. You can easily introduce them via intention action:
    澳门新葡亰游戏网址 1

  • Also you can move property initialization from constructor body or
    initializer block to property declaration:
    澳门新葡亰游戏网址 2

  • One of the long-expected features is completion for overriding
    functions and properties:
    澳门新葡亰游戏网址 3

  • Since M13 the IDE optimizes imports on the fly. Add unambiguous
    imports on the fly
    feature is also supported now. Give it a try:
    澳门新葡亰游戏网址 4

Basically, Static typing does not mean that we have to declare all the
variables first before we use them. Variables may be initialized
anywhere in the program and we (developers) have to do so, to use those
variables anywhere in the program when there is a need. Consider the
following example

安装

IntelliJ IDEA 15 Public
Preview
包含 Kotlin M14
绑定。

更多内容请看发行说明。

Kotlin 是一个基于 JVM 的新的编程语言,由
JetBrains 开发。

其主要设计目标:

  • 创建一种兼容Java的语言

  • 让它比Java更安全,能够静态检测常见的陷阱。如:引用空指针

  • 让 它比Java更简洁,通过支持variable type inference,higher-order
    functions (closures),extension functions,mixins and first-class
    delegation等实现。

  • 让它比最成熟的竞争对手Scala语言更加简单。

文章转载自:开源中国社区 []    

澳门新葡亰游戏网址 5image

In addition to the classes and methods of object-oriented programming,
Kotlin also supports procedural programming with the use of functions.

Like in Java, C and C++, the entry point to a Kotlin program is a
function named “main”. Basically, it passed an array containing any
command line arguments. Consider the following example –

澳门新葡亰游戏网址 6image

Filename extensions of the Java are .java, .class, .jar but on the other
hand filename extensions of the Kotlin are .kt and .kts.

澳门新葡亰游戏网址 7image

Benefits of Kotlin Language

  • 澳门新葡亰游戏网址,Kotlin compiles to JVM bytecode or JavaScript – Like Java, Bytecode
    is the compiled format for Kotlin programs also. Bytecode means
    Programming code that, once compiled, is run through a virtual
    machine instead of the computer’s processor. By using this approach,
    source code can be run on any platform once it has been compiled and
    run through the virtual machine. Once a kotlin program has been
    converted to bytecode, it can be transferred across a network and
    executed by JVM(Java Virtual Machine).

  • Kotlin programs can use all existing Java Frameworks and Libraries –
    Yes, it’s true that Kotlin programs can use all existing java
    frameworks and libraries, even advanced frameworks that rely on
    annotation processing. The main important thing about kotlin
    language is that it can easily integrate with Maven, Gradle and
    other build systems.

  • Kotlin can be learned easily and it is approachable. It can be
    learned easily by simply reading the language reference.The syntax
    is clean and intuitive(easy to use and understand). Kotlin looks a
    lot like Scala but is simpler.

  • Kotlin is Open Source and it costs nothing to adopt.

  • Automatic conversion of Java to Kotlin – JetBrains integrated a new
    feature into IntelliJ which converts Java to Kotlin and saves a huge
    amount of time. And it also saves us to retype mundane code.

  • Kotlin’s null-safety is great – Now get rid of
    NullPointerExceptions. This type of system helps us to avoid null
    pointer exceptions. In Kotlin the system simply refuses to compile
    code that tries to assign or return null. Consider the following
    example

val name: String = null // tries to assign null, won’t compile. fun getName(): String = null // tries to return null, won’t compile.
  • Code reviews are not a problem – Kotlin is much more focuses on
    readable syntax so code reviews are not a problem, they can still be
    done by those team members who are not familiar with the language.

  • The billion dollar mistake made right. As already mentioned above
    that Kotlin avoids the null pointer exception. If we try to assign
    or return null to a variable or function respectively, then it won’t
    compile.

But in some special cases if we need nullability in our program then we
have to ask Kotlin very nicely. Every Nullable type require some special
care and treatment. We can’t treat them the same way as non-nullable
types and this is a very good thing.

We have to add “?” after the variable type. Consider the following
example – Kotlin also fails at compile-time whenever a
NullPointerException may be thrown at run-time. Consider the following
example –

澳门新葡亰游戏网址 8image

  • Versatile

澳门新葡亰游戏网址 9image

  • Lean Syntax and Concise – One liner functions take one line, simple
    structs/JavaBeans can also be declared in one line. Real properties
    generate getters and setters behind the scenes for Java interop. And
    Adding the data annotation to a class triggers autogeneration of
    boilerplate like equals, hashCode, toString and much more.

Consider the following example

/* Java program */public class Address { private String street; private int streetNumber; private String postCode; private String city; private Country country; public Address(String street, int streetNumber, String postCode, String city, Country country) { this.street = street; this.streetNumber = streetNumber; this.postCode = postCode; this.city = city; this.country = country; } @Override public boolean equals { if (this == o) return true; if (o == null || getClass() != o.getClass return false; Address address =  o; if (streetNumber != address.streetNumber) return false; if (!street.equals(address.street)) return false; if (!postCode.equals(address.postCode)) return false; if (!city.equals(address.city)) return false; return country == address.country; } @Override public int hashCode() { int result = street.hashCode(); result = 31 * result + streetNumber; result = 31 * result + postCode.hashCode(); result = 31 * result + city.hashCode(); result = 31 * result + (country != null ? country.hashCode; return result; } @Override public String toString() { return "Address{" + "street='" + street + ''' + ", streetNumber=" + streetNumber + ", postCode='" + postCode + ''' + ", city='" + city + ''' + ", country=" + country + '}'; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public int getStreetNumber() { return streetNumber; } public void setStreetNumber(int streetNumber) { this.streetNumber = streetNumber; } public String getPostCode() { return postCode; } public void setPostCode(String postCode) { this.postCode = postCode; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public Country getCountry() { return country; } public void setCountry(Country country) { this.country = country; }}

澳门新葡亰游戏网址 10image

You May Also Love To Read Deploying Kotlin Application on Docker &
Kubernetes

We were actually very much interested in knowing the compilation speed
of Kotlin as compared to Java.

澳门新葡亰游戏网址 11image

  • Null Safety – As already mentioned in above section that Kotlin
    avoids NullPointerException. Kotlin fails at compile-time whenever a
    NullPointerException may be thrown.

  • Data Classes – In Kotlin there are Data Classes which leads to
    autogeneration of boilerplate like equals, hashCode, toString,
    getters/setters and much more. Consider the following example –

    澳门新葡亰游戏网址 12image

But in Kotlin the above same class can define concisely in one line

/* kotlin Code */

网站地图xml地图