Kotlin

Kotlin - Basic Syntax

Kotlin - Basic Syntax

Knowing the basic Kotlin syntax is the first step towards Kotlin programming. The Kotlin syntax does not call for the semicolon(;) to be a compulsion for ending a statement as in Java C++, C#, etc.  A Kotlin statement works the same way without a semicolon as it does with a semicolon. The entry point in the case of Kotlin programming is the main() function. You must already be knowing that a function refers to a block of code that performs a particular task and main() is a driver function. Look at the following code snippet for better understanding:

  • Argumented Entry Point

This main() function can be made to accept a number of string parameters too as shown in the following example:

Output:

fun main(args: Array<String>) {
    println("Hello, world!!!")
}

 

As you can see, both the codes produce the same output. We can say, for ordinary programs, passing argument in the main() function is optional. 

print() and println():

In Kotlin, print() and println() both the functions are used to print or display the output on the screen.  The former prints its argument to the standard output without any line break, whereas the latter displays its arguments to the standard output along with a line break. Hence, it is desirable to use println() when we want a line break too, else you can use print().  Though, both the functions can be used to print characters, strings, and numbers, either via variables used to store the result or by directly passing the arguments in the form of alphanumeric string, numeric values, or mathematical expressions. Let’s understand the difference between the two functions better by trying them out on the console. 

Example-1

 

fun main(args: Array<String>) {

  //using println()

    println("Hello, ")

    println("I'm Sammy.")

   //using print()

    print("Hello, ")

    print("I'm Sammy.")

}

Example-2

 

fun main(args: Array<String>) {

  //using println()

    println(50)

    println(2+2)

   //using print()

    print(60)

    print(6>4)

}

Example-2

 

fun main(args: Array<String>) {

  //using println()

    println(50)

    println(2+2)

   //using print()

    print(60)

    print(6>4)

}

Package and Import Statements

Packages are used to group related classes together. Packages are specified using the package keyword. Unlike Java, Kotlin does not mandate for the package to match the directory the file is located in. In a Kotlin source file, whether you specify a package or not, it is up to you. Not specifying a package means you want the contents of the Kotlin source file to go to the default package. A package declaration is done at the beginning of a Kotlin file. It indicates that the file may contain declarations of classes, functions, or interfaces of the specified package. Take a look at the following example to see how a pckage declaration is made. 

package com.greatlearning.kotlintutorial
fun show() {
  println("Package declaration!")
}
fun main(){
com.greatlearning.kotlintutorial.show()
}

In the above example, first, a package has been declared. The function show() belongs to the package com.greatlearnin.kotlintutorial, hence it is called in the rest of the program as com.greatlearning.kotlintutorial.show(). Therefore, the following output is generated. 

Another way to use this show() function is via an import statement.  In Kotlin, you can import classes, top-level functions and properties, functions and properties declared in object declarations, and enum constants. Hence, one can also import the show() function into the scope of any other Kotlin program. Kotlin does not have any import static syntax. You can choose to rename a class while importing it into your code by using as keyword like in the example below:

import java.lang.Integer.MAX_VALUE as max_num    //Renaming

fun main(){
    print(max_num)
}

Now, know that there are some default imports in your Kotlin file. Think of being able to use println() function without writing the import statement for the package the kotlin.io package or writing System.out.println every single time in a Kotlin program. It is so because the corresponding package has already been imported by default in the beginning of the Kotlin file. Every Kotlin file consists of following default imports:

  • kotlin.*
  • kotlin.collections.*
  • kotlin.annotations.*
  • kotlin.io.*
  • kotlin.comparisons.*
  • kotli.text.*
  • kotlin.sequences.*
  • kotlin.ranges.*

Along with the above listed packages, java.lang.* and kotlin.jvm.* for JVM, and kotlin.js.* for JS, are also imported to a Kotlin file by default.