They say it’s never too late to learn something. I’m not 100% behind that saying, as there are a lot of fields in which the sooner you start, the better. Programming is that way, obviously it’s a clear advantage to start early if you want to follow a career in that field.
Anyway, for a lot of non-programmers (such as myself), coding your own Android app sounds extremely difficult. Sure, there are a lot of platforms out there that allow you to create your Android app without any coding knowledge, but at some point if you need anything advanced you’ll still need to look at the code. That’s one of the reason I’ve recently started having a bit of fun with Kotlin to see where that gets me. In the worst case scenario I’ll simply give up on learning it after spending a bit of time doing so – but even this scenario has the benefit of training my mind (learning something new helps) and in the end I’d still know something new. The best case scenario is the one where I manage to publish a small Android app built from scratch.
For the others that ponder with the idea of learning to build their own app from scratch, here are the basics:
- you’ll need an IDE to write your code in for the app. I recommend Android Studio by Google because it’s free and a lot of Android devs use it (you can also use IntelliJ IDEA). This has everything you need, it has a code editor where you write your code but also app templates so that you don’t have to start from scratch. Obviously has more advanced tools for devs to debug, test, improve performance and so on.
- you’ll need to decide what language to write your Android app in. While native Java might be preferred, my choice is Kotlin. Why Kotlin? Kotlin was released by the creators of IntelliJ IDEA in 2011. Kotlin is designed to interact with Java code seamlessly so you can think of it as a Java language but simplified in a way without losing advanced functionality.
You’ll also need to start with a tutorial to learn Kotlin. There are a plethora of tutorials out there, but for starting up I’d recommend this one from Google which is free and pretty good to have an idea on How to build your own app in Kotlin.
They’ve also got a video version of it:
Back to Kotlin, while toying around with it I’ve discovered some features that make it easier to work with than other “boring” languages (i.e. C). One of them is the String Templates feature.
Regular String Concatenation in Kotlin
At this point I assume you know a bit about Kotlin. Anyway, say for instance that you are building an app that helps you manage your garden. You’ll need to output text for sure. So let’s assume you want to plant tomatoes and the app will tell you how many you can plant in your garden (there’s certain distances to keep).
To output the text, you will need to concatenate several predefined strings with values calculated by the app. Here’s how it would look like to use regular string concatenation in Kotlin to output text (just look at the println function):
fun main() {
val plant = "Tomatoes"
val gardenSize = 20 // in squaremeters
val rowDist = 50 // distance between row of plants in cm
val colDist = 40 // distance between column of plants in cm
var numPlant = gardenSize*10000 / (rowDist*colDist) // here we approximate how many plants will you be able to plant keeping those distances
// Here is where we output the recommendation on what planting distances to use
println("You can plant " + plant + " keeping a distance of " + colDist + " centimeters in between each plant and in rows at " + rowDist + " centimeters apart. You will be able to plant an approximate number of " + numPlant + " " + plant + " in your garden of " + gardenSize + " square meters. Good luck!")
}
I won’t go into the details of the variables/calculations there as those are just used as an example. But look at the println function, the one that will actually output the text to the user. Look how many separate strings of text we need to concatenate to get in the end the following output:
“You can plant Tomatoes keeping a distance of 40 centimeters in between each plant and in rows at 50 centimeters apart. You will be able to plant an approximate number of 100Tomatoes in your garden of 20 square meters. Good luck!”
String Templates in Kotlin
Luckily, Kotlin has a cute little feature called String Templates. Basically in between those quotes you can simply include your variables/constants and it’ll know how to replace them with their own values, as long as you add the $ sign. So this is what the little program above becomes once you use string templates:
fun main() {
val plant = "Tomatoes"
val gardenSize = 20 // in squaremeters
val rowDist = 50 // distance between row of plants in cm
val colDist = 40 // distance between column of plants in cm
var numPlant = gardenSize*10000 / (rowDist*colDist) // here we approximate how many plants will you be able to plant keeping those distances
// Here is where we output the recommendation on what planting distances to use
println("You can plant $plant keeping a distance of $colDist centimeters in between each plant and in rows at $rowDist centimeters apart. You will be able to plant an approximate number of $numPlant $plant in your garden of $gardenSize square meters. Good luck!")
}
An incredible difference if you ask me, because it’s much more easier to read than the regular concatenation method. If you name your variables right, you’ll be able to figure the output just by looking at the println function.
Just remember that String templates in Kotlin contain a String with any variable names preceded by the $ symbol. One other advantage I see here is that contrast to regular String concatenation, you don’t need to add whitespaces and thus you’d be saving a lot of time to write this part up.
I imagine that String templates are widely used and most likely the preferred way Kotlin devs output text. Not saying that regular String concatenation is wrong as that’s just as valid, but it’s much more easy to use String templates.
Kotlin vs Java?
Anyway, this is just 1 small feature that Kotlin has to offer to ease up app development as I’m sure I’ll discover down the road. Now, deciding if to use regular Java to develop your Android app or Kotlin that’s a matter of preference, but in my brief research this is why I think Kotlin is much more friendly for newbie devs:
- Syntax. The Kotlin syntax is more concise compared to Java, resulting in less boilerplate code. String templates are just an example. Kotlin supports features like data classes, extension functions, and null safety, which are not available in Java.
- Safety features. Kotlin has built-in null safety features, which help prevent null pointer exceptions. Java does not have native null safety features, although libraries like Optional and @NonNull annotations can help mitigate null pointer issues.
- Interoperability. Kotlin is fully interoperable with Java, allowing app devs to use Java libraries and frameworks seamlessly. Java and Kotlin code can even co-exist within the same project, enabling a smooth transition from Java to Kotlin or vice-versa.
- Performance. Both Kotlin and Java are compiled to bytecode and run on the Java Virtual Machine (JVM), so their performance is comparable. What I’m saying is you won’t lose anything in terms of performance by choosing Kotlin over Java. Any differences in performance are generally negligible and are more likely due to the specific implementation rather than the language itself.
- Concurrency. Kotlin provides built-in support for co-routines, making concurrent and asynchronous programming easier and more efficient (i.e. loading an ad asynchronously in your app so that users won’t wait for it to load to use it). Java supports concurrency through features like threads, Executors, and the java.util.concurrent package, but it can be more complex and error-prone compared to Kotlin’s coroutines.
- Ecosystem and Community. Java has been around the block for much longer than Kotlin, which means it has a larger ecosystem and more extensive community support due to its longer history and widespread adoption. Kotlin’s ecosystem and community however have been growing rapidly since its introduction, with increasing support from major companies and developers.
- Learning Curve. I’ve compared the two and Kotlin’s syntax is more modern and intuitive, which may result in a shorter learning curve compared to Java for new developers. Experienced Java developers may find it relatively easy to transition to Kotlin due to its interoperability and similarity to Java.
In summary, I believe Kotlin offers several advantages over Java for wannabe’ Android developers.