Fundamental Kotlin
Milo Vasi First Edition
www.fundamental-kotlin.com
Fundamental Kotlin by Milo Vasi Fall 2016: First Edition Copyright 2016 Milo Vasi All rights reserved. Published by Milo Vasi, ISBN: 978-86-920307-0-3 Website: www.fundamental-kotlin.com
About Fundamental Series
Fundamental Series is a serie of books with intention to bring readers quickly into book's subject and make it possible to relatively easy start working with matter.
Fundamental Kotlin is the first book in
Fundamental Series. It is available only as the electronic publication.
About me
I am a software engineer from Belgrade, Serbia. I am passionate about software development and ways to make it better.
When I am not on regular projects, I am spending time learning and investigating new technologies. One such technology is Kotlin. It gave me a lot of fun playing with Kotlin features and learning it. You can also find me here: - Linkedin: milos-vasic-53778682 - Xing: Milos_Vasic4 - Github: milos85vasic - Email:
Preface
In the 21st century with fast growth of computer technology developers are facing challenges more than ever. Existing programming languages are sometimes not good enough for them to fulfill mission best possible way. One such example is Java (but not the only one).
Even with Java 8, Java did not bring all modern features that developers in the 21st century expected. At the moment some new modern technologies are emerging. One of many is Swift, which is very popular. There are some other good examples: Ruby, Go, Haskel, Elixir, Scala as well as many others. Some programming languages that are not so new to developers like Erlang and Python gained certain popularity because of new trends. Each language offers some advantages.
However, there is still space for new languages to come. That is how Kotlin is born, powerful object-oriented programming language.
What is this book about?
Fundamental Kotlin is a book focused on Kotlin programming language and its comparison to Java. This book is focused on languages most important features and aspects. It will not go in too much theory since it's focused on exact examples with notes and code provided. All examples and code for this book are located here: http://static.fundamental-kotlin.com/code/Fundamental-Kotlin.zip Most of the book and its organisation are based on official language specifications and docs located on official Kotlin website: http://www.kotlinlang.org The reason why I decided to write this book was my strong impression with this language.
As I want as much as possible people to try it I decided to write this examples-driven book. If any of you who read notice anything that is not correct in the code examples please report it to me: . I will check and apply the changes in code if needed. Since this is a new technology for me too it is possible that something may come up. This is the first edition of the book and my plans for the second edition are to update examples, polish it and extend it. Everybody is wellcome to participate so if you have a better example (alternative) than the existing one please email me.
I will be more than happy to include your examples. I hope that you will find this book helpful and practical. Regards, Milo.
Who is this book for?
This book is for developers and for people who are on its way to become developers. It is essential to have at least some basic knowledge of computer programming, especially of Java. Fundamental Kotlin is imagined as guide to Kotlin for developers who spent some time on Java powered projects but it is not mandatory.
If you were programming for some time, it should be easy for you to follow this book.
What is Kotlin?
Kotlin is new programming language running on Java Virtual Machine, Android or browser. It is statically typed language and it can be compiled to JavaScript source code (running on browsers). Kotlin is:
Concise - drastically reduced code boilerplate
Safe - impossible to get
NullPointerExceptionVersatile - it is general-purpose programming language
Interoperable - we can use existing JVM libs and frameworks. A team at JetBrains (creators of IntelliJ Idea ) developed Kotlin, an OSS language with an army of external contributors.
Basic characteristics
Basic Kotlin characteristics are: 1.
Basic characteristics
Basic Kotlin characteristics are: 1.
Running on JVM (Java Virtual Machine) meaning that it is cross-platform compatible 2. Statically typed, the type of a variable is known at compile time 3. Functional: - First-class functions: You can store functions as a variables or pass as a parameters to other functions as a parameters - Immutability: It is guaranteed that state of an immutable object cant change over time - No side effects: Using pure functions that return the same result given the same inputs. Do not modify the state of other objects or interact with the outside world 4. Object-Oriented 5. Free and Open Source
Where is it used?
As we mentioned, Kotlin is general-purpose programming language.
That means that Kotlin can be used for many different needs like: Server-side development, here is the list of some web framework that you can try for web development: Kara - http://karaframework.com Wasabi - https://github.com/hhariri/wasabi Yested - http://www.yested.net Hexagon - https://github.com/jaguililla/hexagon and many others. Android mobile development, you can develop native Android applications from Kotlin.
Kotlin tools
Kotlin is very similar to Java, and so, as Java, Kotlin has compiler. Lets see now how build process is working in Kotlin. All source code is stored in
.kt files that are organized in packages. This is the same as in Java, the only difference is that Java uses .java extension. Kotlin compiler analyzes source code and generates
.class files (as Java does it too).
Then, .class files are packaged the way in which they are package depends on kind of project you are working on. Like this you can compile and run your code. For example create some .kt source code, then compile it using kotlinc, Kotlin compiler, and finally run it using Java: $ kotlinc First.kt -include-runtime -d First.jar $ java -jar First.jar
How to start?
We will show you how to get Kotlin running and start writing some code. It is very simple, especially if you already were working with Java and IntelliJ Idea. The previous example was executed using only Java and Kotlin compiler. To get it running and execute commands from example you need to install Java and Kotlin compiler.
Latest versions of both may be found on Orcale (Java) and Kotlinlang (Kotlin) websites. For more serious programming we will need an IDE. In this book (and probably in any future development) you will use IntelliJ Idea , IDE developed by creators of Kotlin, JetBrains Company. Download and install IntelliJ Idea Community Edition. When it's done, from IDE preferences install support for Kotlin language with all plugins that are coming with it.
Basics
To start writing some code, create new Kotlin project in IntelliJ: File New Project Then choose: Kotlin Kotlin (JVM) Next Name the project, set file system location and choose Java version from dropdown list (Project SDK).
Basics
To start writing some code, create new Kotlin project in IntelliJ: File New Project Then choose: Kotlin Kotlin (JVM) Next Name the project, set file system location and choose Java version from dropdown list (Project SDK).
After you click on Finish button new empty Kotlin project will be created. Now, create your first package, steps are the same, as you would do in Java. Right click on src folder in project structure tree New Package. Give some meaningful name to it like: net.milosvasic.fundamental.kotlin Finally, create your first Kotlin source code file by right clicking on a package you just created and choosing: New Kotlin File / Class. Give some name to a file, like for example:
Next page