0% found this document useful (0 votes)
31 views12 pages

Kotlin

Uploaded by

basaf54408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views12 pages

Kotlin

Uploaded by

basaf54408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

KOTLIN:

DATA TYPES IN KOTLIN:


---- ----- --- ------

Numbers:

Byte: 8-bit signed integer (-128 to 127)


Short: 16-bit signed integer (-32,768 to 32,767)
Int: 32-bit signed integer (-2^31 to 2^31-1)
Long: 64-bit signed integer (-2^63 to 2^63-1)
Float: 32-bit floating point number
Double: 64-bit floating point number
Characters:

Char: 16-bit Unicode character


Booleans:

Boolean: Represents true or false values


Strings:

String: Represents a sequence of characters


Arrays:

Array: Represents a fixed-size collection of elements of the same type


Collections:

List: Represents an ordered collection of elements (may contain duplicates)


Set: Represents an unordered collection of unique elements
Map: Represents a collection of key-value pairs
Ranges:

Range: Represents a range of values


Nullability:

Nullable types: All types in Kotlin can be nullable by appending ? to the type name
(e.g., String?, Int?)

print() - Displays the value present inside it


println() - Displays the value present inside it with a newline character at the
end of the value

fun main() {
var name = "Elwin"
name = "Jason"
print(name)
print("Hello $name")
}
variable is a container that holds a certain value at certain point of time

var = Mutable

val = Immutable

---------------------------------------------------------------------------

EXPLICIT INITIALIZATION:
The below is an explicit initialization
fun main() {

val name : String


val age : Int
name = "Mike"
age = 40
print("My name is $name. I am $age years old.")
}

---------------------------------------------------------------------------

TYPES IN KOTLIN AND THEIR SIZES:

Bit: Smallest unit of data or information (0 or 1).

Basic Types in kotlin are:

Int :32 bits (Whole Number)


Byte : 8bits (Largest value is 127. 2^8 = 128)
Short : 16 bits
Long(X-large bit): 64 bits

Float : 32 bits (Decimal Numbers)


Double : 64 bits

Double has more precision so to build a application that requires more precise
values use double

Every decimal number that is not specified is initialized as double


Ex : val pi = 3.14
In the above example the compiler initailizes the number as a double
Ex : val pi = 3.14f
To represent the number as a floating number we are required to put 'f' at the end
of the number
Float rounds up the number to atleast 6 or 7 digits

Double rounds up the number to 16 digits as per my observations

---------------------------------------------------------------------------
LONG TYPE:

fun main() {

val onelong = 1L

'L' at suffix specifies it as long


}

KOTLIN OPERATIONS:

fun main() {

val a = 23
val b = 12
print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(a%b)
}

val a = 23
val b = 12
print(a/b)

Output : 1

val a = 23.0
val b = 12
print(a/b)

Output : 1.9166666666666667

---------------------------------------------------------------------------

LOOPS AND BRANCHING:

fun main() {
val amount = 900
if (amount >= 1000) {
print("You are wealthy")
} else {
print("You are getting by")
}
}

If we want to create a more branched tree we can use the when expression.

fun main() {
val amount = 999
when(amount) {
100 -> print("You have 100")
125 -> print("You are getting there....")
999 -> print("You are really close")
else -> {
print("$amount is not going to work")
}
}

With when we can check if the number is in or not in between a range

fun main() {

val amount = 99

when(amount) {
in 1..100 -> print("The amount is between 1 and 100")
!in 10..90 -> print("The amount is outside the range")
}
}

Input : amount = 99
Output: The amount is between 1 and 100

Input : amount = 0
Output : The amount is outside the range

---------------------------------------------------------------------------

FOR LOOPS:

fun main() {

for (i in 1..3) {
println(i)
}
}

Output :
1
2
3
fun main()

for (i in 1..10) {
if (i%3 == 0) println(i) else println("--")
}
}

Output:
--
--
3
--
--
6
--
--
9
--

---------------------------------------------------------------------------

FUNCTIONS:

Definition: A set of instructions bundled together to achieve a certain outcome.

Keyword - fun

fun main()
{
hello()
}

fun hello() {
print("This is a function")
}

Function with Int parameter

fun main() {
ran(1,100) - These are called arguments.
Arguments are values that are declared within a function when
a function is called.
}

fun ran(first : Int , second : Int) - These are called parameters


Parameters are variables that are defined
when the function is declared
{
for (i in first..second){
print(i)
}
}

fun main() {
calculate(1,100,"is the multiple of",3)
}

fun calculate(first:Int,second:Int,message:String,mof:Int){
for(i in first..second){
if(i%mof == 0){
println("$i $message $mof")
}
}
}

DEFAULT ARGUMENTS:
------- ---------

fun main() {
calculate(message = "is the multiple of",mof = 11)
}

fun calculate(first:Int = 11,


second:Int = 1000,
message:String,
mof:Int){
for(i in first..second){
if(i%mof == 0){
println("$i $message $mof")
}
}
}

fun main() {
calculate(first = 20,
second = 200,
message = "is the multiple of",
mof = 11)
}

fun calculate(first:Int = 11,


second:Int = 1000,
message:String,
mof:Int){
for(i in first..second){
if(i%mof == 0){
println("$i $message $mof")
}
}
}

RETURNING INT:
--------- ---
fun main() {
// val catage= calculateCatAge(age = 12)
// print("This cat is $catAge years old")
print("This cat is "+calculatecatAge(age = 11)+" years old")
}
// fun calculatecatAge(age:Int): Int{
// return age * 7
// }
fun calculatecatAge(age : Int): Int = age * 7

