Android Development with Kotlin
Marcin Moskala, Igor Wojda
- 440 pages
- English
- ePUB (adapté aux mobiles)
- Disponible sur iOS et Android
Android Development with Kotlin
Marcin Moskala, Igor Wojda
Ă propos de ce livre
Learn how to make Android development much faster using a variety of Kotlin features, from basics to advanced, to write better quality code.About This Bookâą Leverage specific features of Kotlin to ease Android application developmentâą Write code based on both object oriented and functional programming to build robust applications âą Filled with various practical examples so you can easily apply your knowledge to real world scenariosâą Identify the improved way of dealing with common Java patternsWho This Book Is ForThis book is for developers who have a basic understanding of Java language and have 6-12 months of experience with Android development and developers who feel comfortable with OOP concepts.What You Will Learnâą Run a Kotlin application and understand the integration with Android Studioâą Incorporate Kotlin into new/existing Android Java based projectâą Learn about Kotlin type system to deal with null safety and immutabilityâą Define various types of classes and deal with propertiesâą Define collections and transform them in functional wayâą Define extensions, new behaviours to existing libraries and Android framework classesâą Use generic type variance modifiers to define subtyping relationship between generic typesâą Build a sample applicationIn DetailNowadays, improved application development does not just mean building better performing applications. It has become crucial to find improved ways of writing code. Kotlin is a language that helps developers build amazing Android applications easily and effectively. This book discusses Kotlin features in context of Android development. It demonstrates how common examples that are typical for Android development, can be simplified using Kotlin. It also shows all the benefits, improvements and new possibilities provided by this language.The book is divided in three modules that show the power of Kotlin and teach you how to use it properly. Each module present features in different levels of advancement. The first module covers Kotlin basics. This module will lay a firm foundation for the rest of the chapters so you are able to read and understand most of the Kotlin code. The next module dives deeper into the building blocks of Kotlin, such as functions, classes, and function types. You will learn how Kotlin brings many improvements to the table by improving common Java concepts and decreasing code verbosity. The last module presents features that are not present in Java. You will learn how certain tasks can be achieved in simpler ways thanks to Kotlin.Through the book, you will learn how to use Kotlin for Android development. You will get to know and understand most important Kotlin features, and how they can be used. You will be ready to start your own adventure with Android development with Kotlin.Style and approachA step-by-step guide that is filled with numerous real-world examples.
Foire aux questions
Informations
Classes and Objects
- Class declaration
- Properties
- Property access syntax
- Constructors and initializers blocks
- Constructors
- Inheritance
- Interfaces
- Data classes
- Destructive declarations
- Operator overloading
- Object declaration
- Object expression
- Companion objects
- Enum classes
- Sealed classes
- Nested classes
Classes
Class declaration
class Person
val person = Person()
// Instantiate Kotlin class inside Java file Person person = new Person()
// Instantiate class inside Kotlin file var person = Person()
Properties
Getter: A parameterless method with a name that corresponds to property name and a get prefix (for a Boolean property there might be an is prefix used instead)
Setter: Single-argument methods with names starting with set: for example, setResult(String resultCode)
- Ability to change internal implementation without changing an external API
- Enforces invariants (call methods that validate objects state)
- Ability to perform additional actions when accessing member (for example, log operation)
//Test.kt val name:String
public class Person { private int age; private String name; public Person(String name, int age) { this.name = name; this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
class Person { var name: String var age: Int constructor(name: String, age: Int) { this.name = name this.age = age } }
- The constructor method is equivalent of the Java constructor that is called when an object instance is created
- Getters and setters are generated by the Kotlin compiler
class Person constructor(name: String, age: Int) { var name: String var age: Int init { this.name = name this.age = age println("Person instance created") } }
class Person constructor(name: String, age: Int) { var name: String = name var age: Int = age }
class Person constructor (var name: String, var age: Int)
class Person (var name: String, var age: Int)
class Person( var name: String, var age: Int )