Introduction to Kotlin

Introduction 
 
This post will show the basics of the Kotlin language, including basic syntax and functions.
 
All the methods in Kotlin will start with the 'fun' keyword. Let's create the main function like this in Kotlin
  1. fun main(args: Array<String>){  
  2. }   
Now here within the print function, we can directly print the "hello world" string.
  1. fun main(args: Array<String>)  
  2.  {  
  3.     print("Hello World")  
  4. }  
Suppose you want to define a string, then what is the syntax?
 
For that, we have to use the keyword var <space> variable name = “string”
  1. var myString   = "hello word"   
And call the print function and pass myString as a parameter
  1. print(myString)  
Then run the code so in the output we get 'hello world.'
 
So in the case of Kotlin, we can define any variables with this keyword. Suppose you want to make it myNumber = 10 and now simply pass myNumber in the print function. Whatever value that you define here determines the data type of this variable. So here this myNumber automatically becomes integer value. Suppose for example you want to define a variable let's say myString and now suppose you don't want to initialize your string variable here. For that, what can you do?
 
You can simply declare the type of this variable by simply using the semicolon and use the keyword string and later on you can simply define the value of this myString variable to any string values.
 
Var <variable_name> : <data_type> = <value>
  1. var myString : String  = "hello word"  
  2. print(myString)   
In just one statement, you define that this is the variable of the type of string and later on, you decided to initialize it with this value of 'hello world'. Here this string is actually mutable in nature that is at any point of time we can simply modify this variable let's say myString = “another world”
 
Now, this value of this variable has been changed from this hello world to another world now.
 
If you want to fix the value of this variable as hello world only so that later on nobody can change this value for that purpose what you will do you?
 
We can simply use the keyword, Val. Let's say anotherString and then simply use it to define the value let's say my constant string. Later on, if you try to modify this anotherString to some other value then the intelligence IDE will simply show some error this anotherString cannot be reassigned to some other string value. Because here we are using the val keyword, so anotherString cannot be modified at any other point.
 
Here we can say this is our constant value and now we want one whose value can be changed later on.
  1. val anotherString : String  = "my constant string"  
Now suppose we want to print your name with the help of some function defined inside my file.
 
For that, I will simply create one more function let's say display use the bracket open and close and define the method body now inside this let us use print and here let us print Pranav.
 
Now my objective is that I want to pass this value here inside the display method so for that what I will do?
 
I will simply define a variable let's say name : String = “Pranav” in order to define any variable along with the data type then the syntax is this first you need to define the variable name followed by a colon and then define the data type.
 
Here instead of printing the “Pranav” directly, we can simply print out name and now let us replace this statement by display and then simply pass named variable so when this statement will be executed in the run time this Pranav value which is present will fall here and therefore, it will be printed inside the display method.
  1. fun main(args: Array < String > ) {  
  2.     val name: String = "Pranav"  
  3.     display(name)  
  4. }  
  5. fun display(message: String) {  
  6.     print(message)  
  7. }   
We will see more on this subject in the next article.


Similar Articles