Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!

David Griffiths

Author
+ Follow
since May 09, 2015
David likes ...
Android Python Ruby
I write code for computers and words for humans.
Cows and Likes
Cows
Total received
5
In last 30 days
0
Total given
0
Likes
Total received
18
Received in last 30 days
2
Total given
5
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by David Griffiths

Congratulations Will, Celso, Al and Ron!

We hope you enjoy the book.

And thank you to the ranch hands for allowing us to tarry a while before we head 'em up and move 'em out :-)

<tip hats/>

D+D
7 months ago
Hello Jim,

Yes! We take a constructionist approach to teaching, which means that we get you to learn by doing. Often, when learning something new, it is better to begin by doing something and afterwards learning what it meant. We have exercises and puzzles throughout the book. In many ways, the exercises are the spine of Head First books.

Thanks for the question.

D+D
7 months ago
Hi!

Extension functions give you the ability to teach existing classes new tricks. :-) When you create an extension function, it feels like you are teaching some existing class a new method. In reality, the compiler will track down any calls to the new extension function and replace it with a call to the extension. This means that although it *looks* like you are adding new methods to existing classes, you not. It's a trick that's played at compile time, and so calls to extensions are always statically resolved (i.e. the code they call is always resolved at compile time, rather than run time).

Some people like to use extension functions because they make your code feel more "object-y". Rather than passing an object to a function, it feels like you are calling a method on the object. It is similar in some ways to add methods to prototypes in JavaScript, except it is a trick played by the Kotlin compiler, rather than a change to the underlying code.

That was a great question. Hope you enjoy the book

D+D
7 months ago
Hello Ludoviko,

We teach both object and functional approaches in the book, but we do not immediately jump into functional coding until a little way into the book. This is because we need to make sure that you first have a grounding in the basics of the language, such as types, variables and so on. But functional coding is one of the most powerful features in the language, and we make sure that you have a good understanding of not simply how to process collections with functional code, but also how to create higher-order functions (functions which accept functions and return functions) so that you can do things like compose data converters together.

Hope this helps.

D+D
7 months ago
Hello Will,

Yes it is just a quick introduction. Coroutines were finalised towards the end of the book, and we were determined to get some coverage.

You can think of a co-routine as a kind of lightweight thread. When you run code in a thread you are typically using a physical resource. You will normally find that threaded code will run on a distinct core in the CPU (this doesn't *have* to happen, but typically does). That means that as you use more and more threads, you steadily start to use up the resources available for running them. After a while, threads will start to block.

Co-routines provide a more advanced way of looking at multi-tasking code. Although you can run co-routines in their own threads, by using the global scope, co-routines commonly share threads. Most tasks involved doing some work and then waiting for something to happen. When this happens in a co-routines, control of the thread is rapidly switched to another co-routine, which can profitable use of the thread while the other co-routine is not using it.

This allows you to write code that looks very similar to threaded code, but which runs on the same thread. That allows you to scale the number of co-routine in a way that you never could with threads. There is no problem running hundreds of co-routines at the "same time" in a way that you never could with threads.

You can see the co-routine code for creating a drum machine here:

https://github.com/dogriffiths/HFKotlin/blob/master/appendix_a_coroutines/Drum%20Machine/src/main/kotlin/Beats.kt

The code for running "playBeats" for different instruments at the same time looks like this:

suspend fun main() {
   runBlocking {
       launch { playBeats("x-x-x-x-x-x-", "toms.aiff") }
       playBeats("x-----x-----", "crash_cymbal.aiff")
   }
}

The code for playing the Tom-toms and the cymbals will both run on the same thread, but appear to run completely in parallel, because they are using co-routines. It's this feature that makes co-routines incredibly powerful if you want to handled thousands of requests (for example) at the same time.

Thanks for the great question :-)

D+D
7 months ago
Hello Carl,

That's a great question. Kotlin can be used with any of the major Java frameworks, so there's Spring is still a popular choice. Spring 5.0 introduced extensions that allow for more idiomatic Kotlin code and is proving popular.

Once of the most interesting innovations to come from the Kotlin community is the Ktor framework. This is an asynchronous framework that has a number of similarities with Node. You can find an introductory tutorial to Ktor here:

https://ktor.io/quickstart/guides/application.html

We believe Ktor will really be a framework to watch in the future. It really uses the power of Kotlin to create scalable, efficient applications.

Thanks for the question :-)

D+D
7 months ago
Hello :-)

It's a general purpose language, like Java or C++. The reason (initially) was to create a more modern language that could be used in the same way that Java could be used. That was one reason why Google chose it for Android development. Kotlin can be compiled down to the same byte codes as Java, and so can be run on Android devices, without needing to create a whole new tool chain. However, it can also be used to create more expressive server-side code that Java, and also it became clear that because it is a general language, it would also be possible to transpire it into languages like JavaScript which will allow it to run inside node and inside browsers. More recently, Kotlin native allows it to be used on devices that have neither Java nor JavaScript.

