Category Archives: Java

(English) Java threads, Android Async Task and Kotlin Coroutines

Není k dispozici v češtině.

Imagine that you need to do some long time operation and update UI with its result. You probably know that you should not call things that might take some time on UI thread. If you do so, an application might become unresponsive and that would lead to poor user experience or system stepping in and offering to kill an application with infamous Application is not responding dialog.
So how can you avoid this? There are many ways. Most commonly used are threads, AsyncTasks and now also Kotlin coroutines. In this article, I will make a quick introduction and provide reference links for more details.
Continue reading

(English) Default Interface Methods in Java 8 and Kotlin

Není k dispozici v češtině.

Before Java 8, interfaces couldn’t implement any code. With Java 8, interfaces can include default methods implementations and also static methods.

Default Methods

Default method is method implemented in interface marked by the modifier default. These methods can be called from all classes implementing the interface. What is it good for? Let’s imagine a game with a lot of heroes that have can do some actions. E.g. going left and right. After while add a new action, for example jumping. Now we have to implement this action in all methods. And maybe this action is same for all heroes or we don’t need this action to be used by all heroes. The implementation of second option could look like this:
Continue reading

(English) How to check object equality in Java?

Není k dispozici v češtině.

Java is not purely object-oriented programming language. We can use both objects and primitives for our variables. We can check easily if two primitives are same by identity operator ==. It is not as easy for objects.

==

Comparing objects with == is one of the frequented beginner’s mistakes. A beginner will check if two Strings with value “apple” are same and the answer is false. Because it doesn’t really check if objects are same but whether they point to the same memory address. How can you then check real equality of objects? Equals is the method you have to use.
Continue reading