ebook img

Programming Kotlin: Create Elegant, Expressive, and Performant JVM and Android Applications PDF

445 Pages·2019·8.941 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming Kotlin: Create Elegant, Expressive, and Performant JVM and Android Applications

Early Praise for Programming Kotlin Venkat is someone that can take any topic and make it pleasurable and easy to understand. In this book he will take you on an adventure of learning Kotlin in a clear and pragmatic way, providing real-world usages, while at the same time showing you the benefits of the language. ➤ Hadi Hariri Developer Advocate, JetBrains This book will open the door to the amazing world of Kotlin. You’ll be guided along this journey by one of the most famous public speakers and educators of our day. Have a nice Kotlin! ➤ Eugene Petrenko, PhD Developer, Speaker, JetBrains Enter the cave of Kotlin using this excellent book as your headlight. Gain confi- dence in this beautiful language as a cave explorer gains self-assurance in finding his way in a newly explored cave never seen by the human eyes before. Explore pragmatically deeper and deeper into this modern JVM language using the knowledge, hints, and guidelines provided by an extraordinary, experienced programming-language enthusiast: Venkat. ➤ Tom Adam Senior Consultant, CEO, Lambda Consulting AS The engaging and humorous way of explaining things, as Venkat does in his talks, is also displayed in this book. It is a very well-structured and easy-to-read guide for everyone who is—or wants to begin—programming in Kotlin. ➤ Brian Vermeer Developer Advocate, Snyk Really a perfect book to get up and running, and not just hear about the hype of Kotlin. It actually explained what’s the buzz about Kotlin. Venkat did it once more! A must-read book, not just to get up and running with Kotlin but also to compare what we are missing in Java. ➤ Zulfikar Dharmawan Software Engineer, ING Bank NV Kotlin is a very promising new language, and Venkat uses his knowledge, humor, and clear admiration for Kotlin to create a very readable and educational book. Venkat explains things very well, provides helpful advice, and even gives an occa- sional laugh. ➤ Tory Zundel Software Architect The book is well-crafted with good, succinct examples—highly recommended for Java developers looking to transition into Kotlin. ➤ Ashish Bhatia Software Enginner and Blogger, ashishb.net If you’re content with Java and see no need for null-safe traversal or first-class delegation then put this book down; you’re not ready. Otherwise, read this book. You will be entertained and educated simultaneously. ➤ Daniel DeGroff CTO, FusionAuth We've left this page blank to make the page numbers the same in the electronic and paper books. We tried just leaving it out, but then people wrote us to ask about the missing pages. Anyway, Eddy the Gerbil wanted to say “hello.” Programming Kotlin Creating Elegant, Expressive, and Performant JVM and Android Applications Venkat Subramaniam The Pragmatic Bookshelf Raleigh, North Carolina Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic books, screencasts, and audio books can help you and your team create better software and have more fun. Visit us at https://pragprog.com. The team that produced this book includes: Publisher: Andy Hunt VP of Operations: Janet Furlow Managing Editor: Susan Conant Development Editor: Jacquelyn Carter Copy Editor: Sakhi MacMillan Indexing: Potomac Indexing, LLC Layout: Gilson Graphics For sales, volume licensing, and support, please contact [email protected]. For international rights, please contact [email protected]. Copyright © 2019 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. ISBN-13: 978-1-68050-635-8 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—September 2019 Contents Foreword . . . . . . . . . . . . . xiii Acknowledgments . . . . . . . . . . . xv Introduction . . . . . . . . . . . . xvii 1. Hello Kotlin . . . . . . . . . . . . . 1 Reasons to Love Kotlin 3 Why Should You Choose Kotlin? 6 Taking Kotlin for a Ride 7 Compile to Bytecode and Run 8 Compiling to Other Targets 13 Which Option to Choose? 14 Wrapping Up 14 Part I — Scripting with Kotlin 2. Kotlin Essentials for the Java Eyes . . . . . . . 19 Less Typing 20 Sensible Warnings 24 Prefer val over var 26 Improved Equality Check 28 String Templates 29 Raw Strings 30 More Expressions, Fewer Statements 33 Wrapping Up 35 3. Working with Functions . . . . . . . . . . 37 Creating Functions 38 Default and Named Arguments 43 vararg and Spread 46 Contents • viii Destructuring 49 Wrapping Up 51 4. External Iteration and Argument Matching . . . . . 53 Range and Iteration 54 Iterating over Arrays and Lists 57 When It’s Time to Use when 58 Wrapping Up 62 5. Using Collections . . . . . . . . . . . 63 Flavors of Collections 63 Using Pair and Triple 66 Arrays of Objects and Primitives 68 Using List 70 Using Set 72 Using Map 73 Wrapping Up 75 6. Type Safety to Save the Day . . . . . . . . . 77 Any and Nothing Classes 78 Nullable References 80 Type Checking and Casting 87 Explicit Type Casting 91 Generics: Variance and Constraints of Parametric Types 93 Reified Type Parameters 101 Wrapping Up 104 Part II — Object-Oriented Kotlin 7. Objects and Classes . . . . . . . . . . 107 Objects and Singletons 107 Creating Classes 113 Companion Objects and Class Members 124 Creating Generics Classes 127 Data Classes 129 Wrapping Up 132 8. Class Hierarchies and Inheritance . . . . . . . 133 Creating Interfaces and Abstract Classes 134 Nested and Inner Classes 138 Inheritance 140

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.