Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Moaml Mohmmed

Rating No ratings yet

Google promoted Kotlin as a first class language on its Java-based Android platform back in May. Since then, the whole development world has been wondering: what is this language? Kotlin has been around for a few years and has been running on production systems, after the languages 1.0 release in February 2016, for a year or so. The language has received a lot of praise and loving words from the developer community. It is a breath of fresh air, a good upgrade to systems running older versions of Java, and still somehow an old dog in a familiar playing field. What is Kotlin? What does it bring that the JVM doesn't already have? Kotlin vs. Java There are a few approaches we can take when introducing Kotlin. We can discuss it through Java, the language Kotlin needs to be based on due to its JVM runtime, or we can do it through Scala, the language Kotlin is heavily influenced by. There is no doubt that Kotlin is better than Java. It is much safer and more concise. It provides you with a bunch of additions to your standard Java language and enhances a few bits and pieces that Java developers have grown to dislike. Additions include things like null safety, extension functions, data classes, objects, first class functions as well as extensive and expressive lambdas. Kotlin also enhances Java’s type inference and type system and takes massive leaps forward with collections. Kotlin vs. Scala Perhaps, it’s better to compare Kotlin against Scala. This comparison might scare some of you quite a bit because Scala has the reputation of being simultaneously intriguing and frightening. It heavily introduces functional programming paradigm to you while still mixing it into familiar object orientation (hence in an awfully lot of cases creating a mishmash of advanced techniques from both paradigms), brings in some new build tools, and gives your internal flow state a frustrating break every now and then due to long compile times. I come bearing both good news and bad news. Let’s star

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical, step-by-step introduction to Kotlin for programmers coming from Java or other JVM languages, covering syntax, object-oriented features, and functional additions—ideal for Android developers or backend engineers wanting a concise, safer alternative to Java. 【Book Arc】 - **Opening (~0%–4%)**: Introduces Kotlin's position in the JVM ecosystem, comparing it to Java (safer, more concise) and Scala (less complex, better tooling). Sets expectations for a language that's 100% Java-compatible but adds modern features like null safety, extension functions, and lambdas. - **Early (~4%–12%)**: Covers fundamental data types, type conversions, and string handling. Highlights Kotlin's improvements over Java, such as string interpolation and cleaner value object declarations, while explaining numeric type conversions and floating-point comparison rules. - **Early (~12%–20%)**: Moves into arrays and collections, emphasizing Kotlin's invariant arrays (unlike Java) and primitive-type arrays (IntArray, etc.) to avoid boxing. Also introduces string templates and shows real-world adoption examples like Corda and JetBrains Account. - **Early (~20%–28%)**: Explains control flow: `if` as an expression, `when` as a powerful replacement for switch, and `for` loops with iterators. Emphasizes Kotlin's expression-oriented design, where conditions return values, reducing boilerplate. - **Early (~28%–36%)**: Delves into classes and constructors, covering primary/secondary constructors, `init` blocks, and default parameter values. Shows how to create instances without `new` and introduces inheritance with `open`/`override` keywords, plus property overriding rules. - **Middle (~36%–52%)**: Explores advanced class features: abstract classes, property getters/setters, compile-time constants (`const`), and delegated properties. Then covers extension functions/properties (adding methods to existing types), companion objects (static-like members), and object expressions for anonymous classes—key Kotlin features for cleaner, more expressive code. 【Key Takeaways】 - **Kotlin is a safer, more concise Java** (Opening): It adds null safety, extension functions, data classes, and expressive lambdas while remaining 100% Java-compatible. This makes it a low-risk upgrade for existing JVM projects. - **String interpolation simplifies text handling** (Early): Using `$variable` or `${expression}` inside strings eliminates Java's cumbersome concatenation. This is a small but daily productivity win for developers. - **Arrays are invariant, preventing runtime failures** (Early): Unlike Java, `Array<String>` cannot be assigned to `Array<Any>`, catching type errors at compile time. Use `Array<out Any>` for covariance, and primitive arrays like `IntArray` to avoid boxing overhead. - **`if` and `when` are expressions, not statements** (Early): They return values, so you can replace Java's ternary operator and verbose switch statements with cleaner, more readable code. This reduces boilerplate and improves clarity. - **Classes use `open`/`override` explicitly** (Early): Unlike Java's default-open classes, Kotlin requires marking classes and methods as `open` to allow inheritance. This prevents accidental overrides and makes the design intent explicit. - **Extension functions add methods without inheritance** (Middle): You can add `swap()` to `MutableList<T>` or define custom properties, avoiding decorator patterns and keeping code organized. This is a powerful tool for enhancing third-party libraries. - **Companion objects replace static members** (Middle): They allow static-like access while remaining real objects that can implement interfaces. Use `@JvmStatic` for true static methods on the JVM, bridging Kotlin and Java interop. 【Reading Tips】 - **Skim the Kotlin vs. Java/Scala comparisons** (Opening): These are motivational and contextual; focus on the bullet lists of Kotlin's advantages rather than the philosophical discussion. - **Deep-read the data types and arrays sections** (Early): These are foundational and include subtle rules (e.g., floating-point comparisons, array invariance) that will trip you up later. Take notes on type conversion functions like `toByte()`, `toShort()`. - **Practice the control flow examples** (Early): The `when` expression and `if`-as-expression are core to idiomatic Kotlin. Rewrite Java switch statements in Kotlin to internalize the syntax. - **Pay attention to class inheritance rules** (Early): The `open`/`override`/`final` keywords are non-negotiable for correct code. Work through the examples with `super` calls and multiple inheritance to avoid confusion. - **Treat extension functions as a highlight** (Middle): This is where Kotlin shines. Experiment by adding a utility function to a built-in type (e.g., `List<T>.lastIndex`) to see how it improves code readability. 【Coverage Limits】 This guide covers the first half of the book (up to ~52%), focusing on syntax, types, control flow, classes, and extensions. Excerpts do not cover later topics like coroutines, collections API details, or advanced functional programming patterns; those would require reading the full book.
Page 7
of advanced techniques from both paradigms), brings in some new build tools, and gives your internal flow state a frustrating break every now and then due to...
View in text
Excerpt 2
Chapter III the conditions As in any programming language, Kotlin contains expressions to control flow: if, when, loop, and while. It also supports the words...
View in text
Excerpt 3
mplementation of any other element of the type of overtrid). To avoid this, elements of the open type must not be defined in the base class in any of the Or...
View in text
Excerpt 4
for additional properties Companion Object Extensions When a class contains an object, it can then define additional functions and properties for that object...
View in text
Excerpt 5
prop: KProperty <*> ): ReadOnlyProperty <MyUI, T> { checkProperty (thisRef, prop.name) // Create circular return ResourceDelegate () } private fun checkPrope...
View in text
Excerpt 6
to return these functions to static types, such as: (It is good to use Set instead of HashSet and use the function that returns the static type List <T> inst...
View in text
Excerpt 7
rovides such additions: operator fun <K, V> Map <K, V> .iterator (): Iterator <Map.Entry <K, V >> = entrySet () .iterator () operator fun <K, V> Map.Entry <K...
View in text
Excerpt 8
or not. Exceptions in Kotlin present it with a simple example; the following code is from the JDK interface that is implemented in the StringBuilder class: A...
View in text
Tags
AI categories
Programming Languagekotlinandroid
ISBN: B08B9RKL74
Publisher: Mem lnc
Publish Year: 2020
Language: English
Pages: 176
File Format: PDF
File Size: 519.0 KB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…