Table of Contents
1 COVER
8 INTRODUCTION WHAT DOES THIS BOOK COVER?
9 1 Objects All the Way Down KOTLIN: A NEW PROGRAMMING LANGUAGE WHAT IS KOTLIN? KOTLIN IS OBJECT-ORIENTED INTERLUDE: SET UP YOUR KOTLIN ENVIRONMENT CREATING USEFUL OBJECTS INITIALIZE AN OBJECT AND CHANGE A VARIABLE
10 2 It's Hard to Break Kotlin UPGRADE YOUR KOTLIN CLASS GAME KOTLIN HAS A LARGE NUMBER OF TYPES OVERRIDING PROPERTY ACCESSORS AND MUTATORS CLASSES CAN HAVE CUSTOM BEHAVIOR TYPE SAFETY CHANGES EVERYTHING WRITING CODE IS RARELY LINEAR
11 3 Kotlin Is Extremely Classy OBJECTS, CLASSES, AND KOTLIN ALL CLASSES NEED AN EQUALS(X) METHOD EVERY OBJECT INSTANCE NEEDS A UNIQUE HASHCODE() SEARCHING (AND OTHER THINGS) DEPEND ON USEFUL AND FAST EQUALS(X) AND HASHCODE() BASIC CLASS METHODS ARE REALLY IMPORTANT
12 4 Inheritance Matters GOOD CLASSES ARE NOT ALWAYS COMPLEX CLASSES CLASSES CAN DEFINE DEFAULT VALUES FOR PROPERTIES SECONDARY CONSTRUCTORS PROVIDE ADDITIONAL CONSTRUCTION OPTIONS HANDLE DEPENDENT VALUES WITH CUSTOM MUTATORS NEED SPECIFICS? CONSIDER A SUBCLASS YOUR SUBCLASS SHOULD BE DIFFERENT THAN YOUR SUPERCLASS
13 5 Lists and Sets and Maps, Oh My! LISTS ARE JUST A COLLECTION OF THINGS LISTS (AND COLLECTIONS) CAN BE TYPED LISTS ARE ORDERED AND CAN REPEAT SETS: UNORDERED BUT UNIQUE MAPS: WHEN A SINGLE VALUE ISN'T ENOUGH FILTER A COLLECTION BY … ANYTHING COLLECTIONS: FOR PRIMITIVE AND CUSTOM TYPES
14 6 The Future (in Kotlin) Is Generic GENERICS ALLOW DEFERRING OF A TYPE GENERICS TRY TO INFER A TYPE WHEN POSSIBLE COVARIANCE: A STUDY IN TYPES AND ASSIGNMENT CONTRAVARIANCE: BUILDING CONSUMERS FROM GENERIC TYPES UNSAFEVARIANCE: LEARNING THE RULES, THEN BREAKING THEM TYPEPROJECTION LETS YOU DEAL WITH BASE CLASSES
15 7 Flying through Control Structures CONTROL STRUCTURES ARE THE BREAD AND BUTTER OF PROGRAMMING IF AND ELSE: THE GREAT DECISION POINT WHEN IS KOTLIN'S VERSION OF SWITCH FOR IS FOR LOOPING USE WHILE TO EXECUTE UNTIL A CONDITION IS FALSE DO … WHILE ALWAYS RUNS ONCE GET OUT OF A LOOP IMMEDIATELY WITH BREAK GO TO THE NEXT ITERATION IMMEDIATELY WITH CONTINUE RETURN RETURNS
16
8 Data Classes
CLASSES IN THE REAL WORLD ARE VARIED BUT WELL EXPLORED
A DATA CLASS TAKES THE WORK OUT OF A CLASS FOCUSED ON DATA
DESTRUCTURING DATA THROUGH DECLARATIONS
YOU CAN “COPY” AN OBJECT OR MAKE A COPY OF AN OBJECT