Kotlin Tutorial (Programing Language)
By: Luis A. Sierra
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 moreKotlin 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 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 moreKotlin 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.
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.
Hello, World! It is the first basic program in any programming language. Let's write the first program in the Kotlin programming language.
println("Hello, world!!!")
}
Copy the code below and run it at: https://play.kotlinlang.org
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.
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
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.
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)
}

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.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.
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
* Convert to Integer
* Convert to Boolean
* Format DateTime
* Convert to ASCII
* String Replace
* String Length
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
println(names[0])
Copy the code below and run it at: https://play.kotlinlang.org
val names = arrayOf("Luis A. Sierra", "Maria Ramirez", "Jose Valdez", "Roberto Baez")
Copy the code below and run it at: https://play.kotlinlang.org
println(names.size)
Copy the code below and run it at: https://play.kotlinlang.org
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 are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions.
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
A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.
The for statement, also called the for loop, lets you repeat a statement or compound statement a specified number of times.
Traditional for:
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.
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 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 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.
Tutorial's Files:
Post a Comment