Reader small image

You're reading from  Mastering Kotlin for Android 14

Product typeBook
Published inApr 2024
Reading LevelIntermediate
PublisherPackt
ISBN-139781837631711
Edition1st Edition
Languages
Right arrow
Author (1)
Harun Wangereka
Harun Wangereka
author image
Harun Wangereka

Harun Wangereka is a Google Developer Expert for Android and an Android engineer with over seven years of experience and currently working at Apollo Agriculture. Harun is passionate about Android development, never tired of learning, building the tech community, and helping other developers upscale their skills. He is a co-organizer at Droidcon Kenya and a former Kotlin Kenya and Android254 co-organizer. Through these communities, he has been able to impact thousands of developers. He is also an Android author at Kodeco where he has written 8 articles, published a book; Saving Data on Android, Second Edition, and is also a video course instructor. He has given numerous sessions on Android and Kotlin across different communities worldwide.
Read more about Harun Wangereka

Right arrow

Introduction to Kotlin

Kotlin is a language that runs on the Java Virtual Machine (JVM) developed by JetBrains. It was developed to overcome the following challenges that Java had:

  • Verbosity: Java has a very verbose syntax and this leads to developers writing a lot of boilerplate code even for trivial tasks.
  • Null pointer exceptions: By default, Java allows variables to have null values. This normally results in null pointer exceptions, which has been called the billion-dollar mistake in Java as many applications have been affected by this.
  • Concurency: Java has threads, but managing concurrency and thread safety can be such a hard task at times. This leads to a lot of performance and memory issues that seriously affect applications that need to do work off the main thread.
  • Slow adoption of features: The Java release cycle is slow and it is difficult to use the latest Java version to develop Android apps as there’s a lot to be done to ensure backward compatibility. This means it’s hard for Android developers to easily adopt the new language features and improvements as they’re stuck using older versions.
  • Lack of functional support: Java is not a functional language, which makes it hard for developers to write functional code in Java. It’s hard to employ features such as high-order functions or treat functions as first-class citizens.

Over the years, Kotlin has evolved to be multiplatform and server-side and not serviced, and is used in data science as well. Some of the features where Kotlin has an edge over Java are as follows:

  • Conciseness: The syntax is concise, which in turn reduces the amount of boilerplate code that you write.
  • Null safety: Many Java developers are very familiar with the famous Null Pointer Exception that was a source of many bugs and issues in applications. Kotlin was designed with null safety in mind. Variables that can have null values are indicated when declaring them, and before using these variables, the Kotlin compiler enforces checks for nullability, thereby reducing the number of exceptions and crashes.
  • Coroutines support: Kotlin has built-in support for Kotlin coroutines. Coroutines are lightweight threads that you can use to perform asynchronous operations. It’s easy to understand and use them in your applications.
  • Data classes: Kotlin has a built-in data class construct that makes it easy to define classes that are used primarily to store data. Data classes automatically generate equals(), hashCode(), and toString() methods, reducing the amount of boilerplate code required.
  • Extension functions: Kotlin allows developers to add functions to existing classes without inheriting from them, through extension functions. This makes it easier to add functionality to existing classes and reduces the need for boilerplate code.
  • Smart casting: Kotlin’s smart casting system makes it possible to cast variables without the need for an explicit cast. The compiler automatically detects when a variable can be safely cast and performs the cast automatically.

JetBrains is also the company behind IntelliJ IDEA. The language support in this Integrated Development Environment (IDE) is also great.

Kotlin has evolved over the years to support the following different platforms:

  • Kotlin Multiplatform: This is used to develop applications that target different platforms such as Android, iOS, and web applications
  • Kotlin for server side: This is used to write backend applications and a number of frameworks to support server-side development
  • Kotlin for Android: Google has supported Kotlin as a first-class language for Android development since 2017
  • Kotlin for JavaScript: This provides support for writing Kotlin code that is transpiled to compatible JavaScript libraries
  • Kotlin/Native: This compiles Kotlin code to native binaries and runs without a Java Virtual Machine (JVM)
  • Kotlin for data science: You can use Kotlin to build and explore data pipelines

In summary, Kotlin provides a more modern and concise approach to programming than Java while still maintaining interoperability with existing Java libraries and code. In addition, you can write Kotlin code and target different platforms.

Now that we have got the gist of Kotlin and its various features, let’s move on to the next section where we will understand Kotlin as a programming language and understand Kotlin syntax, types, functions, and classes.

Previous PageNext Page
You have been reading a chapter from
Mastering Kotlin for Android 14
Published in: Apr 2024Publisher: PacktISBN-13: 9781837631711
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Harun Wangereka

Harun Wangereka is a Google Developer Expert for Android and an Android engineer with over seven years of experience and currently working at Apollo Agriculture. Harun is passionate about Android development, never tired of learning, building the tech community, and helping other developers upscale their skills. He is a co-organizer at Droidcon Kenya and a former Kotlin Kenya and Android254 co-organizer. Through these communities, he has been able to impact thousands of developers. He is also an Android author at Kodeco where he has written 8 articles, published a book; Saving Data on Android, Second Edition, and is also a video course instructor. He has given numerous sessions on Android and Kotlin across different communities worldwide.
Read more about Harun Wangereka