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 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.
- Make sure your system have 7 or latter version.
- Download and install Intellij IDE.
- Now click on IntelliJ IDE desktop icon to launch it.
- Create new project as,
- Select Project Category then click on next.
- Give the project name,location and SDK path. Then click on finish.
- Now create kotlin file on src folder using IDE
- Now add your hello world program as below and run by right clicking and choosing run menu or play button from standard tool bar,
Basics Keywords
Kotlin has following tokens which are interpreted as hard keywords – that can’t use as identifiers.
Int | Float | Double | Short | Long |
As | break | Continue | Class | do |
Else | For | Fun | If | in |
!in | interface | Is | !is | null |
Object | package | Return | Super | this |
Throw | True | Try | Typealias | Val |
Var | when | While | as? | False |
Byte | Boolean | | | |
And soft keywords – that can use as identifiers.
By | catch | Constructor | Delegate | dynamic |
Field | File | Finally | Get | import |
Init | property | Param | Receiver | set |
Setparam | where | | | |
Modifier keywords
Abstract | annotation | Companion | Const | crossinline |
Data | enum | External | Final | infix |
Inline | inner | Internal | Latinit | noinline |
Open | operator | Out | Override | private |
protected | public | Reified | Sealed | suspend |
Tailrec | varag | | | |
Datatype
Type | Width in Byte |
Int | 4 |
Long | 8 |
Float | 4 |
Double | 8 |
Short | 2 |
Byte | 1 |
Operators
Operators | Description |
+,-,*,/,% | 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. |
Shl | Signed Shift left |
Shr | Signed shift right |
Ushr | Unsigned shift right |
And | Bitwise and |
Or | Bitwise or |
Xor | Bitwise xor |
Inv | Bitwise 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