Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Programming Kotlin

You're reading from   Programming Kotlin Get to grips quickly with the best Java alternative

Arrow left icon
Product type Paperback
Published in Jan 2017
Publisher Packt
ISBN-13 9781787126367
Length 420 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Stefan Bocutiu Stefan Bocutiu
Author Profile Icon Stefan Bocutiu
Stefan Bocutiu
Stephen Samuel Stephen Samuel
Author Profile Icon Stephen Samuel
Stephen Samuel
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Programming Kotlin
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface
1. Getting Started with Kotlin FREE CHAPTER 2. Kotlin Basics 3. Object-Oriented Programming in Kotlin 4. Functions in Kotlin 5. Higher Order Functions and Functional Programming 6. Properties 7. Null Safety, Reflection, and Annotations 8. Generics 9. Data Classes 10. Collections 11. Testing in Kotlin 12. Microservices with Kotlin 13. Concurrency

Null syntax


Tony Hoare, the inventor of the quicksort algorithm, who introduced the concept of the null reference in 1965 called it his "billion dollar mistake". Unfortunately, we have to live with null references as they are present in the JVM, but Kotlin introduces some functionality to make it easier to avoid some common mistakes.

Kotlin requires that a variable that can assigned to null be declared with a ?:

    var str: String? = null 

If this is not done, the code will not compile. This next example would result in a compile time error:

    var str: String = null 

Kotlin has much more than this to help in the fight against null pointer exceptions, and there is a full discussion of nulls and null safety in Chapter 7, Null Safety, Reflection, and Annotations.

Type checking and Casting: If a reference to an instance is declared as some general type A, but we want to test if we have a more specific type B, then Kotlin provides the is operator. This is equivalent to the instanceof...

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 €14.99/month. Cancel anytime
Banner background image