• 12 hours
  • Medium

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 12/12/19

Grasp the Kotlin origin story

Log in or subscribe for free to enjoy all this course has to offer!

By the end of this course, you'll marvel at all the possibilities Kotlin provides, especially compared to what Java offers.

I don’t understand. Why are you comparing it to Java? 

We are comparing Kotlin to Java because the two are 100% interoperable. This means that you can write source code both in Java and Kotlin on the same project. 😃 You can even call methods you've created using Kotlin within Java code (and vice versa). Pretty cool, huh?

The origins of Kotlin

Let’s go back to the dawn of time … well, almost! The idea for Kotlin took shape in the minds of a team of engineers at JetBrains way back in 2010.

Motivated by what they perceived to be Java’s limitations as they were developing their products, these engineers began seeking a viable long-term alternative—with one firm condition: any new language had to be 100% compatible with existing Java code.

That’s right:  it would have been inconceivable for JetBrains to go back to square one on all of its existing Java projects—they were huge (we’re talking several million lines of code)!

After trying other JVM-compatible languages like Scala (whose slow compile time became an obvious deficiency), JetBrains engineers decided to take a gamble and create their own language! 😄 #GoHardOrGoHome

Five years later, JetBrains open-sourced Kotlin’s first officially stable release (v1.0) on Github.

The philosophy behind Kotlin

Every great project is driven by a powerful core mission, and Kotlin is no exception. The goal behind Kotlin was to design a language that’s concise, safe, pragmatic, and 100% interoperable with Java.

Kotlin will make you a more productive programmer because it fixes a whole host of issues that have plagued Java developers for years. For example, Kotlin’s safety features make the Java NullPointerException much less likely to occur. And because Kotlin is fully compatible with Java, it can be used to create both native mobile applications in Android, and web applications like the J2EE platform, for example.

In short, wherever there is Java, Kotlin can be used. 👊

Wow, that’s great! But what does it look like under the hood? 🤔

Great question! When you program using Java, your class (.java) is compiled by the Java compiler (in your IDE), which transforms it into a file (.class) containing your compiled code (bytecode).

Using the new language, the code to be compiled is written in Kotlin, not Java. However, the bytecode compiled by the Kotlin compiler (kotlinc) will remain exactly the same!

Illustrates Compilation Process for Java and Kotlin
Kotlin compilation process

The advantages of a statically typed programming language

Like Java, Kotlin is a so-called statically typed programming language.

That means you can use the keyword "static," right? 😄

Wrong! That was a trick! 😊 In fact, a language is considered statically typed if the type (integer, string, boolean, etc.) of a program variable is known at compile time. Statically typed languages allow your IDE to tell you when it’s unhappy—for example when you assign a string to a number—by means of an error message indicating type mismatch.

Better to be alerted right away by your IDE than later by your crashing application, right?

private fun complexOperation() : Int {
return "0"
// Type mismatch.
// Required:Int
// Found: String
}

On the other hand, a language is considered dynamically typed if it is determined by run-time values. In other words, if you can define variables or functions that store or return data of any type, they will resolve only at the time of program execution. Dynamically typed languages speed up development time, because there is less code to write. However, they require more memory at run time to resolve the type and its variables in real time.

Ah, I get it! But why are you telling me this?

In their efforts to improve Kotlin’s readability, JetBrains developers were inspired by dynamic typing and integrated it into their programming language—even though the new language is actually statically typed. 😉

val zero = 0
val message = "Hello world !"
fun sayHello() = "hello everyone"

Nice, right? In fact, in Kotlin, everything is determined by the compiler, which automatically deduces your variable types. We call this type inference. And don’t worry, we’ll go into this in detail in the next chapter.

To sum up, a statically typed language such as Kotlin (or Java) offers several major advantages, including greater:

  • Performance:  Because types are known at compile time, there is no need to deduce them at runtime.

  • Reliability:  Because the compiler catches many more errors earlier in the development process—at compile time—the chances of a crash during execution are minimized.

  • Efficiency:  With statically typed languages, you can refactor much more easily, and also set up advanced tools such as autocomplete in your IDE.

The advantages of Kotlin as a functional language

If you have been using Java for a while, you will be familiar with object-oriented programming. Kotlin is an object-oriented language, too—with, however, the added advantage of also being a so-called functional language.

I’m guessing this does NOT mean that it’s a language that... functions? 😬

Here are a few of the features that define functional programming languages:

  • First-class functions:  Functional programming allows you to treat functions as values in your code. For example, you can store functions in variables, pass functions as parameters, and return functions! You’ll learn more about this in the chapter on lambdas. 😉

  • Immutability:  The functional programming approach encourages the use of immutable variables; that is, variables whose state cannot be changed after they are created. This technique improves performance and reliability by making the program resistant to problems such as multi-threading (accessing—and simultaneously modifying—the same variable in different threads). More on this in the next chapter.

  • Elimination of side effects:  Using only pure functions ensures that the returned result will always be the same. No matter how many times the function is called, it will modify only its own local variables, and there will therefore, be no undesirable (or unexpected) effects on global variables in your application. We will go into this further in the chapter on functions.

So are you beginning to glimpse the power and flexibility of this language? Well, hold on to your keyboard:  there’s more to come in the next chapter!

Conclusion:

  • Kotlin was developed by JetBrains to address the limitations it encountered with Java during the development of its various products.

  • The Kotlin programming language was designed to be concise, safe, pragmatic, and 100% interoperable with Java.

  • Kotlin is a statically typed programming language that supports type inference via its compiler.

  • Kotlin supports object-oriented programming and functional programming.

  • Kotlin is open source, supports most IDEs, and works on most current Java platforms (that accept the JVM bytecode).

Example of certificate of achievement
Example of certificate of achievement