Pages

Saturday, June 2, 2018

Learn Kotlin 4 (Basic Types 2 and packages: ,Hi friends this is the continuation of previous post so i kindly request you to read that before this post. As this blog is also prepared for beginners so i advice you that you don't need to by-heart this lessons just read and understand it if you have any doubt ask in the comment section)

Strings

The strings are a sequence of characters either as a literal constant or as some kind of variable in kotlin strings are represented by the type String . strings are immutable and it's characters can be accessed by indexing operation . s[i] a string can be iterated over with a for -loop (for-loop will be explained in the coming lessons ) we can concatenate string using + operator it can also be done for other types also as long the first element in the expression is string here is an example
val a = "abc" + 1
println(a + "nads")

The above code shows how the string abc is concatenated with 1 and then it is printed out as "abc1nads" by concatenating a and nads kotlin has two types of string literals they are escaped string literal and raw strings escaped strings are which that may have escaped characters in them and raw strings are which that can contain new line and arbitary text here is an example of escaped string

val s = "Hello World\n"
here the escaping is done in the conventional way with a backslash
A raw string is delimited by triple quotes contains no escaping and can contain new line any other character example

val o = """for (c in "value")
                     println(c) """
The leading whitespace can be removed using the function trimMargin()
val text = """
                     |this is the first line
                     |this is the second line
                     |this is the third line
                     |this is the fourth line
                 """,trimMargin()

the | is used as margin prefix but you can choose another character and pass it as a parameter like trimMargin('>')

String Template

         strings may contain template expressions means the pieces of code which are evaluated and concatenated with the string which starts with a dollar sign like $a and contains like a simple name or an arbitrary expression in curly braces here is some examples


val i = 14
println("i is $i")
//this prints 'i is 14'
//another one with an arbitrary expression in curly braces
val a = "abc"
println("$a.length is ${s.length}"  )
//prints "abc.length is 3"


Templates are supported both inside the raw string and escape string if you need to represent a literal $ character in a raw string which doesn't support backslash escaping we can use the following syntax given below

val price = """
           $('$')9.99
            """

Packages

The packages is a namespace that contains the set of related classes all the contents such as classes and functions of source file are contained by the package this package is to be declared first in the kotlin project for example
 
package nads.sani
fun me(){}
class myclass{}

So as explained above the me functions full name is nads.sani.me and the full name of myclass is nads.sani.myclass if the package is not specified the contents of such a file belong to default package that has no name. A number of packages are imported to kotlin as default for example
  • kotlin.*
  • kotlin.annotation.*
  • kotlin.collections.*  
  • kotlin.comparison.*
  • kotlin.io.*
  • kotlin.ranges.*
  • kotlin.sequences.*
  • kotlin.text.*
Additional packages are imported depending on the target platform
Like JVM(java):
  • java.lang.*
  • kotlin.jvm.* 
And for Js(Javascript):
  • kotlin.js.*
Imports
Not only default imports there are also other imports can be include in here
We can import either a single name like:

import nads.sani//sani is now accessible without qualification

Or we can include all the accessible files from the nads file by using:

import nads.*//by using the * at last all the files from the nads are included here
And if there is a name clash in our file we can use the as keyword here to use our desired name by changing it to other name in the file for example

import nads.sani //sani is accessible here
import jack.sani as chani  //access the features of jack.sani by using chani

Some facts on imports

  • the import keyword is not only restricted to importing classes can also be used on other declarations like:Top level function and properties,function and properties declared in object declarations,enum constants
  • like in java kotlin doesn't have a "import static" syntax all of this declarations are imported using the regular import keyword

Thats all for this post guys more will be included in the next post hope you like the post to get the latest update on this blog please follow by email or follow me and don't forget to share and comment below 

No comments:

Post a Comment