Sunday, April 5, 2020

Kotlin Concepts and Programming

Kotlin is making waves. The question is why?

Kotlin is syntax friendly, conciseness, flexibility and powerful language. The compiler keeps you safe like unsafe, buggy code and kotline's compiler puts a lot of effort into making sure your code is as clean as possible, preventing many of the errors that can occur in other programming languages.

So Kotlin virtually eliminates the kind of error that regularly occurs in other programming languages that means safer, more reliable code and less time spent chasing bugs.

You can use Kotlin nearly everywhere

This is because you can choose which platform to compile your Kotlin code against.

Java Virtual machines(JVMs)
it can be compiled to JVM bytecode so you can use Kotlin practically anywhere. Kotlin is 100% interoperable with java.

Android
Kotlin has first-class support for Android.

Client-side and server-side JavaScript

you can also transpile -----or translate and compile ---- Kotlin code into JavaScript.So you can run it in a browser. You can use it to work with both client-side and server-side technology such as WebGL or Node.Js

Native Apps
You can also directly compile your kotlin code to native machine code. This allows you to write code that will run for example on iOS or Linux.

Now take a look on few examples:

val name = "Abhinav"  // Declare a variable 'name' and give it a value of "Abhinav"

val height = 9

println("Hellow")

val a = 6
val b = 7

val c = a + b+10
val str = c.toString()

val numList = arrayOf(1,2,3,4,5,6,7)

var x = 0

while(x < 3)
{
   println("Item $x is ${numList[x]}")
   x = x + 1
}

Sample Program: 

App.kt:

fun main(args : Array<String>)
{
  println("ABHINAW")
}

So you can ask a question here like

Do I have to add the main function to every Kotlin file I create?
Ans: No, A kotlin application might use dozens of files but you may only have one with the main function - the one that starts the application.

Now let's run the  above code here:

So when you run the code in IDE :

1) The IDE compiles your kotlin code source code into JVM bytecode.
that means compiling App.kt creates a class file called AppKt.class .

2)The IDE starts the JVM and runs AppKt.class
The JVM translates the Appkt.class bytecode into something the underlying platform understands then runs it.

Bullet Points:


  • Use fun to define a function.
  • Every application needs a function named main.
  • Use // to do single-line comment.
  • A string is a string of characters. You denote a String by closing its characters in double-quotes.
  • Code blocks are defined by a pair of curly braces {}.
  • The assignment operator is one equals ==.
  • The equals operator use two equals signs ==.
  • Use var to define a variable whose value may change.
  • Use all tod define a value whose value will stay the same.
  • you can also use if as an expression so that it returns a value.In this case , he else clause is mandatory.








No comments: