What is Kotlin?
Kotlin is a programming language introduced by JetBrains in2011,
official designer of the smartest Java IDE, called Intellij IDEA. Kotlin is
free, was free and will continue to be free. It is developed under the
Apache 2.0 license and the source code is available on GitHub.
This is a general-purpose programming language:
- Strongly statically typedthat is executed in the JVM
In 2017, Google announced that Kotlin is aofficial language
for Android development.
Combines object-oriented programming and functional resources in
a unique platform
Kotlin focuses mainlya Java Virtual Machine (JVM),but
alsocompile to JavaScript.
Internally orKotlin depends on the Java class library
existing
Kotlin was developed based on Java
Architecture
• Compile Kotlin to bytecode that can be executed on the JVM
• This bytecode is exactly the same as the bytecode generated by the file
Java.class.
• Whenever Kotlin targets JavaScript, the Kotlin compiler
convert the fileThefileextension.kttypicallydenotesaKotlinsourcefile.in ES5.1
• Whenever two byte-encoded files (two programs
different from Kotlin and Java) are executed on the JVM, they can
communicate with each other and this is how an interoperable resource is established
from Kotlin to Java.
Native Kotlin
Kotlin/Native is a technology forcompile Kotlin code into binaries
natives, thatcan be executed without a virtual machineKotlin/Native
supports the following platforms:
• Mac OS
• iOS, tvOS, watchOS
• Linux
• Windows (MinGW)
• Android NDK
• Much more...
It is easy to include compiled Kotlin code in existing projects written
in C, C++, Swift, Objective-C and other languages.
Kotlin program entry point
- It is optional to pass a parameter in the functionmain()from version 1.3 of
Kotlin.
print() vs println()
Point-and-semicolon (;) in Kotlin
Packages in Kotlin
Kotlin code is usually defined in packages, although the specification of the
package is optional
He if you do not specify a package in a source file, its content will
for the standard package
Commentsin Kotlin
Kotlin uses afun keyword to define a function, so that
If we try to use it as a variable name, then it will be an exception. For example:
Variables
Kotlin variables are created using eithervaryouval
Variablesmutáyou see Kotlin
Mutable means that thevariable can be reassigned to a different value after the
assignment startsl.
We use the keywordvar
Variablesread-only Kotlin
A read-only variable can be declared usingval(instead of var)
Once a value is assigned, it cannot be reassigned.
Used to define different constant values, that is, the variables that will hold
your values throughout the program.
Types of variablesKotlin
Kotlinis smart enough to recognizethat 'Zara Ali' is a string and that
19 is a numeric variable.
However,you can explicitly specify a variable typewhen creating it:
Variable naming rulesKotlin
• Kotlin variable names must start with aletter, $ or
underlined
• Kotlin variablesdifferentiate uppercase from lowercase,what
it means that Zara and ZARA are two different variables.
• The Kotlin variable cannot have any whitespace or others.
control characters.
• The Kotlin variablethere cannot be nameshowvariablewhy
they are reserved keywordsin Kotlin.
Numeric data types in Kotlin
Kotlin character data type
- It is used to store a single character and is represented by the keyword
typeChar.
Kotlin string data type
Boolean data type Kotlin
Kotlin array data type
They are a collection of homogeneous data.
Kotlin data type conversion
Kotlindoes not support direct conversionfrom one type of numeric data to another
To convert a numeric data type into another type:
• toByte()
• toShort()
• toInt()
• toLong()
• toFloat()
• toDouble()
• toChar()
Kotlin String Models
Kotlin string length
Lastindexstring Kotlin
Changing uppercase and lowercase of strings
String concatenation
We can use the operator+ to concatenate two strings or we can also use
the functionplus()to concatenate two strings.
Cut characters from a string
Finding a String within a String
Kotlin provides the functionindexOf()to find a text within a string.
Returns the index of the first occurrence of a specified text in a string
Comparing Two Strings
Kotlin provides the functioncompareTo()to compare two strings.
This function returns 0 if two strings are equal; otherwise, it will return 1.
Kotlin if...else Expression
Kotlinif...elseit can also be used as an expression that returns one
value and this value can be assigned to a variable. Below is a syntax
simpler of the expressionif...elsedo Kotlin :
When
Kotlinwhenit can be used as an expression or as an instruction,
just like a switch statement in Java
For Loop
Collections
Kotlin Immutable Collection
Kotlin Mutable Collection
Kotlin Classes
Primary Constructor Kotlin
Initializer block
The primary constructor cannot contain any code.
The initialization code can be placed in initializer blocks.
prefixed with the keywordinit
Secondary Builder
Kotlin allows you to create one or more secondary constructors for your class.
This secondary constructor is createdusing the keyword constructor