Kotlin Beginners Guide

Kotlin The New Language for Android Development

Kotlin is a new official language for Android by Google. After, literally drowning in Android language for so many years, we developers have to learn a new language in process to be competitive in market. Anyways, it is what it is. Why a new Language?

I don't know how many of you have thought but I have been thinking a lot why a new language on whole? Always we have used Java for android development, but that doesn't make it our best choice. Since Java 8, developers are trying to fill the gap from previous versions to new versions, but android on whole doesn't support Java 8 fully.
Kotlin relatively new language based on JVM (Java Virtual Machine) enables us to follow a new horizon of development. Kotlin is compatible from latest JDK to JDK6 , meaning Kotlin application can run from older devices to new devices.
Having similar byte code structure enables Kotlin applications to run as fast as Java. Kotlin enables to use all existing libraries. Kotlin has very efficient compilation time.
So, concluding that Kotlin is efficient and we got to use Kotlin in future (not that we have a choice here).
Lets start with the basic of Kotlin Language.
Declaration of variables and assigning values are basically required in any language. In Kotlin, the variables are declared either by val OR var. A val declaration is read-only.
Now what it this read-only?
Well, it means that instance can't be changed.
A var declaration can be reassigned as compared to a val declaration which cannot be reassigned.So we can say that a var declaration is similar to any other Java declaration  and its considered mutable.A var can be reassigned as many times as possible.
Unlike Java, all declaration are Not-NULL in Kotlin. Meaning that you'll actually get compile-time error if you try to set them to a NULL value.
So if you are intended to give a NULL value like Java then the object should be followed by a question mark.
Lets start with simple example of Kotlin, explaining the basics. Follow the code given below.

 class MainActivity : AppCompatActivity() {  
   override fun onCreate(savedInstanceState: Bundle?) {  
     super.onCreate(savedInstanceState)  
     setContentView(R.layout.activity_main)  
   }  
 }  

Classes in Kotlin are declared using keyword 'class'. The default visibility in Kotlin is public. Classes and methods are final by default. You can declare them open if you want them extensible. In Kotlin you append ':NameOfParentClass()' to subclass a declaration. 'override' is a keyword acknowledging that 'onCreate()' is overridden method. 'fun' is used to identify method. See again the question mark ahead of 'Bundle' that is because you need Bundle to check for NULL in future usage.


Comments

Popular posts from this blog

The var & val Of Kotlin

Remove findViewById in Kotlin-Android