Kotlin 1.3 is an Exciting Option for Android Mobile Apps Development

by Jul 30, 2019#Android, #FrontPage, #Mobile

Printer Icon
P

Table of Content

  1. Introduction 
  2. Data Class
  3. Collections

Introduction

Kotlin is a new programming language, it is designed to interoperate fully with Java. It offers a more concise syntax for developers, but also, it can improve the quality of your app due that its null safety checks, by default it won’t allow any property to be null preventing the most common error in Java/Android applications, the famous NullPointerException.

Kotlin is mainly used for Android development, and it has been adopted for most of the developers and companies because it offers a better and concise programming syntax which makes developers more comfortable writing code, also it is preferred because its stability preventing the common errors seen in a Java program, but also, because it is easy to switch to iOS development given that Kotlin syntax is very similar to Swift.

Here is a couple of examples that compare Java vs Kotlin, you will notice that Kotlin requires much less code and it is easier to read.

Data Class

In Java, you need to declare all the getters and setters as well as the different combinations of its constructor. In the other side, Kotlin just require you to declare this with the “data” keyword and have its arguments in the constructor, and you can have this in one single line of code.

Java
public class Movie {
  private int id; 
  private String name;           
  private String synopsis;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
     return name;
  }

  public void setName(String name) {
     this.name = name;
  }

  public String getSynopsis() {
     return synopsis;
  }

  public void setsynopsis(String synopsis) {
     this.synopsis = synopsis;
  }
}
Kotlin
data class Movie (
      var id: Int,
      var name: String,
      var synopsis: String)

Collections

Kotlin comes with a very useful helper method to manage and iterate over the collections. For example, what if your application is required to get the average age of employees in the company in Dallas. Let’s see how we could accomplish this in Java and Kotlin.

Java
public double getAvgAge
  (@NotNull List<Employee> employees) {
  int ageSum = 0;
  int count= 0;

  for (Employee employee : employees) {
    if (“Dallas”.equals(employee.getCity()) {
      ageSum += employee.getAge();
      count++;
    }
  }

  if (count == 0) return 0;
  return ageSum / count;
}
Kotlin
fun getAvgAge (val employees: List<Employee>):
    Double {
      return employees.filter{
        it.city == City.Dallas }
        .map{ it.age }.average()
    }

Check the official documentation for further details on Kotlin.

Photo by Marc Reichelt on Unsplash

About Us: Krasamo is a mobile development and consulting company focused on the Internet-of-Things and Digital Transformation.

Click here to learn more about our mobile development services.

RELATED BLOG POSTS

Digital Strategy 101

Digital Strategy 101

Digital strategy consulting provides businesses with insights and direction in developing digital strategies that unlocks and sustain real value and innovation—a key to gaining a competitive advantage in today’s marketplace.

Smartphones NFC Payments Systems – More Than Just a Touch Simpler

Smartphones NFC Payments Systems – More Than Just a Touch Simpler

NFC payments systems are a safe way of transferring private data between smartphones and other devices. When used for payments, it securely establishes a phone user’s identity, verifies that the person has the necessary funds to pay for a product or service, and authorizes the transfer of those funds to the seller. For both the smartphone owner and the vendor, this is a quick, convenient, and seamless way to buy and sell goods and services. It completes a typical transaction in about half the time it takes a contactless card.

App Ideas: Discovery and Features Planning

App Ideas: Discovery and Features Planning

Discovering app ideas and app features through an iterative process that converts them into concepts and prototypes with a delightful and seamless user experience (UX).