Quick overview of Basic Synatx
Quick Over view of Basic Syntax
- Comments
- Variables
- Functions
- Class
- Constructors
Comments
Comments are used describe the purpose of the statements or functions. We have 2 types of comments. Comment are ignored by the compiler.
- Inline Comment or Single line comment
- Multi line comment
Inline Comment
Inline comments are used to write single line comments and these are written by using two forward slashes.
Ex:
fun main(args:Array<string>)
{
print("Hello world") //This is inline comment
}
Multi Line Comment
Multi line comments are used to write documentation or to describe the functionality of a method. Mainly multi Line comments are used when the description is not suitable with in one line multi line comments are used.
Ex:
/*
*
*This is a multi line comment and here main is method and an entry point to the kotlin programming
*
*/
fun main(args:Array<String>)
{
print("Hello world") //This is inline comment
}
Variables
In kotlin var is keyword, which is used to define or declare the variables. You can define the constant values by using the keyword val.
Ex:
fun main(args:Array<String>)
{
var myString="Kotlin String"
var myInt=10
var myDecimal=10.5
//Declare and initialize later
var myKotlinString:String
var myKotlinInt:Int
myKotlinString="Kotlin after initialized string"
myKotlinInt=15
//Declaring constant values
val myConstString="constant"
myConstString="cannot change"// Re assigning the variable which is declared using val key word the intellisence will throw error
println(myString)
println(myInt)
println(myDecimal)
println(myKotlinString)
println(myKotlinInt)
println(myConstString)
}
press Shift+F10 to run the programme
Output:
Kotlin String
10
10.5
Kotlin after initialized string
15
constant
Functions
Functions are nothing but set of statements to perform some operation. we can pass parameters to the functions based on its method signature. The syntax to specify the parameters in a method signature is variable name followed by colon followed by variable type.
Ex:
//Entry poin of the programme
fun main(args:Array<String>)
{
var name:String
name="Kotlin Programmer"
//Calling function
displayName(name)
}
//Called function
fun displayName(name:String)
{
println("function called with name: "+name)
println("function called with name: $name")
}
Output:
function called with name: Kotlin Programmer
function called with name: Kotlin Programmer
Here the both println statements are works same but in the second statement the kotlin programming uses string interpolation .
Class
kotlin programming to write a class and calling its function
fun main(args:Array<String>)
{
var programmerObj=Programmer()
programmerObj.writeCode()
}
class Programmer
{
fun writeCode()
{
println("write code completed")
}
}
output:
write code completed
Constructor
In kotlin is constructor is little different to compare with java constructor. we can pass the parameter values at the time of class declaration only.
fun main(args:Array<String>)
{
var programmerObj=Programmer("Write code completed")
programmerObj.writeCode()
}
class Programmer(var message:String)
{
fun writeCode()
{
println(message)
}
}
Output:
Write code completed
Calling class files in kotlin
Right click on the src under project select new select kotlin file/class like bellow
The kind you have to select class
myFirstKotlinFile.Kt
Note:This file is created in previous examples if you have not seen those article just create file like class file but kind must be the file
fun main(args:Array<String>)
{
var programmerObj=Programmer()
programmerObj.writeCode()
}
//Programmer Class file code
class Programmer { fun writeCode() { println("Write code completed") } }
Output:
Write code completed
Note:Here the kotlin file and class file both are in same package so there is no need to import anything.
Creating package and calling class files in kotlin
Right click on the src under project select new select package like bellow
Give the package name and click ok.
If you want to create sub package right click on the package name and select new select package.
Give the package name click ok
To create the class file right click on the above created package select new and select kotlin file/class like same as above
src/myFirstKotlinFile.kt
import com.kotlin.Programmer fun main(args:Array<String>) { var programmerObj= Programmer() programmerObj.writecode() }
Note:Here we are calling the programmer class from out side of the package so must write
import statement.By Pressing alt + Enter is the short cut to import statements.
src/com/kotlin/Programmer
package com.kotlin class Programmer { fun writecode() { println("Write code completed") } }
Output:
Write code completed
Comments
Post a Comment