Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
107 views

Lesson 1 - Kotlin Basics

This document provides a summary of Kotlin basics including: getting started with Kotlin in IntelliJ IDEA, operators, data types, variables, conditionals, lists and arrays, and null safety. It covers topics such as mathematical, comparison, and assignment operators, integer, floating point, string, and boolean types, mutable and immutable variables, if/else and when statements, for, while, repeat loops, and lists versus arrays.

Uploaded by

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

Lesson 1 - Kotlin Basics

This document provides a summary of Kotlin basics including: getting started with Kotlin in IntelliJ IDEA, operators, data types, variables, conditionals, lists and arrays, and null safety. It covers topics such as mathematical, comparison, and assignment operators, integer, floating point, string, and boolean types, mutable and immutable variables, if/else and when statements, for, while, repeat loops, and lists versus arrays.

Uploaded by

Ingeniero Aarm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Lesson 1:

Kotlin basics

Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 1: Kotlin basics
○ Get started
○ Operators
○ Data types
○ Variables
○ Conditionals
○ Lists and arrays
○ Null safety
○ Summary

Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Get started

Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Open IntelliJ IDEA

Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Create a new project

Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Name the project

Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Open REPL (Read-Eval-Print-Loop)

It may take a few


moments before the
Kotlin menu appears
under Tools.

Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Create a printHello() function

Press Control+Enter
(Command+Enter on
a Mac) to execute.

Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Operators

Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Operators
● Mathematical operators
● Increment and decrement operators
● Comparison operators

● Assignment operator

● Equality operators

Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Math operators with integers

Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Math operators with doubles

Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Math operators

⇒ ⇒

⇒ indicates output
⇒ ⇒ from your code.

Result includes the


type (kotlin.Int).

Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Numeric operator methods
Kotlin keeps numbers as primitives, but lets you call methods on numbers
as if they were objects.

Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Data types

Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Integer types
Type Bits Notes

Long 64 From -263 to 263-1

Int 32 From -231 to 231-1

Short 16 From -32768 to 32767

Byte 8 From -128 to 127

Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Floating-point and other numeric types
Type Bits Notes

Double 64 16 - 17 significant digits


Float 32 6 - 7 significant digits
Char 16 16-bit Unicode character

Boolean 8 True or false. Operations include:


- lazy disjunction, - lazy conjunction,
- negation

Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Operand types
Results of operations keep the types of the operands

⇒ ⇒

⇒ ⇒

Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Type casting
Assign an Int to a Byte


Convert Int to Byte with casting

Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Underscores for long numbers

Use underscores to make long numeric constants more readable.

Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Strings

Strings are any sequence of characters enclosed by double quotes.

String literals can contain escape characters

Or any arbitrary text delimited by a triple quote (""")

Android Development with Kotlin This work is licensed under the Apache 2 license. 21
String concatenation

Android Development with Kotlin This work is licensed under the Apache 2 license. 22
String templates
A template expression starts with a dollar sign ($) and can be a simple value:

Or an expression inside curly braces:

Android Development with Kotlin This work is licensed under the Apache 2 license. 23
String template expressions

Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Variables

Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Variables

● Powerful type inference


● Let the compiler infer the type
● You can explicitly declare the type if needed
● Mutable and immutable variables
● Immutability not enforced, but recommended

Kotlin is a statically-typed language. The type is resolved at compile time and


never changes.

Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Specifying the variable type

Colon Notation

Important: Once a type has been assigned by you or the compiler, you can't
change the type or you get an error.

Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Mutable and immutable variables
● Mutable (Changeable)

● Immutable (Unchangeable)

Although not strictly enforced, using immutable variables is recommended in


most cases.

Android Development with Kotlin This work is licensed under the Apache 2 license. 28
var and val

Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Conditionals

Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Control flow

Kotlin features several ways to implement conditional logic:


● If/Else statements
● When statements
● For loops
● While loops

Android Development with Kotlin This work is licensed under the Apache 2 license. 31
if/else statements

Android Development with Kotlin This work is licensed under the Apache 2 license. 32
if statement with multiple cases

Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Ranges

● Data type containing a span of comparable values (e.g.,


integers from 1 to 100 inclusive)
● Ranges are bounded
● Objects within a range can be mutable or immutable

Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Ranges in if/else statements

Note: There are no spaces around the "range to" operator (1..100)

Android Development with Kotlin This work is licensed under the Apache 2 license. 35
when statement

As well as a when statement, you can also define a when expression that
provides a return value.

Android Development with Kotlin This work is licensed under the Apache 2 license. 36
for loops

You don’t need to define an iterator variable and increment it for each pass.

Android Development with Kotlin This work is licensed under the Apache 2 license. 37
for loops: elements and indexes

Android Development with Kotlin This work is licensed under the Apache 2 license. 38
for loops: step sizes and ranges


for (i in 3..6 step 2) print(i)

for (i in 'd'..'g') print (i)


Android Development with Kotlin This work is licensed under the Apache 2 license. 39
while loops
var bicycles = 0
while (bicycles < 50) {
bicycles++
}
println("$bicycles bicycles in the bicycle rack\n")

do {
bicycles--
} while (bicycles > 50)
println("$bicycles bicycles in the bicycle rack\n")

Android Development with Kotlin This work is licensed under the Apache 2 license. 40
repeat loops

Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Lists and arrays

Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Lists

● Lists are ordered collections of elements


● List elements can be accessed programmatically through their
indices
● Elements can occur more than once in a list

An example of a list is a sentence: it's a group of words, their order is important,


and they can repeat.

Android Development with Kotlin This work is licensed under the Apache 2 license. 43
Immutable list using listOf()

Declare a list using and print it out.

⇒ [trumpet, piano, violin]

Android Development with Kotlin This work is licensed under the Apache 2 license. 44
Mutable list using mutableListOf()

Lists can be changed using mutableListOf()

With a list defined with val, you can't change which list the variable refers to, but
you can still change the contents of the list.

Android Development with Kotlin This work is licensed under the Apache 2 license. 45
Arrays

● Arrays store multiple items

● Array elements can be accessed programmatically through


their indices

● Array elements are mutable


● Array size is fixed

Android Development with Kotlin This work is licensed under the Apache 2 license. 46
Array using arrayOf()

An array of strings can be created using arrayOf()

With an array defined with val, you can't change which array the variable refers
to, but you can still change the contents of the array.

Android Development with Kotlin This work is licensed under the Apache 2 license. 47
Arrays with mixed or single types

An array can contain different types.

An array can also contain just one type (integers in this case).

Android Development with Kotlin This work is licensed under the Apache 2 license. 48
Combining arrays

Use the + operator.

Android Development with Kotlin This work is licensed under the Apache 2 license. 49
Null safety

Android Development with Kotlin This work is licensed under the Apache 2 license. 50
Null safety

● In Kotlin, variables cannot be null by default


● You can explicitly assign a variable to null using the safe call
operator
● Allow null-pointer exceptions using the !! operator
● You can test for null using the elvis (?:) operator

Android Development with Kotlin This work is licensed under the Apache 2 license. 51
Variables cannot be null

In Kotlin, null variables are not allowed by default.

Declare an Int and assign null to it.

Android Development with Kotlin This work is licensed under the Apache 2 license. 52
Safe call operator

The safe call operator (?), after the type indicates that a variable can be null.

Declare an Int? as nullable

In general, do not set a variable to null as it may have unwanted consequences.

Android Development with Kotlin This work is licensed under the Apache 2 license. 53
Testing for null
Check whether the numberOfBooks variable is not null. Then decrement that
variable.

Now look at the Kotlin way of writing it, using the safe call operator.

Android Development with Kotlin This work is licensed under the Apache 2 license. 54
The !! operator
If you’re certain a variable won’t be null, use !! to force the variable into a
non-null type. Then you can call methods/properties on it.

throws NullPointerException if s is null

Warning: Because !! will throw an exception, it should only be used when it


would be exceptional to hold a null value.

Android Development with Kotlin This work is licensed under the Apache 2 license. 55
Elvis operator

Chain null tests with the ?: operator.

The ?: operator is sometimes called the "Elvis operator," because it's like a smiley
on its side with a pompadour hairstyle, like Elvis Presley styled his hair.

Android Development with Kotlin This work is licensed under the Apache 2 license. 56
Summary

Android Development with Kotlin This work is licensed under the Apache 2 license. 57
Summary
In Lesson 1, you learned how to:
● Create an IntelliJ IDEA project, opening REPL, and execute a function
● Use operators and numeric operator methods
● Use data types, type casting, strings, and string templates
● Use variables and type inference, and mutable and immutable
variables
● Use conditionals, control flow, and looping structures
● Use lists and arrays
● Use Kotlin's null safety features
Android Development with Kotlin This work is licensed under the Apache 2 license. 58
Pathway

Practice what you’ve learned by


completing the pathway:
Lesson 1: Kotlin basics

Android Development with Kotlin This work is licensed under the Apache 2 license. 59

You might also like