Improving productivity with Kotlin

I recently wrote an article about what's new in Kotlin 1.4.20. And the first comment turned out to be a little unfair to Kotlin.





He argued that why should Kotlin be in mobile development, because there is Java, because it is one of the best programming languages.





And to all this, a lot of Android Framework code is written in Java, or rather more than 50%!





Before I share my opinion and present this story, please, please do not beat me with chairs.





Well, let's start with statistics!





What do professional developers say?

Google , Kotlin.





:





67 % Android , Kotlin, , !





Florina Muntenescu (Android Developer Advocate)





, Kotlin, 100% .





, .





Google , ?

Android Kotlin:





  1. - , .





  2. - Kotlin Java





Flipkart:





, 50% , , Kotlin.





Cash App:





Cash App Kotlin, Builder - , ( 25% ).





Kotlin Zomato





Duolingo

Duolingo - Google Play ( 100 . ).





46% ( , ..). Kotlin.





. : , , 2 !





, , !





: Java Kotlin 30%, 90%!





Kotlin

Android Java, :





1)





2) Build





Kotlin , .





Builder Java:





public class Task {
     private final String name;
     private final Date deadline;
     private final TaskPriority priority;
     private final boolean completed;

     private Task(String name, Date deadline, TaskPriority priority, boolean completed) {
         this.name = name;
         this.deadline = deadline;
         this.priority = priority;
         this.completed = completed;
     }

     public static class Builder {
         private final String name;
         private Date deadline;
         private TaskPriority priority;
         private boolean completed;

         public Builder(String name) {
             this.name = name;
         }

         public Builder setDeadline(Date deadline) {
             this.deadline = deadline;
         return this;
         }

         public Builder setPriority(TaskPriority priority) {
             this.priority = priority;
             return this;
         }

         public Builder setCompleted(boolean completed) {
             this.completed = completed;
             return this;
         }

         public Task build() {
             return new Task(name, deadline, priority, completed);
         }
     }
}
      
      



Kotlin ( hashCode(),



equals()



):





data class Task(
     val name: String,
     val deadline: Date = DEFAULT_DEADLINE,
     val priority: TaskPriority = TaskPriority.LOW,
     val completed: Boolean = false
)
      
      



!





Singleton Java:





public class Singleton{
    private static volatile Singleton INSTANCE;
    private Singleton(){}
    public static Singleton getInstance(){
        if (INSTANCE == null) {                // Single Checked
            synchronized (Singleton.class) {
                if (INSTANCE == null) {        // Double checked
                    INSTANCE = new Singleton();
                }
            }
        }
        return INSTANCE;
    }
    private int count = 0;
    public int count(){ return count++; }
 }
      
      



Kotlin:





object Singleton {
     private var count = 0
     fun count(): Int {
         return count++
     }
 }
      
      



.





Kotlin , , :





fun borrow(){
    library -= book //  operator overloading
    val (title, author) = book //  data 
    println("Borrowed $title") //  
}
      
      



, Kotlin null :





var str: String? = null // , , 
												//  str    null
        
println(str?.length) //    Safe (?) 
    
val len = str?.length ?: 0 //  0,  str   null

var listOf: List<String>? = null //    null

listOf?.filter { it.length > 3 } //   
    	?.map { it.length }
      ?.forEach { println("Length more 3 -> $it") }
      
      



Android Kotlin, , :





@Inject
lateinit var viewModelFactory: MyViewModelFactory

private val viewModel by viewModels<MyViewModel> { viewModelFactory }

      
      



Kotlin , :





dependencies {
  	implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
  	implementation "androidx.room:room-ktx:$room_version"
		implementation "androidx.paging:paging-runtime-ktx:$paging_version"
}
      
      



Java , , Kotlin 95% , Java .





, Java , :)





Kotlin , Android Framework Java Java.





:





  1. Medium Florina Muntenescu (Android Developer Advocate)





  2. Twitter Florina Muntenescu





  3. Twitter Android Developers





  4. Duolingo Kotlin





  5. Android Developers Store: Zomato Kotlin












All Articles