Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Kotlin Quick Start Guide

You're reading from   Kotlin Quick Start Guide Core features to get you ready for developing applications

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789344189
Length 178 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Marko Devcic Marko Devcic
Author Profile Icon Marko Devcic
Marko Devcic
Arrow right icon
View More author details
Toc

Generics

We've already seen generics in previous chapters, and in this section, we'll learn how they work, how they are implemented, and what the differences are from generics in Java.

Simply put, generics are types with parameters. Take a look at the List interface, for example; without generics, you could add objects of any type to it. But, since the list interface is generic, you have to specify a generic type for when you are creating an instance of it. You can create a list of strings, a list of integers, and so on. This then makes the list type-safe; you cannot add an integer to a list of strings.

The generic type is specified in angle brackets, so a list of strings type is declared like this:

List<String> 

Creating an instance of a generic type is the same as creating normal types; you call a generic class constructor and specify the generic type...

lock icon The rest of the chapter is locked
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.
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 $19.99/month. Cancel anytime