LEARN
KOTLIN AND PYTHON
QUICKLY
CODING FOR BEGINNERS
WITH HANDS ON PROJECTS
BY
J J TAM
LEARN KOTLIN
QUICKLY
CODING FOR BEGINNERS
WITH HANDS ON PROJECTS
BY
J J TAM
Introduction to Kotlin
Kotlin is a high-level programming language runs on JVM. Kotlin compiles the sources code into java byte code and runs the application using jvm. You can use java and kotlin interchangeably, means you can call a kotlin function from Java and vice versa.
Prerequisites
Java fundamentals are necessary.
Is Kotlin Object oriented?
Yes, Kotlin is object oriented language, you can create classes, objects, and apply oops concepts like polymorphism, inheritance etc.,
Is kotlin functional?
Yes, Kotlin is a functional language. In functional programming lngauge, functions are basic building blocks, you can return a function, pass a function as argument to other function etc.,
Is Kotlin open source?
Yes
What kind of application Can I develop using Kotlin?
- Build Mobile Applications
- Develop server side applications
- You can develop any application, that can be developed in Java
Can I run kotlin code in web browser?
Kotlin can be compiled to JavaScript, so you can run the applications in browser.
Is Kotlin statically typed?
Yes kotlin is statically typed language. For statically typed languages, type of the variable is known at compile time. You can refer my post statically vs dynamically typed languages to know the difference between statically and dynamically typed languages.
Can kotlin infer type?
Yes, unlike Java, you no need to specify the type while defining the variable. Kotlin infer the type based on the context.
Ex
var name = "JJTam"
Since "JJTam" is assigned to the variable name, koltin infer the type as String.
var age = 28
Since number 28 is assigned to the variable age, kotlin infer the type as int.
Install Kotlin
Kotlin can be installed in command line mode (or) can be installed in IDEs like Eclipse, Intelij.
a. Install Kotlin in command line mode
b . Install Kotlin in Eclipse
c . Install Kotlin in Intelj
Installing Kotlin in command line mode
Download kotlin from below location. At the time of writing this article, 1.1.4 is the latest version.
https://github.com/JetBrains/kotlin/releases/tag/v1.1.4
Unzip the downloaded zip file, and add the bin directory to system path.
Open new command prompt and type kotlinc command.
C:\>kotlinc
Welcome to Kotlin version 1.1.4 (JRE 1.8.0_102-b14)
Type :help for help, :quit for quit
>>>
As you see above output, it shows the kotlin version and underlying jre version.
Type the command :help to list available commands.
C:\>kotlinc
Welcome to Kotlin version 1.1.4 (JRE 1.8.0_102-b14)
Type :help for help, :quit for quit
>>>
You can evaluate expressions in Kotlin prompt.
>>> var a =
>>> var b =
>>> a + b
>>> a-b
-
>>> a*b
>>> a/b
>>> println(a)
>>> println( "value of b : " + b)
value of b :
Kotlin: Hello World program
.kt is the extension for kotlin files. Create a file HelloWorld.kt with below content.
HelloWorld.kt
fun main (args:Array){
println( "Hello World" )
}
Keyword fun is used to define a function.
main is the main function of the application. Program execution starts from here.
args:Array
In Kotlin variable name is declared first, followed by the type. In above case, args is variable of type array of strings.
println("Hello World")
Above statement prints the message Hello World to the console. As you see, unlike java, Kotlin statements do not end with ;.
Compile HelloWorld.kt
Open command prompt and type the command kotlinc HelloWorld.kt.
C:\Users\JJTam\Documents\Study\Kotlin\Programs>kotlinc HelloWorld.kt
C:\Users\JJTam\Documents\Study\Kotlin\Programs>dir
Volume in drive C is OSDisk
Volume Serial Number is 1034-4F6F
Directory of C:\Users\JJTam\Documents\Study\Kotlin\Programs
09/02/2017 11:09 AM 57 HelloWorld.kt
09/02/2017 11:18 AM 980 HelloWorldKt.class
2 File(s) 1,037 bytes
3 Dir(s) 63,190,188,032 bytes free
As you see the output of dir command, you can observe, kotlin generates HelloWorldKt.class and a META-INF directory.
Run HelloWorld.kt
You can run the HelloWorldkt file using kotlin HelloWorldKt command.
C:\Users\JJTam\Documents\Study\Kotlin\Programs>kotlin HelloWorldKt
Hello World
Creating and run jar
You can also create a jar file and run the jar file using java launcher.
Use the command kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar
to create a jar file.
The include-runtime option tells kotlin compiler, include kotlin run time in HelloWorld.jar file.
C:\Users\JJTam\Documents\Study\Kotlin\Programs>kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar
C:\Users\JJTam\Documents\Study\Kotlin\Programs>dir
Volume in drive C is OSDisk
Volume Serial Number is 1034-4F6F
Directory of C:\Users\JJTam\Documents\Study\Kotlin\Programs
09/02/2017 11:24 AM 869,262 HelloWorld.jar
09/02/2017 11:09 AM 57 HelloWorld.kt
2 File(s) 869,319 bytes
2 Dir(s) 63,401,816,064 bytes free
Once the jar file is created, you can call the jar file, just like how you run other jar files. Use the command java -jar HelloWorld.jar
C:\Users\JJTam\Documents\Study\Kotlin\Programs>java -jar HelloWorld.jar
Hello World
Setting up Kotlin in Eclipse
Open Eclipse. Help -> Eclipse Marketplace.
Search for kotlin.
Click on the button Install to install the kotlin plugin.
Once the installation is successful, Eclipse asks for a restart. Restart the Eclipse.
Create new Kotlin Project
Open Eclipse. File -> New -> Other.
Kotlin -> Kotlin Project.
Press Next.
In the Kotlin Project window, give the project name as Hello World and click on the button Finish.
Kotlin project structure looks like below.