Why should you learn it? Simply because it will *probably* integrate with your existing projects, and it will give you access to features like co-routines and more advanced generics.

Thanks for the great question!

D+D
7 months ago
Hello Paul,

On Android, Kotlin is calling the underlying component framework, so it works in a very similar way to Java code. However you will find that although it is using all the same RecyclerView code etc. in the same way, it will be a little more concise. In other areas of Android development, such as multi-tasking, Kotlin co-routines will be a much more efficient way of thinking about designing the code.

We should point out that Head First Kotlin is really a language book, so we don't really go into the specifics of Kotlin coding on Android. For that, you might want to look at future versions of our companion book, Head First Android...

Thanks for the question :-)

D+D
7 months ago
Hello Ron,

The Kotlin runtime will add a little to your app size, adding around 1MB. The performance in simple terms would be the same, because both languages can be compiled down to the same byte codes. However, co-routines (finalised at the end of last year) allow you to use threads much more efficiently by writing your code in a different style. Therefore, you can more easily write code that will make use of CPU resources that would otherwise be blocked.

(Check out the appendix in Head First Kotlin where we write a drum machine on a single thread with coroutines...)

Thanks for the question

D+D
7 months ago
Hello Tony,

That's a really good question. Clearly, at the moment, there are far, far more Java developers in the world than Kotlin developers. However, Kotlin can be used in pretty much any place where Java can be used. But because it contains more advanced language features that Java, it provides you with more powerful thinking tools, which allows you to think differently as a coder. If we were hiring a developer, we would not choose them based on the languages they know, but on the way that they think, and Kotlin provides you with ways of thinking that Java doesn't.

But maybe we're biased :-)

Thanks for the great question.

D+D
7 months ago
Hi Celso!

If we had to pick one feature it would be: brevity.

The language is far more concise, and features like lambdas and co-routines make event-based code (such as Android app code) far easier to create and understand. Your model objects will also look more data-ish, if you use data classes, so your code will generally be more readable.

Regards,

D+D
7 months ago
Hello Will,

A Java developer can be up and running pretty quickly, but to truly get to grips with the language will take significantly longer. Things like data-classes are easy to create and use if you are familiar with Java beans. Some features (such as contra-variance) will feel a little odd for even experienced Java developers.

If you are not a Java developer, you should be creating useful code within a few days. With Head First Kotlin, we'd suggest doing a chapter a day. That way, within a couple of weeks you will have a firm foundation in the language.

Thanks for the question :-)

D+D
7 months ago
Hello Will,

There is an Eclipse plug-in for Kotlin available, but I'm afraid neither of us have used it in quite a while, so we can't really say how well it's currently working.

The JetBrains support is obviously excellent, and does a very good job of allowing Java and Kotlin code to live quite happily alongside each other. There can be problems converting some code from Java to Kotlin, because Kotlin is much better at spotting potential problems and preventing you compiling the code. In our experience this is mostly when null pointers are not checked correctly in the Java code.

D+D
7 months ago
Hello Rogerio,

Kotlin is a general purpose language, and it can be used anywhere Java can run (inside Java servers; on Android phones), hooked into JavaScript and even compiled down to native code. It's particularly well suited for the creation of libraries which might need to run on different platforms. It's lambda support and multi-tasking make it a very good choice for event based coding, such as mobile apps.

It is really closer to Scala than Ruby. It's type safe, and is quite good at inferring the type of objects reference by variables. It is also very good at knowing when code is likely to have problems with null pointers, and complain at compile. It is less like Ruby, which has a looser type system. But can (and is) used for web application development, both inside Java frameworks like Spring and in Kotlin specific frameworks like the excellent Ktor https://ktor.io

Hope this helps, and thanks for the question

D+D
7 months ago
Hello Matthew,

No you don't need to know Java to learn Kotlin, although it can help. Kotlin can be learned without knowing Java, and in HFK that's what we try to do. In many ways, Kotlin is to Java, as Java was to C++. Java added features that C++ was missing, and Kotlin adds features that Java doesn't have. Java prevented you doing some things that you could do in C/C++ (like pointer arithmetic) in order to make your code more stable. Rather than removing features from Java for this purpose, Kotlin actually adds features to improve stability--such as better handling of null pointer checks.

If you want to try your hand at Kotlin, go to https://play.kotlinlang.org/ and run the code inside the browser. Another advantage of Kotlin over Java is that it can be compiled to run in a browser.

Thanks for your question, and good luck with your Kotlin journey!

D+D
7 months ago