Learn basic Kotlin programming: basic syntax for Kotlin - AkuCode




Hello World Kotlin
            We can try Kotlin online on its official website https://try.kotlinlang.org/which has been provided by Kotlin's own developers. For the first time, we can try to create the text "Hello, World".

Variables and Basic Data types
            A variable is a place to hold value or data. The analogy is like an empty glass that can be filled with any (Varia-able). It can be filled with tea, can be filled with white water, can be filled with milk, can be filled with numeric values, can be filled with a String value, and many others. Kotlin recognizes two variables, mutable and immutable. What is it?

·       Mutable Variable (var)
It is a variable that allows it to be changed in value after it is created. Defined with the keyword var. here's an example of use:

·       Immutable Variable (Val)
Variable is the opposite of a mutable variable where the value of a variable cannot be changed once it is created. Immutable variables are defined by the Val keyword. usage example:

            There are two types of data on the barcode above "var" & "Val". What's different..?

Var is a type of data that can be variable or we can change the data in the Var with the upcoming nature. While Val is a data type that remains irreversible with the upcoming nature.
Data types in Kotlin
·       Type Basic
The number representation in Kotlin is very similar to Java, however, Kotlin cannot perform internal conversions of different data types. The following table lists different variables.

Length for different numbers
Type
Size
Double
64
Float
32
Long
64
Int
32
Short
16
Byte
8
     In the example below, we'll look at how Kotlin works with different data types.

·       Characters
The Char in Kotlin represents the Character Data type that is declared using one quotation 'c'

·       Boolean
The Boolean data type only has two values "true" and "false" Boolean commonly used to make the decision in the code

·       String
A String is a data type used to store Kalimay/text data. Writing strings in Kotlin uses double quotation marks ("") or three quotations (""") for more than one line sentence.
Example:

·       Arrays
Arrays in Kotlin are represented by an Array class, so it's not actually a native data structure. We can, therefore, call arrays in Kotlin as a collection class. Here's an example of using arrays in Kotlin:

            Indexing arrays are similar to other programming languages. Here, we are looking for the second index, whose value is "3".
·       Collection
The collection is a very important part of the data structure, which makes the development of software easy for the enginers. Kotlin has two types of collections – one can't be changed (list, maps, and uneditable batches) and Collection can change (this type of collection can be edited). It's important to remember which type of Collection we'll be using in our app since the Kotlin system doesn't represent any specific differences in it.

·       Ranges
Ranges are another unique characteristic of Kotlin. Like Haskell, it provides an operator that helps us iterate over a range. Internally, this is implemented using range () and the operator form is (..)


If you have questions please submit in the comments, thank you

Comments