• Kotlin - Tutorials

    Kotlin Basics – Kotlin String

    String is a sequence of characters. In this guide, we will see how to declare, use and manipulate strings in Kotlin. Declare a String in Kotlin There are whole bunch of ways we can define a String in Kotlin. Lets have a look at the following example, here we have declare two immutable strings website & longString and we have also declared two mutable strings name & lName. package tejprogramming fun main(args : Array<String>){ /** * These Strings are Immutable which * means they are read-only and * unchangeable */ val website = "Tej Sumeru" /** * This is how we declare long strings */ val longString…

  • Kotlin - Tutorials

    Kotlin Basics – Kotlin Keywords

    [vc_row][vc_column][vc_column_text]There are certain words in Kotlin that have special meaning and cannot be used as identifiers(variable name, function name, class name etc). These words are called reserved words or keywords. In this guide, we will learn about keywords and identifiers.[/vc_column_text][vc_custom_heading text=”Types of Keywords in Kotlin “][vc_column_text]We have two types of keywords: 1. Hard Keywords 2. Soft Keywords[/vc_column_text][vc_custom_heading text=”1. Hard Keywords”][vc_column_text]These keywords cannot be used as identifiers. For example This is valid: //valid variable name val myvar = 5 This is invalid: //error: "else" cannot be used as a variable name val else = 5 Kotlin Hard keywords Table as class break…

  • Kotlin - Tutorials

    Kotlin Tutorial for Beginners | Learn Kotlin

    [vc_row][vc_column][vc_column_text]Kotlin is a statically-typed programming language, developed by JetBrains. If you have basic knowledge of Java, you will be able to learn Kotlin in no time. This Kotlin tutorial is designed for beginners so you would be able to understand Kotlin programming even if you have no knowledge of Java. Kotlin and Java are interoperable which means you can use them together in a Project as well as you can re-write a Java code in Kotlin efficiently. The syntax of Kotlin is concise than Java. In this tutorial you will learn why use Kotlin, what are the advantages of it and several guides…

  • Kotlin

    Variables

    Kotlin has powerful type inference. While you can explicitly declare the type of a variable, you’ll usually let the compiler do the work by inferring it. Kotlin does not enforce immutability, though it is recommended. In essence use val over var. var a: String = "initial" println(a) val b: Int = 1 val c = 3 Declares a mutable variable and initializing it. Declares an immutable variable and initializing it. Declares an immutable variable and initializing it without specifying the type. The compiler infers the type Int. var e: Int println(e) Declares a variable without initialization. An attempt to use the variable causes…

  • Kotlin

    Functions with vararg Parameters

    Varargs allow you to pass any number of arguments by separating them with commas. The vararg modifier turns a parameter into a vararg. This allows calling printAll with any number of string arguments. Thanks to named parameters, you can even add another parameter of the same type after the vararg. This wouldn’t be allowed in Java because there’s no way to pass a value. Using named parameters, you can set a value to prefix separately from the vararg. At runtime, a vararg is just an array. To pass it along into a vararg parameter, use the special spread operator *…

  • Kotlin

    Operator Functions

    Certain functions can be “upgraded” to operators, allowing their calls with the corresponding operator symbol. This takes the infix function from above one step further using the operator modifier. The operator symbol for times() is * so that you can call the function using 2 * “Bye”. An operator function allows easy range access on strings. The get() operator enables bracket-access syntax. operator fun Int.times(str: String) = str.repeat(this) println(2 * "Bye ") operator fun String.get(range: IntRange) = substring(range) val str = "Always forgive your enemies; nothing annoys them so much." println(str[0..14])  

  • Kotlin

    Infix Functions

    Member functions and extensions with a single parameter can be turned into infix functions. Defines an infix extension function on Int. Calls the infix function. Creates a Pair by calling the infix function to from the standard library. Here’s your own implementation of to creatively called onto. Infix notation also works on members functions (methods). The containing class becomes the first parameter. Note that the example uses local functions (functions nested within another function). fun main() { infix fun Int.times(str: String) = str.repeat(this) println(2 times "Bye ") val pair = "Ferrari" to "Katrina" println(pair) infix fun String.onto(other: String) = Pair(this,…

  • Kotlin

    Default Parameter Values and Named Arguments

    A simple function that takes a parameter of type String and returns Unit (i.e., no return value). A function that takes a second optional parameter with default value Info. The return type is omitted, meaning that it’s actually Unit. A function that returns an integer. A single-expression function that returns an integer (inferred). Calls the first function with the argument Hello. Calls the function with two parameters, passing values for both of them. Calls the same function omitting the second one. The default value Info is used. Calls the same function using named argument and changing the order of the arguments. Prints…

  • Kotlin

    Hello World

    fun main() { println("Hello, World!") } An entry point to a Kotlin application is the main function. In Kotlin 1.3, you can declare main without any parameters. The return type is not specified, which means that the function returns nothing. println writes a line to the standard output. It is imported implicitly. Also note that semicolons are optional. In Kotlin versions earlier than 1.3, the main function must have a parameter of type Array<String>. fun main(args: Array<String>) { println("Hello, World!") }