Want to make creations as awesome as this one?

Transcript

Kotlin : BASIC TYPES AND VARIABLES

SANTIAGO Kevin
WLODARCZAK Arthur

1

Chapter 2

1. Introduction

3. Things to know about variable's value

2. How to declare variable and type

4. Store multiple values in an array

5. Things to know about Strings

INDEX

6. Conclusion

2

1. iNTRODUCTION

3

YOUR CODE NEEDs VARIABLEs !!

4

Think of a cup.

What is a variable

Big cups

Small cups

Giant cups

5

Give it a name

Tell if the variable can be reused or not

Choose a type

Assign a value

hOW TO CREATE A VARIABLE

6

2. How to declare VARIABLE AND type

7

What happends when you declare a variable

Be careful about the type.

Example :
var str : string = 4 // Won't work

Let the compiler choose

Example :
var x = 5 //this var is an int

8

What happens when you declare a variable

val --> can't be changed

var --> can assign a new value to the variable

9

Hexadecimal and binary

Kotlin's basic types

Integers

Floating points

Boolean

Characters and String

10

Binary : prefix 0b

Hexadecimal : prefix 0x



Float : 32 bits

exemple : var x = 123.5

Double : 64 bits


char : only one character, with single quotes

string : multiples characters, with doubles quotes


boolean : true or false


What happens when you declare a variable

Example :
var variable1 : Int = 12 // great job !


var variable2 : Int = 3.5 // won't compile

Declare a variable explicitly

11

3. THINGS TO KNOW ABOUT VARIABLE'S VALUE

12

Make sure that their types are compatible

Assigning a value to an other variable

Example :




13

Can't create a variable from a type to another

Assigning a value to an other variable

Example :

var x = 5
var z : Long = x // Won't compile

Use functions

Example :

var z : Long = x.toLong()

14

What happens when you convert a value ?

Functions : useful when new object is larger

Be careful when new object is smaller

Example :

var var1 : Long = 1234567890123
var var2 = var1.toInt() // Value is 1912276171

var x = 123.456
var y : Int = x.toInt() // Value is 123

15

4. Store multiple values in array

16

How to create an array ?

arrayOf()

Example :
var myArray = arrayOf(1,2,3)
print(myArray[0])


size

Example :
print(myArray.size)


17

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.


How can we update an array ?

Example :

var myArray = arrayOf(1,2,3)
myArray[1] = 15 // The value of the 2nd item is now 15

Be careful with the type of the array

18

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.


How can we explicitly define the array's type ?

Example :

var myArray : Array<Byte> = arrayOf(1,2,3)

Val and Var ?

var myArray = arrayOf(1,2,3)
myArray = arrayOf(4,5) // will compile

val myArray = arrayOf(1,2,3)
myArray[1] = 6 // new value of the item


19

4. Things to know about Strings

20

String templates

Allows you to construct complex Strings with very little code !


Example : //Include value of a variable
var x = 42
var value = “Value of x is $x”

// Include functions
var myArray = arrayOf(1,2,3)
var arraySize = “The size is ${myArray.size}”
var firstItem = “The first item is ${myArray[0]}”

//Include complexe expressions
var result = “myArray is ${if (myArray.size > 10) “large” else “small” }”

21

Conclusion

22