10 Reasons Why You Should Drop Java and Switch to Kotlin

In the following article, I’m going to be listing 10 advantages Kotlin has over Java. Some common complaints I usually hear against learning Kotlin is that “Java already works fine” or “I already know Java and don’t want to learn another language”. The first reason is true to an extent, however the second is sheer laziness. Believe it or not, your language of choice will probably not be popular or around forever and even if it is, better ones may come along. As was the case with Kotlin being announced as an official language for Android development at Google IO 2017. On top of this, if you are already an intermediate or above in Java – learning Kotlin should not take you much time at all. If you are new to Android development I suggest reading another one of my articles, “How to Become an Android Developer” or this one if you want to see what Android development with Kotlin looks like. Note: these are in no particular order 10. Model Objects Unlike in Java, where, when creating classes only meant to hold data (models or POJOs to be specific), we are required to write all of the fields, setters, getters as well as a constructor. In Kotlin, we can define all of these things on a single line. Java

Kotlin

Both of these classes are identical in functionality. As you can see the Java class is your standard POJO (Plain Old Java Object) with a constructor and setter/getter methods. The Kotlin code looks a little bit different (and much shorter), but it is the same class. In Kotlin, we include the constructor and class members when declaring the class itself. We also do not need to write explicit setter and getter methods, which is huge. Var is used to …

Beginner’s Guide to Dagger in Android, with Kotlin

What is Dagger? Newer developers tend to think it’s a library that simply enables the use of dependency injection – this is false. Dagger2 is just a dependency injection framework that makes DI much easier to implement. In the following article I’m going to walk you through the entire process of setting up Dagger with code examples. I’ll also explain the basic concepts, such as components, modules and subcomponents in order to help you make sense of everything. If you are new to Android development, I’d recommend reading another article I wrote on how to become an Android developer. NOTE: Be sure to follow along with the sample project I wrote located here: https://github.com/jtrollkarl/DaggerExample This sample project makes use of the Mosby library, which allows for quick setup of the MVP pattern. The most important thing to note is that the presenter(s) is retained across device reconfig. If you do not understand what MVP is, check out this article explaining it. Dagger2 – Something Confusing? Similar to RxJava, Dagger is often said to come off as being difficult to understand and use. I can admit that when I first tried to learn Dagger, I felt completely lost. I read a lot of tutorials and watched every YouTube video I could find. Slowly but surely, one day it finally clicked. Though hopefully the learning process won’t be as arduous for you with the following article. Just remember to breathe and take it slow.   The Advantages Dagger really shines in projects with a lot of dependencies. It can be a pain to manually inject every single one of our dependencies into a class when creating it – Dagger can do this for us quite easily. Let’s take a look at the following example of a function which builds a class through constructor injection. Without Dagger

Refactoring Your Java Android App to Kotlin

With the release of Android back in 2008, the go-to language for app development has always been Java – however, that’s beginning to change. At Google IO 2017, it was announced that Android would be receiving “first class support” for Kotlin. Since then, the language has been slowly ramping up to becoming the preferred language for development on the Android platform. In the following article I’m going to give you some ways you can refactor/rewrite your existing Java app code to Kotlin, while also pointing out the advantages Kotlin gives us. But what about Java? I still feel like Java is worth knowing, since a lot of the Android APIs, as well as their documentation, are written in it. If you’re just starting Android development, I would reccommend developing at least an intermediate level of Java knowledge before moving on to Kotlin. If you’d like an in-depth guide to getting started with Android development, you can read an article I wrote detailing how to become an Android developer in under a year. I should also mention that Kotlin is interoperable with Java. For this reason, you aren’t forced to choose one or the other. You’re able to call Java code with Kotlin and vise versa. So, don’t feel like you’re wasting time just because you’re learning Java. Refactoring Java to Kotlin I’m going to present some Java classes from my own personal projects, with the Kotlin counterpart following. The advantages Kotlin presents should be evident from simply looking at the code, but I’ll be sure to go through each. SharedPreferences

Here’s an implementation of an interface used for managing a few SharedPreferences  values. The class is quite straight-forward, as far as preference managers go. Let’s take a look at the same class in Kotlin.

Immediately, you can already see …

How to Become an Android Developer in Less Than a Year

The title got your attention didn’t it? Now don’t worry, this isn’t clickbait. In the following article I am going to tell you everything you need to know about what it takes to become a professional Android developer. However, don’t assume accomplishing this will be easy – it is going to take a lot of work. Achieving this quickly is going to require dedication and a strict routine. In my opinion, you will need to set aside 2 – 4 hours a day, at least 5 days a week, to studying. For 99.9% of people – this is completely doable. Also, if you have intermediate programming knowledge, this can be done in 6 months or less. Before we get in to discussing exactly what you need to study, I’m going to elaborate on how you should study Android development.   When should I study? Whenever you can set aside a solid window of time. For most people in school or those with a full-time job, the best time would be in the evening. In the case of students, just dedicate at least 2 hours a day on top of your normal studies. Do not be tempted to go out and get wasted on the weekend, or even worse, during the week. You have little to gain and a lot to lose by engaging in this behavior – I’ve been there and it’s not worth it. For those with a fulltime job – I know this can be extremely difficult. By the time you get home from work you are probably already exhausted. This feeling can be exacerbated if you are working a job you don’t like. On top of this, you need to make dinner and wash the dishes (if you are not blessed by the holy invention known as the dishwasher). I feel your pain. However, …

Time & Space Complexity in Functions – Big O Notation

Knowing how fast a function will run or how much memory it will require when running are important factors to consider when writing algorithms. This may not be something to consider in small, simple programs, but just imagine a large project riddled with unoptimized and slow performing functions. It can have a drastic impact on overall system performance. Luckily, there is big O notation, which allows for quick deduction as to how fast a function will perform or how much memory it will require. Introducing, Big O Notation Big O notation (also known as asymptotic notation) is simply a mathematical notation we can use to measure the performance of a function. There is no math involved in this approach, rather you only need to look at the code in order to classify it. The main way we go about achieving this is by determining how fast the runtime will grow in relation to the size of the given input. For example, some functions will have a relatively quick runtime with a small input, but as input grows larger, runtime can increase exponentially. This will be clear after looking at a few examples. Time Complexity Constant time, O(1) Let’s take a look at a very simple function.

No matter what the size of the input, this function will always take the same amount of time. We deduce this by the fact that there is always only going to be one “step”. Our String can be one or one thousand characters – it will still take the same amount of time to print both inputs. In this case we can say that this function runs in O(1) time, or rather, constant time. Linear time, O(n)

This function will run in O(n) time or what is known as linear time. This is due to the fact that …