var Mutable variable The value can be changed var corresponds to a regular non

Var mutable variable the value can be changed var

This preview shows page 2 - 5 out of 14 pages.

var : Mutable variable. The value can be changed. var corresponds to a regular (non-final) Java variable. var name = "kotlin" name = "programming language" Basic Types Numbers : The built-in number types are : long(width:64) Int (width:32) Short (width:16) Byte (width:8) Double (width:64) Float (width:32) Booleans : Represents a value which is either true or false. Chars : Char represent a single character. var name:Char = 'A' Basic Types - Strings Strings : Strings are immutable. String literals are created using double quotes or triple quotes. Double quotes create an escaped string : val string = "string with \n new line" println(string) Output : string with new line
Image of page 2
Triple quotes create a raw string : val string = """string with \n new line""" println(string) } Output: string with \n new line Basic Types - Arrays Arrays : An array can be created by using the library function arrayOf() : var array = arrayOf(1, 2, 3) Arrays can also be created from an initial size and a function, which is used to generate each element : val perfectSquares = Array(5, { k -> k * k }) Kotlin also has specialized classes to represent arrays of primitive types without boxing overhead : ByteArray, CharArray, ShortArray, IntArray, LongArray, BooleanArray, FloatArray, and DoubleArray. String Templates It is an effective way of embedding values, variables, or expressions inside a string without the need for pattern replacement or string concatenation. A variable is embedded by prefixing with a dollar ($) symbol : val name = "folks" val str = "Hello $name" println(str) // Hello folks Arbitrary expressions are embedded by prefixing with a dollar ($) and wrapping in braces {}: val name = "folks" val str = "Hello $name. Folks has ${name.length} characters" println(str) //Hello folks.Folks has 5 characters Packages and Imports Packages allow to split code into namespaces. A source file may start with a package declaration, as shown below : package com.tcs.samples If the package is not specified, the contents of such a file belong to "default" package that has no name. Using import, Kotlin imports : Single package or all the items in the package. Top-level functions and properties. Functions and properties declared in object declarations. enum constants. import com.tcs.samples.* If Expression if is an expression in Kotlin, it returns a value. When Expression The when construct in Kotlin is more powerful than Java s switch. There are two forms of when : The first is similar to switch, accept an argument, and a series of conditions
Image of page 3
are checked against the value : fun whatChar(x: Char) { when (x) { 'a' -> println("x is a") 'z'-> println("x is z") else -> println("X is neither a or z") } } The second is without an argument and uses as an alternative for a series of if...else conditions : fun whatChar(x: Char){ when { (x=='a') -> println("x is a") (x=='z')-> println("x is z") else -> println("X is neither a or z") } } Ranges A range is an interval with a start value and an end value. Any comparable types can be used to create a range, using the .. operator : val aToZ = "a".."z" val oneToTen = 1..10 in operator is used to test whether a given value is included in the range : var isTrue=4 in oneToTen There are two library functions to create ranges : downTo() - create a range counting down rangeTo() - create a range up to a value val countingDown = 10.downTo(1) val rangeTo = 1.rangeTo(5)
Image of page 4
Image of page 5

You've reached the end of your free preview.

Want to read all 14 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture