Kotlin Tutorial (Programing Language)
By: Luis A. Sierra

Introduction

Kotlin is a modern, trending programming language.

Kotlin is easy to learn, especially if you already know Java (it is 100% compatible with Java).

Kotlin is used to develop Android apps, server side apps, and much more

What is Kotlin

Kotlin is a modern, cross-platform programming language developed by JetBrains and released in 2011. Although designed to run on the Java Virtual Machine (JVM), Kotlin already supports platforms such as Android, iOS, and the web.



Kotlin is widely used for Android due to its interoperability with Java, its conciseness, and its excellent security features.

History


Kotlin was developed by JetBrains in 2010. They initially released it under the name Project Kotlin in July 2011. They needed a language that was concise, elegant, expressive, and also interoperable with Java, as most of their products were developed in Java, including the Intellij Idea. They were looking for an alternate language to reduce the amount of boilerplate code required and to introduce new constructs, such as higher-order functions, to make language more expressive and concise. One of the goals of the Kotlin language was to be able to compile code as quickly as Java.

JetBrains open-sourced the project under the Apache 2 license in February 2012. Kotlin v1.0 was released on February 15, 2016.

Kotlin Get Started

Hello, World! It is the first basic program in any programming language. Let's write the first program in the Kotlin programming language.

fun main() {
     println("Hello, world!!!")
}

Copy the code below and run it at: https://play.kotlinlang.org


Kotlin Comments

Comments can be used to explain Kotlin code, and to make it more readable. It can also be used to prevent execution when testing alternative code.

Single-line comments starts with two forward slashes (//).

fun main() {
    // This is a comment 
    println("Welcome to http://interesting-homemade-projects")
}

Copy the code below and run it at: https://play.kotlinlang.org


Multi-line comments start with  /* and ends with */.

fun main() {
    /* The code below will print the url of my web site
       to the screen */
    println("Welcome to http://interesting-homemade-projects")
}

Copy the code below and run it at: https://play.kotlinlang.org


Kotlin Variables

Variables are the "place" where we can store the data that the program needs.

To create a variable, use var or val, and assign a value to it with the equal sign (=):


fun main() {
   var  Thousand = 1000;
   println(Thousand)
   
   val Pi = 3.14;
   println(Pi)
   
   var AuthorName = "Luis A. Sierra";
   println(AuthorName)
    
}

Copy the code below and run it at: https://play.kotlinlang.org



Note:

The difference between var and val is that variables declared with the var keyword can be changed/modified, while val variables cannot.

Case Sensitivity

Case sensitivity defines whether uppercase and lowercase letters are treated as distinct or equivalent.

"kotlin : is case sensitive"



Kotlin Data Type

In Kotlin, the type of a variable is decided by its value:

fun main() {

    val id = 10                                // Int
    val percentage = 18.55            // Double
    val gender = 'M'                      // Char
    val isVerified = true                // Boolean
    val name = "Luis A. Sierra"    // String    

    println("Id: " + id)
    println("Name: " +name)
    println("Percentage: " + percentage)
    println("Gender: " + gender)
    println("Verfied: " + isVerified)
    
}

Copy the code below and run it at: https://play.kotlinlang.org

However,it is possible to specify the type if you want:
fun main() {

    val id : Int = 10                                       // Int
    val percentage  : Double  = 18.55          // Double
    val gender : Char = 'M'                          // Char
    val isVerified  : Boolean = true              // Boolean
    val name : String =  "Luis A. Sierra"     // String    

    println("Id: " + id)
    println("Name: " +name)
    println("Percentage: " + percentage)
    println("Gender: " + gender)
    println("Verfied: " + isVerified)
    
}

Copy the code below and run it at: https://play.kotlinlang.org
Sometimes you have to specify the type, and often you don't. Anyhow, it is good to know what the different types represent.



Type Conversions

Is the process of converting one data type to another. The type conversion is only performed to those data types where conversion is possible.

The following table lists some of the built-in methods from the Convert class that you can use.


Example:

fun main() {
  val num : Int = 10
  val dec : Double =  5.25 
  val boolean : Boolean =  true
    
  println(num.toString())      //Convert to String
  println(num.toDouble())    //Convert to Double
  println(dec.toInt())             //Convert to Int
  println(boolean.toString()) //Convert to String
}

Copy the code below and run it at: https://play.kotlinlang.org


MORE EXAMPLES:

* Convert to String


* Convert to Integer


* Convert to Double


* Convert to Boolean


* Convert to Date


* Convert to Time


* Convert to DateTime


* Format DateTime


* DayName of DateTime


* Convert to ASCII


* String Replace


* String Length


* String Copy

Kotlin Array

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

val names = arrayOf("Luis A. Sierra""Maria Ramirez", "Jose Valdez""Roberto Baez")
println(names[0])

Copy the code below and run it at: https://play.kotlinlang.org


Array Length / Size


val names = arrayOf("Luis A. Sierra""Maria Ramirez", "Jose Valdez""Roberto Baez")
println(names.size)

Copy the code below and run it at: 
https://play.kotlinlang.org


Operators

Is a character that represents a specific mathematical or logical action or process.

In Kotlin different types of operators are available; those are:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators


Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.


Assignment Operators


Assignment operators are used to assign values to variables.


Comparison Operators

Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions.


Logical Operators

Logical operators are used to determine the logic between variables or values:


if/else Statement

The if-else statement is used to execute both the true part and the false part of a given condition. If the condition is true, the if block code is executed and if the condition is false, the else block code is executed


WHEN Statement

The WHEN statement is subordinate to a SELECT statement and is always followed by an expression enclosed in parentheses.


While Statement

A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.


For Statement

The for statement, also called the for loop, lets you repeat a statement or compound statement a specified number of times.

Traditional for:


For with ranges:


Function

A function, subprogram, procedure, method, routine or subroutine is a callable unit that has a well-defined behavior and can be invoked by other software units to exhibit that behavior.

Note: functions return and not return a value.


Class

A class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).


Constructor

In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of function called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.


Properties

Properties are special attributes or characteristics associated with an object. They encapsulate data and provide access control through getter and setter methods, allowing you to interact with the object's state without directly accessing its variables.



Inheritance

Inheritance is one of the core features of object-oriented programming. It's a programming procedure that allows you to reuse code by referencing the behaviors and data of an object. In other words, a class that inherits from another class shares all the attributes and methods of the referenced class.


Exception handling

Exception is typically used to denote a data structure storing information about an exceptional condition. One mechanism to transfer control, or raise an exception, is known as a throw; the exception is said to be thrown. Execution is transferred to a catch.

Post a Comment

Previous Post Next Post