Kotlin: Master Kotlin Programming - Step by Step Guide!
- 12.5 hours on-demand video
- 1 downloadable resource
- Full lifetime access
- Access on mobile and TV
- Certificate of Completion
Get your team access to 4,000+ top Udemy courses anytime, anywhere.Try Udemy for Business
- Learn to configure environment for Kotlin development
- Discover how to mimic the concept of static in Kotlin using Companion Objects along with Object-Oriented Kotlin programming
- Explore recursive functions, pattern matching, and immutable data structures.
- Learn higher-order lambda expressions and generics.
- Apply the new Android Kotlin KTX extensions to make app development more concise, pleasant, and idiomatic.
- Get the grips on Extension functions and Data classes
- Grasp the Kotlin Standard Library API
- Use Kotlin to build a DSL
Functions can be overly long with additional unnecessary scaffolding. That makes it more difficult to understand what the code is doing. The goal here is to reduce the code to a single line.
Check the function body for conversion
Replace a block body with an expression body
Verify that the function is two lines or less
Often when calling a function, the parameters lack detail. Also, overloaded functions can result in a lot of unnecessary duplicated code. Use named arguments to add parameter info and default parameters to reduce the number of overloaded functions.
Add named arguments to the function
Add default parameters to the function
Verify that the amended function works with a different number of parameters
Adding functionality to a class can be a sizable task involving inheriting a new class. Adding an extension function allows more functionality to be added with creating an inherited class.
Define an extensional function
Call the extension function
Verify that there are no syntax errors and the code runs with the expected result
It is common that, after creating a class instance, a number of methods of the object will need to be called. That means the object named will be called a number of times. By using a number of scoping methods, the references to object names can be removed.
Add a with function to remove object names
Use the apply extension for method initialization
Successful calls of with and apply functions, with the expected result
Null pointer exceptions can be catastrophic for applications. Kotlin methods will be provided for removing null pointer exceptions in a graceful and concise manner.
Use the Elvis operator for handling nulls in a concise manner
Use Smart Cast to remove ClassCastException
Run the code to verify solutions and remove the exceptions
Using the standard If condition to check for null makes the code verbose and unnecessarily long. A solution will be provided for handling the null expression in a clearer, more concise way.
Replace the If condition with let to make your code more concise
Combine let with Elvis for returning default values
Run the code and confirm that let returns same result
Removing null elements from a collection is a common situation. But this can involve several operations, making code long, verbose, and open to errors. Provide solutions for removing null elements which are concise and clear.
Apply filterNotNull for removing nulls from a collection
Use the listOfNotNull function to create a null-free collection
Run the code for both solutions to confirm that lists have no nulls
When an object is shared between different threads, its data can be corrupted if the threads try to access it at the same time. Provide solutions to control access to the shared object from multiple threads.
Add the synchronized function to the thread definitions
Replace this function with the Reentrant withLock extension function
Check both solutions and maintain BasicThreadOne access of the shared object
Using a class instance for shared data between multiple threads will cause problems if more than one instance is created. Use a singleton to create a single instance of a class.
Run the class code to confirm multiple list instances
Replace the class with the object singleton
Run the singleton code to check for single list
Classes can consist of multiple constructors, which tend to be verbose and take time for the reader to absorb. Provide a method of initializing classes’ properties in a single line, making the code concise.
Implement a primary constructor for initializing properties
Initialize the properties inside the primary constructor parenthesis
Run the code to check whether the class properties are initialized
Extending a framework class with multiple constructors can be complex and difficult. Provide the Kotlin recommended approach for extending a class with multiple constructors.
Describe how to implement secondary constructors
Describe how to pass secondary constructor parameters to the super constructor
Run code to check whether the super constructor properties are initialized
When providing additional logic to a property that stores a value, recursion can occur, resulting in an exception. Provide a solution to remove recursion issues.
Provide a backing field to the property getter
Provide a backing field to the property setter
Run code to test that recursion has been removed
Creating a sub-class which will be used in a localized place involved an additional amount of code. Provide an alternative solution to using a sub-class.
Use an object expression to create an anonymous object
Override the superclass method in the anonymous constructor
Start the thread to verify that the overridden method executes
When using a class with multiple secondary constructors, additional code will be required for creating instances of the class. Provide an alternative solution to creating class instances using multiple constructors.
Replace secondary constructors with primary constructor and companion object
Add a factory pattern to the companion object
Verify that the factory methods provide the same result as secondary constructors
Using traditional functions can result in unnecessary, similar code. Find an alternative method to defining and calling functions.
Replace the function call with a lambda
Add a function type to the lambda result
Run code to verify that the result is same as the original
Java does not support passing a function to another function as an argument. Find a solution where a function can be passed to another function.
Create a function with a function type as its parameter
Replace the function argument with a lambda expression
Call the function type parameter from inside the function
There can be confusion between read-only versus immutable collections related to thread safety. Create code to check read-only and immutable collections for thread safety.
Write code that checks if read-only contents can be modified
Write code that checks if immutable contents can be modified
Run code to identify which collection is thread-safe
Filtering and modifying the contents of a collection are common operations. Find Kotlin solutions to simplify this.
Implement the Kotlin filter extension function
Implement the Kotlin map extension function
Run code for both functions and check the result collection
The class toString, equals, and hashCode methods are very common methods. But they need to be implemented manually, which involves a lot of additional boilerplate code. Find an alternative method to simplify the creation of the toString, equals, and hashCode methods.
Manually create toString, equals, and hashCode methods to illustrate effort
Replace manual methods with the data class
Run code using the data class to verify the same output
There are a number of functions available which can provide the same result. But the time taken to complete the operation may not be the same. Find the method for recording the time taken for executing a function. Find a way of generating the equivalent Java code to help identify any potential performance issues.
Use the measureTimeMillis function to record elapsed time
Use Kotlin Bytecode tool to display equivalent Java code
Find slow-running function and identify cause from java code
Loading an application with large initialization data can result in slow startup times of the application. This can be mitigated by delaying the loading of the list until required. Find a solution whereby the data can be loaded on demand.
Manually implement a lazy initialization pattern with a backing property
Replace the pattern with the Kotlin lazy delegate
Run code and check that both results are the same
When passing a lambda to a higher order function as an argument, extra methods can be generated and boxing and unboxing can occur, resulting in additional performance overheads. Find a solution which removes the additional methods and any boxing/unboxing that may occur when passing a lambda as a function argument.
Create a capturing lambda expression which creates extra methods in Java
Create a lambda that implements boxing and unboxing in Java
Inline functions and verify the removal of methods, boxing, and unboxing in Java
Creating public properties in a companion object will result in additional getter and setter methods being created. This can make an additional performance overhead. Find a solution which removes the creation of the extra setter and getter methods.
Add a const keyword to public property
Add a @JvmField annotation to the public property
View equivalent Java code to confirm the removal of methods
Running multiple operations, such as chaining filter and map calls on large collections, can result in poor performance. Find a solution to improve performance when running multiple operations on large collections.
Use Kotlin Bytecode tool to identify performance issues
Replace the implementation with a sequence
View equivalent Java code to confirm the removal of temporary lists
When calling Android APIs using Kotlin, additional boilerplate code is required for some simple method calls. The Android Toast function, for example, requires an additional sub-method, context, time duration, and then an apply call. The solution will be to replace Android Toast with the KTX core toast method.
Highlight the issue by demonstrating the Android Toast call
Replace Android Toast with KTX core toast
Run KTX core toast to verify the same output
When making SQLite transactions, a number of calls need to be made in a try-finally harness to ensure that the SQLite transaction is completed. The harness, along with three SQLite transaction calls, adds a significant amount of boilerplate code and complexity. Replace the Android SQLite transactions with the KTX SQLite module.
Demonstrate the problem by executing a SQLite transaction
Replace the Android SQLite transaction with a KTX SQLite transaction
Execute the KTX SQLite transaction code to verify the output
When replacing Android fragments using the FragmentManager, additional methods such as beginTransaction and commit are required. Yet again, boilerplate code! Replace the Android Fragment transaction code with the KTX fragment transaction.
Demonstrate the issue by implementing an Android fragment transaction
Replace the Android transaction with the KTX fragment transaction
Run code to verify that the operation completes the same
Creating certain types of collections such as ArraySet will involve code to manually create a value and then populate the items in the array one at a time, before returning the populated ArraySet. Use the KTX collection module to create an ArraySet with a single method call.
Demonstrate the issue by implementing an ArraySet the Android way
Replace the Android code with the KTX collection arraySetOf method
Run code to verify that the same ArraySet collection is created
When implementing background tasks, the device API level and capabilities will have to be checked before deciding on a solution, which could be JobScheduler, Firebase JobDispatcher, or AlarmManager. The WorkManager will do the API and capability check, along with selecting the correct background implementation.
Implement a worker to populate a collection in a background task
Implement a worker that provides notification updates from the background task
Send data and observe updates from the workers
In this video, we will see what the properties in Kotlin are and learn how they are different from Java.
Explore keywords for declaring properties
Learn to declare a read only property, and a mutable property
Understand how to call and initialize properties from Kotlin and Java
What are the functions and function variables in Kotlin?
Learn to declare a function and explore keywords for declaring variables
Explore default visibility modifiers and study the read only and mutable variables
Understand the use of functions in Kotlin and use variables inside the functions
- No prior knowledge is required for this course, you will be learning Kotlin development from scratch.
Kotlin is a statically typed language whose syntax is more expressive and concise than Java’s. Kotlin has been adopted by Google as a first-class language for developing Android apps. Since then, Kotlin has gained a huge popularity worldwide among developers due to its highly appreciable features therefore it also helps to build amazing applications in an easy and effective way.
This course is an easy-to-follow guide with a step-by-step approach which will get you up and running with Kotlin basics and fundamentals such as variables, data types, string templates, expressions, null values, and loops. You will also learn to design application structures using functional programming concepts and implement business logic using tools provided by Arrow library. Next you will be introduced to using the Android Studio IDE which covers the concepts such as functions, lambdas, properties, object oriented code, safety aspects and type parameterization, Microservices, testing, concurrency which will guide you to write Kotlin’s code to production. Finally you will be mastering to integrate Kotlin in any existing Android project built using C++ or Java.
By the end of the course, you’ll be able to write faster & cleaner code to build your own robust program in Kotlin to make your life easy as a Kotlin developer. Also you will be more proficient in using Kotlin for any kind of app development.
Contents and Overview
This training program includes 2 complete courses, carefully chosen to give you the most comprehensive training possible.
The first course, Learn Kotlin Programming starts by showing you how to set up the Kotlin environment and install Intellij IDEA to write Kotlin code. After that, the course dives into the Kotlin basics and fundamentals such as variables, data types, string templates, expressions, null values, and loops. Moving on, you’ll learn how to write functions in Kotlin. You’ll explore Kotlin as an Object-Oriented Language by exploring interfaces and various Kotlin classes . Next you’ll dive into Functional programming in Kotlin using Lambdas and higher-order functions . Finally, you’ll explore the collection framework and perform operations such as filtering and sorting using Predicates and FlatMaps in Kotlin. By the end of the course, you’ll be able to build your own robust program in Kotlin.
In the second course, Hands On Functional Kotlin demonstrates Kotlin language features and use-cases as well as popular functional programming concepts and techniques. You will learn to design application structures using functional programming concepts and implement business logic using tools provided by Arrow library.
In the third course, Kotlin – Tips, Tricks, and Techniques you will discover new possibilities with Kotlin and improve your app development process. In this course, you will work with interesting tools and techniques on examples which you can adopt straightaway. You will speed up your development with less coding and get faster results. By the end of this course, you will be able to write faster, cleaner code and make your life easy as a Kotlin developer.
In the fourth course, Mastering Kotlin for Android Development you will begin with exploring all the features of Kotlin language that make the language better alternative to Java. It will show you the environment setup, and the difficulty level will grow steadily with the features covered next. Moving on, we’ll introduce you to using the IntelliJ IDE, which plays an integral role in Kotlin development. We’ll cover Kotlin’s basic programming concepts such as functions, lambdas, properties, object-oriented code, safety aspects, type parameterization, and finally concurrency, which will guide you to write Kotlin’s code to production. By the end of the video, you will be proficient in using Kotlin for any kind of app development.
About the Authors
Book of Brains creates and distributes high-quality technology training content. We aim to teach technology the way it is used in industry and the professional world. Our trained team of professionals delivers hands-on workshops and training content for mobile as well web development technologies. We constantly monitor current industry trends and keep our training materials and workshops up to date.
Stepan Goncharov lives in Singapore. He is involved in helping local developers to grow by sharing knowledge and organizing Kotlin Singapore User Group events. He has been engineering apps and games for Android since 2008. He has experience in QA, as a product manager, marketer, blogger, consultant, engineering manager, and much more. He has been involved in the development of apps that nobody uses and apps with millions of users worldwide. He currently works at Grab, actively uses Kotlin and Rx, and spends more and more time contributing to OSS. When he is not recording video courses or organizing events for developers, he travels around South East Asia skiing on a wakeboard.
Nigel Henshaw is a mobile software developer who, as well as providing software development services, loves to share his knowledge through his YouTube channel and website. Nigel originates from New Zealand, where he started out as an electrician. After one too many electric shocks, he wisely decided on a career change. After completing a two-year diploma in Computer Science and Technology at the Auckland University of Technology, Nigel relocated to London for new opportunities. He landed a job as a test engineer with a pioneering tech company called Psion, who were one of the first to develop hand-held devices and mobile operating systems. Before too long, Nigel was transferred to the base porting team and found himself in the midst of porting operating systems and writing device drivers with Symbian OS using C++.
Nigel has experienced many wonderful opportunities through the software industry, working in the UK, Scotland, and Japan. He has held jobs as a software engineer, consultant, project manager, and general manager of a remote development site. In his spare time, Nigel is currently learning Japanese, enjoys cycling and running, and likes to finish off the day by soaking in a Japanese hot spring.
Marko Devcic is a Software Engineer currently working as an Android Developer for Austrian company mySugr in Vienna. He has a Master's degree in Engineering from University of Zagreb in Croatia. Over the years, he has worked with various technologies and languages, mostly C# and Java. He is passionate about technology and software development. He loves reading about programming languages and writing simple and clean code. In his spare time, he writes code for his personal projects, and contributes to open source projects. You can follow him on Github (username deva666). He also likes to write about Kotlin, C#, and programming in general on his web page.
- This course is for programmers who are new to Kotlin. Professional developers and students who want to learn more about Kotlin and functional programming concepts. Moreover it is intended for all levels, and developers with prior knowledge can benefit because the course also covers advanced topics of Kotlin.