Hey Developers, welcome back to part 3 of Kotlin tutorials. In this post, we will quickly get an overview of the basic Syntax of Kotlin programming such as Class, Function, Variable, Constructor, and Comment.
This post is just a quick start of upcoming posts, in the upcoming post we will know more about functions, classes and etc. so this post makes you comfortable with the Kotlin language, so without any further discus, let’ see Kotlin Syntax step by step.
Page Contents
Quick overview of the basic Syntax of:
- Comment
- Variable
- Function
- Class
- Constructor
Step 1. First, open IntelliJ Idea or Android Studio.
Step 2. Go to > src > create new class > write some code like:
fun main() { print("Hello World") }
First, let see how we do the comment line in the Kotlin.
1. Comment Syntax
So in order to write an inline comment, you can simply use double forward-slash (//) such as:
fun main() { print("Hello World") }
You can also write multiple comments like:
/* this is inline comment 1 * this is inline comment 2 * this is inline comment 3 * this is inline comment 4 * this is inline comment 5 */ fun main(){ print("Hello World") }
Whatever statement you will write withing two apstring and forward slash, combine won’t execute this line of the code, This will be ignored during this compile times,
2. Variable and Data Type Syntax
In Kotlin we used two types of keywords for declaring of variable-
- var
- val
var- You can reassign a value.
val- You can not reassign a value, it can be assigned only once.
Let’s see how we can use variables in the case of Kotlin. Go to IntelliJ idea or Android Studio, create a class and write the main function.
fun main() { print("Techpass Master") }
In the above code, you will see, we are directly printing Techpass Master(“Techpass Master”), Now suppose if we want to define a string so what is the syntax of defining the variable, so for that in this main function we have to use the keyword of var or val like var myDomain = “Techpass Master”
fun main() { var myDomain = "Techpass Master" print(myDomain) // in the print stetment simply pass (myDomain) }
Now run the code and you will get the output:
Techpass Master
Same, you will define other data types like Int, Float, Double and more.
fun main(){ var myString = "Hello Word" // String Data Type var myInt = 10 // Int Data Type var myFloat = 20.0 // Float Data Type var myDouble = 50.00000 // Double Data Type println(myString) // print string value println(myInt) // print int value println(myFloat) // print float value println(myDouble) // print double value }
Run the program and you will get the output like this:
Hello Word 10 20.0 50.0
It is the basic syntax of variable, Now let see the next syntax
3. Function Syntax
In the Kotlin we are declared Function by using the fun keyword we will more discuss functions in the upcoming post. Now let’s see the basic syntax of the function. Write the main function first.
fun main() { var myString = "Hello Word" println(myString) // print string value }
In the above code, you will see, we are printing some string values directly in the main function, suppose if you want to print this string value from a separate function, for that we will create a function of the downside of the main function.
fun main() { var myString = "Hello Word" // string value myPrintFuction(myString) // call the fuction } fun myPrintFuction(myString:String ) { println(myString) }
When you call the myPrintFuction on the main function you will get the output like this:
Hello Word
Now let’s discuss about the class:
4. Class Syntax
In Kotlin we are declared class by using the class keyword. This is a short overview of the class in the upcoming post we will more discuss about the class. Now let’s see the basic syntax of class, suppose if you want multiple functions and variables in one place then you can use class.
fun main() { // call fist fuction for sum of two number var addNumber= MyDemoClass() // create object of the class addNumber.sum(10,10) // call fuction with the help of class object // call second fuction for multiply of two number var multiplyNumber= MyDemoClass() // create object of the class multiplyNumber.multiply(10,10) // call fuction with the help of class object }
class MyDemoClass() { // fist fuction for sum of two number fun sum(a:Int,b:Int){ println(a+b) } // second fuction for multiply of two number fun multiply(a:Int,b:Int){ println(a*b) } }
Run the code, you will get the output:
20 100
5. Class with Constructor Syntax
fun main() { var personName= MyDemoClass("Xyz") // create object of the class personName.displayName() // call fuction with the help of class object } // class with constructor class MyDemoClass(val person:String) { // define variable name fun displayName(){ println("The name of the person is ${person}") // here we are using STRING INTERPOLATION(${}) instead of + } }
Run the code, you will get the output:
The name of the person is Xyz
I hope you like the post, in the upcoming post we will discuss each and everything lot more detail.
If you have any questions regarding this post. Feel free to comment and share the post with other developers. Happy Learning!!!
Read Next ⇣