RETURNING BOOLEAN:
--------- -------
fun main() {
val age = calculatecatAge(age = 10)
if(checkage(age)){
print("This cat is old ($age years old)")
}
else{
print("This cat is young ($age years old)")
}
}
fun calculatecatAge(age : Int): Int = age * 7

fun checkage(age : Int): Boolean = age >= 70

LAMBDA FUNCTION:
------ --------

fun main() {
println(sum(1,2))
println(age(6))
}
val sum : (Int,Int)->Int = {a , b -> a + b}
// val lambdaname : Type = {paramaterlist -> codeBody}

val age : (Int) -> Int = {a -> a*7}


"IT" LAMBDA KEYWORD:
--- ------- --------

Unit = {} - In kotlin Unit means void or nothing.

fun main() {
name("Slim Shady")
lname("Eminem")
}

val lname: (String) -> Unit = {println("-"+it)}

val name: (String) -> Unit = {println("My name is, My name is, My name is Chica
Chica Chica Chica $it")}

fun showName(name : String){


println(name)
}

FUNCTION AS PARAMTER:
-------- -- --------

fun main() {
message("Hello there,"){
print(it)
add(12,12)
}
}

fun message(message : String , funAsParameter: (String) -> Int){


println("$message ${funAsParameter("Nigga")}")
}

val add : (Int, Int) -> Int = {a,b -> a+b}

COLLECTION:
-----------
Kotlin Collections Functions Cheat Sheet - Article To Refer for More

A collection usually contains a number of objects or items in a certain order of


the same type.

MUTABLE AND UNMUTABLE LIST:


------- --- --------- ----

fun main() {
val mutablelist = mutableListOf("Rocky" , "Blacky" , "Tinku" , "Jimmy")
mutablelist.add(1,"Sammy")
mutablelist.remove("Jimmy")
mutablelist.removeAt(1)
mutablelist.forEach{
println(it)
}

val unmutablelist = listOf("Rocky" , "Blacky" , "Tinku" , "Jimmy")


unmutablelist.forEach{
println(it)
}
for(i in unmutablelist){
println("Names: $i")
}
}

LIST METHODS IN KOTLIN:


---- ------- -- ------
fun main(){
val mutablelist = mutableListOf("Rocky" , "Blacky" , "Tinku" , "Jimmy")
val unmutablelist = listOf("Rocky" , "Blacky" , "Tinku" , "Jimmy")
println("Size of unmutable list: "+unmutablelist.size)
println("First element in unmutable list is : "+unmutablelist.get(0))
println("Size of mutable list: "+mutablelist.size)
println("Second element in mutable list is: "+mutablelist.get(1))
println("Last element in mutable list is: "+mutablelist[3])
println("Third element in unmutable list is: "+unmutablelist[2])
println("Index of Jimmy is: "+mutablelist.indexOf("Jimmy"))
println("Index of Blacky is: "+unmutablelist.indexOf("Blacky"))
}

SETS IN KOTLIN:
---- -- -------

fun main() {
val myset = setOf("A","AB","C","B","C")
val mutableset = mutableSetOf(1,2,3,4,5,6,7,8)
println(myset)
println(mutableset)
myset.forEach{
println(it)
}
mutableset.add(9)
mutableset.add(2)
mutableset.forEach{
println(it)
}
println(mutableset)

MAPS IN KOTLIN:
---- -- -------

fun main() {
val secmap = mapOf("Up" to 1,
"Down" to 2,
"Left" to 3,
"Right" to 4)
println(secmap)
println(secmap.keys)
println(secmap.values)
if("Up" in secmap) println("Yo nigga")
print("\n\n")

val musecmap = mutableMapOf("One" to 1,


"Two" to 2,
"Three" to 3,
"Four" to 4)
println(musecmap)
println(musecmap.keys)
println(musecmap.values)
musecmap.put("Five",5)
println(musecmap)
musecmap["Six"] = 6
println(musecmap)
}

INITIALIZING LISTS:
------------ ------

Code:
-----

fun main() {
val list = mutableListOf<String>()
list.add("hola")
println(list)
list.add("Mi Amigos")
println(list)
for(i in 1..10) list.add(i,"Hey $i")
println(list)
}

<String> - This specifies that only string items can be added into the list

Output:
-------
[hola]
[hola, Mi Amigos]
[hola, Hey 1, Hey 2, Hey 3, Hey 4, Hey 5, Hey 6, Hey 7, Hey 8, Hey 9, Hey 10, Mi
Amigos]

EMPTY COLLECTIONS:
----- ------------

Code:
-----

fun main() {
val emptylist = emptyList<String>()
val emptyset = emptySet<String>()
val emptymap = emptyMap<String,Int>()
println(emptylist)
println(emptyset)
println(emptymap)
}

Output:
-------

[]
[]
{}

FILTER METHOD:
------ -------

Code:
-----

fun main() {

val list = listOf("James", "Jose" , "Roberto" , "Mourino")


val found1 = list.filter{
it == "Roberto"
}
val found2 = list.filter{
it == "Giovanni"
}
val found3 = list.filter{
it.length > 4
}
val found4 = list.filter{
it.endsWith("o")
}
val found5 = list.filter{
it.startsWith("J")
}
val found6 = list.filter{
it.startsWith("j")
}
val found7 = list.filter{
it.startsWith("j", ignoreCase = true)
}
val found8 = list.filter{
it.startsWith("j", ignoreCase = true) && it.endsWith("S", ignoreCase =
true)
}
println(found1)
println(found2)
println(found3)
println(found4)
println(found5)
println(found6)
println(found7)
println(found8)
}

Output:
-------

[Roberto]
[]
[James, Roberto, Mourino]
[Roberto, Mourino]
[James, Jose]
[]
[James, Jose]
[James]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy