0% found this document useful (0 votes)
7 views

Lecture 1- Kotlin Programming Language Introduction_١١٠٠١٨_065349

Uploaded by

nailapp2023
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture 1- Kotlin Programming Language Introduction_١١٠٠١٨_065349

Uploaded by

nailapp2023
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 75

Kotlin

Programming
Language
An Introduction
Kotlin Syntax
• The fun keyword is used to declare a function. A function is a block of code
designed to perform a particular task. In the example above, it declares the
main() function.

• The main() function is something you will see in every Kotlin program. This
function is used to execute code. Any code inside the main() function's curly
brackets {} will be executed.

• For example, the println() function is inside the main() function, meaning that
this will be executed. The println() function is used to output/print text, and in
our example it will output "Hello World".
Introduction to Kotlin
Programming Language
Kotlin Output
• The println() function is used to output values/print text.
• You can add as many println() functions as you want.
Note that it will add a new line for each function.
• You can also print numbers, and perform mathematical
calculations.
• There is also a print() function, which is similar to
println(). The only difference is that it does not insert a
new line at the end of the output:
Kotlin Output
Kotlin Output
Kotlin Output
Kotlin Comments
• Comments can be used to explain Kotlin code, and to make it
more readable.
• It can also be used to prevent execution when testing alternative
code.
• Single-line comments starts with two forward slashes (//): Any
text between // and the end of the line is ignored by Kotlin (will
not be executed).
• Multi-line comments start with /* and ends with */: Any text
between /* and */ will be ignored by Kotlin.
Kotlin Comments
Kotlin Comments
Kotlin Comments
Kotlin Variables
• Variables are containers for storing data values.
• To create a variable, use var or val, and assign a value to
it with the equal sign (=).
• Syntax:
var variableName = value
val variableName = value
Kotlin Variables
• The difference between var and val is that variables
declared with the var keyword can be changed/modified,
while val variables cannot.
• Variables in Kotlin do not need to be declared with a
specified type (like "String" for text or "Int" for numbers,
if you are familiar with those).
Kotlin Variables
• The difference between var and val is that variables
declared with the var keyword can be changed/modified,
while val variables cannot.
• Variables in Kotlin do not need to be declared with a
specified type (like "String" for text or "Int" for numbers,
if you are familiar with those).
Kotlin Variables

To create a variable in Kotlin that should store text and


another that should store a number, look at the following
example:
Kotlin Variables

It is possible to specify the type if you insist:


Kotlin Variables

When you create a variable with the val keyword, the value
cannot be changed/reassigned. The following example will
generate an error:
Kotlin Variables

When using var, you can change the value whenever you
want:
Kotlin Variables

The val keyword is useful when you want a variable to always


store the same value, like PI (3.14159...):
Kotlin Variables

To combine both text and a variable, use the + character:


Kotlin Variables

You can also use the + character to add a variable to another


variable:
Kotlin Variables

For numeric values, the + character works as a mathematical


operator:
Kotlin Data Types
• Data types are divided into different groups:
• Numbers
• Characters
• Booleans
• Strings
• Arrays
Kotlin Data Types
• Numbers:
• Integer types store whole numbers, positive or negative (such as
123 or -456), without decimals. Valid types are Byte, Short, Int and
Long.
• Floating point types represent numbers with a fractional part,
containing one or more decimals. There are two types: Float and
Double.
If you don't specify the type for a numeric variable, it is most often
returned as Int for whole numbers and Double for floating point
numbers.
Kotlin Data Types
• Numbers:
Kotlin Data Types
• Numbers: Scientific notation
Kotlin Data Types
• Booleans: can only take the values true or false:
Kotlin Data Types
• Characters: is used to store a single character. A char
value must be surrounded by single quotes, like 'A' or 'c':
Kotlin Data Types
• Type Conversion: convert the value of one data type to
another type.
• To convert a numeric data type to another type, you must
use one of the following functions: toByte(), toShort(),
toInt(), toLong(), toFloat(), toDouble() or toChar().
Kotlin Data Types
• Type Conversion:
Kotlin Operators
• Operators are used to perform operations on variables
and values.
• Kotlin divides the operators into the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
Kotlin Operators
• Arithmetic operators:
Kotlin Operators
• Assignment Operators:
Kotlin Operators
• Comparison Operators:
Kotlin Operators
• Logical Operators:
Kotlin Strings
• Access a String:
Kotlin Strings
• String Length:
Kotlin Strings
• String Functions:
Kotlin Strings
• Comparing Strings:
Kotlin Strings
• Finding a String in a String:
Kotlin Strings
• Quotes Inside a String:
Kotlin Strings
• String Concatenation:
Kotlin Strings
• String Templates/Interpolation:
Kotlin Booleans
• Boolean values:
Kotlin Booleans
• Boolean expression:
Kotlin Booleans
• Boolean expression:
Kotlin If ... Else
• Kotlin if:
Kotlin If ... Else
• Kotlin else:
Kotlin If ... Else
• Kotlin else if:
Kotlin If ... Else
• Kotlin If..Else Expressions:
Kotlin If ... Else
• Kotlin If..Else Expressions:
Kotlin When
• Kotlin when:
Kotlin While Loop
• While loop:
Kotlin While Loop
• Do-While loop:
Kotlin Break and Continue
• Kotlin Break:
Kotlin Break and Continue
• Kotlin Continue:
Kotlin Arrays
• Kotlin Array:
Kotlin Arrays
• Access the Elements of an Array:
Kotlin Arrays
• Change an Array Element:
Kotlin Arrays
• Change an Array Element:
Kotlin Arrays
• Array Length / Size:
Kotlin Arrays
• Check if an Element Exists:
Kotlin Arrays
• Loop Through an Array:
Kotlin Ranges
• Kotlin Ranges:
Kotlin Ranges
• Check if a Value Exists:
Kotlin Ranges
• Break or Continue a Range:
Kotlin Functions
• Create Your Own Functions:
Kotlin Functions
• Function Parameters:
Kotlin Functions
• Return Values:
Kotlin Classes and Objects
• Create a Class:
Kotlin Classes and Objects
• Create an Object:
Kotlin Classes and Objects
• Multiple Objects:
Kotlin Constructors
• Kotlin Constructor:
Kotlin Class Functions
• Kotlin Function:
Kotlin Inheritance (Subclass and
Superclass)

You might also like