Getting Started With Kotlin - Basics

What is Kotlin?

Google announced Kotlin as an official language for Android Application development in Google I/O 2017.Kotlin is a statically-typed language, developed by JetBrains and Open source Contributors in the 2011, that runs on JVM(Java Virtual Machine) and is compiled to JavaScript source code. It is very suitable for server-side application development with full compatibility of Java frameworks. You can use multiple frameworks like - Spring, Vert.x, Ktor etc. for web application development.  

Features                                                                          
  • It is compatible to greater than or equal version of JDK 6
  • As Kotlin uses inline functions and lambda operations it requires much less time to execute.
  • Kotlin allows using all Java frameworks.
  • Kotlin supports incremental compilation so it required less compilation time than Java.
  • Kotlin is very easy to learn. It does not make too many restrictions in programming.
  • It is null-safe..
  • It can be learned easily.
  • Code review is easy in Kotlin.
  • Automatic conversion java to Kotlin.
  • Kotlin has dropped new keyword of java.
  • Kotlin has dropped static keyword of java.
  • It uses Safe call operator (.?).
  • Characters are not number in Kotlin
  • Octal literals are not supported.
  • Extension functions, you can add methods to classes without changing their source code.
  • Kotlin has data keyword to create model classes.
  • It uses a colon (:) operator instead of extends keyword of Java.
Toolchain

Kotlin

Kotlin source code converts to bytecode after compilation which can run on JVM or JavaScript source code.

Setup IntelliJ IDE for Kotlin

You can use IntelliJ, Android Studio, and Eclipse for Kotlin programming.

 Here are the steps to setup kotlin with Intellij IDE.

  1. Make sure your system have 7 or latter version.
  2. Download and install Intellij IDE.
  3. Now click on IntelliJ IDE desktop icon to launch it.
  4. Create new project as,

    Kotlin
  1. Select Project Category then click on next.

    Kotlin

  2. Give the project name,location and SDK path. Then click on finish.

    Kotlin

  3. Now create kotlin file on src folder using IDE

    Kotlin 
  1. Now add your hello world program as below and run by right clicking and choosing run menu or play button from standard tool bar,

    Kotlin
Basics Keywords

Kotlin has following tokens which are interpreted as hard keywords – that can’t use as identifiers.

IntFloatDoubleShortLong
AsbreakContinueClassdo
ElseForFunIfin
!ininterfaceIs!isnull
ObjectpackageReturnSuperthis
ThrowTrueTryTypealiasVal
VarwhenWhileas?False
ByteBoolean   

And soft keywords – that can use as identifiers.

 BycatchConstructorDelegatedynamic
FieldFileFinallyGetimport
InitpropertyParamReceiverset
Setparamwhere   

Modifier keywords

AbstractannotationCompanionConstcrossinline
DataenumExternalFinalinfix
InlineinnerInternalLatinitnoinline
OpenoperatorOutOverrideprivate
protectedpublicReifiedSealedsuspend
Tailrecvarag   

Datatype

TypeWidth in Byte
Int4
Long8
Float4
Double8
Short2
Byte1

Operators

OperatorsDescription
+,-,*,/,%Arithmetical operators
+=,-=,*=,/=,%=Augmented assignment operators
++,--Increment and decrement operators
&&,||,!Logical and,or,not operators
==,!=Equality operators
===,!==Referential equality operators
<,>,<=,>=Comparison operators
[,]Index access operatos
!!Asserts that an expression in not null
?.Perform a safe call
?:Takes the right hand value if left hand value is null
::Crates a member or class reference.
..Creates a range
:Seprate a name from type declaration
?Marks a type is nullable.
->Separates a parameter and body of lambda expression.
@Introduce an annotation
;Separates statements
$Reference a variable or statement in string tamplates.
_Substitute an unused parameter.
ShlSigned Shift left
ShrSigned shift right
UshrUnsigned shift right
AndBitwise and
OrBitwise or
XorBitwise xor
InvBitwise inversion

variable

You can use var keyword to create variables .

Syntax

<access specifiers>  var  <variable name> : < datatype>=<value>;

Literal Constant

  • Decimals - any number i.e 1234,Long literals are indicated by ‘L’   like 123L
  • Hexadecimals -  0x0F
  • Binaries - 0b000001101
  • Doubles -  212.7 or 212.5e5
  • String - “hello kotlin” or “ ” ” (triple quotes for raw string.) 
  • Octal literals are not supported by Kotlin
  • Underscore can use for numeric literals i.e val tenthousand=10_000
  • Characters are not number in kotlin

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now