Thursday 18 May 2017

About the Kotlin language for Java programmers from



This article talks about the programming language Kotlin. You will learn about the reasons for the appearance of the project, the possibilities of the language and see a few examples. The article is written primarily in the expectation that the reader is familiar with the programming language java, however, those who know another language can also get an idea of the subject. The article is superficial and does not address issues related to compiling into javascript. On the official website of the project you can find the full documentation, I'll try to talk about the language in brief.

about the project

Not so long ago, the company JetBrains, engaged in the creation of development environments, announced its new product - the programming language Kotlin. The company was attacked by a wave of criticism: critics suggested that the company come to its senses and finish the plug-in for Scala, instead of developing its own language. Developers on Scala really do not have a good development environment, but the developers can understand the problems of the plug-in: Scala, which was born thanks to researchers from Switzerland, absorbed many innovative scientific concepts and approaches, which made the creation of a good development tool a very difficult task. At the moment, the segment of modern languages with static typing for JVM is small, so the decision to create your own language along with the development environment to it looks very far-sighted. Even if this language does not take root at all in the community - JetBrains first of all makes it for their needs. Any java programmer can understand these needs: Java, as a language, develops very slowly, new features do not appear in the language (we are waiting for functions of the first order for several years already), compatibility with old versions of the language makes the appearance of many useful things impossible and in the near future Future (for example, a decent parameterization of types). For a software development company, the programming language is the main working tool, so the efficiency and simplicity of the language are the indicators on which not only the ease of development of tools for it depends but also the costs of the programmer for coding, that is, how easy it will be to accompany and Understand it.

About language

The language is statically typed. But compared to java, the Kotlin compiler adds information about the possibility of a link to null in the type, which tightens the type checking and makes the execution safer:

fun foo(text:String) { println(text.toLowerCase()) // NPE? Нет! } val str:String? = null // String? -- тип допускающий null-ы foo(str) // <- компилятор не пропустит такой вызов -- // тип str должен быть String, чтобы // передать его в foo

Despite the fact that such an approach can relieve the programmer of a number of problems associated with NPE, for a java programmer, it seems superfluous at first - you have to do extra checks or conversions. But after a while programming on kotlin, going back to java, you feel that you do not have this information about the type, you are thinking about using annotations Nullable / NotNull. There are also issues of backward compatibility with java - there is no such information in java bytecode, but as far as I know, this question is still in the process of solution, but for now, all java types coming from are nullable.

By the way, about backward compatibility: Kotlin is compiled into JVM bytecode (language developers spend a lot of effort in supporting compatibility), which makes it possible to use it in one project with java, and the ability to use java and Kotlin classes makes minimal the threshold of Kotlin integration into a large already existing one Java project. In this regard, it is important to use multiple java developments, creating a project entirely on kotlin. For example, it was almost not difficult for me to make a small project based on spring-web MVC.

Let's see a fragment of the controller:

path(array("/notes/")) controller class NotesController { private autowired val notesService : NotesService? = null path(array("all")) fun all() = render("notes/notes") { addObject("notes", notesService!!.all) } //... }

About the Kotlin language for Java programmers from
4/ 5
Oleh

Berlangganan

Suka dengan artikel di atas? Silakan berlangganan gratis via email