Skip to content

Commit 9a979a9

Browse files
authored
Initial Commit
Learning basics of kotlin, these include functions and their uses varaible manipulation array, list, map uses and manipulation basic loops basic if/when(switch) statements
0 parents commit 9a979a9

File tree

9 files changed

+160
-0
lines changed

9 files changed

+160
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
11+
</component>
12+
</module>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
fun sayHello(greeting:String, itemToGreet:List<String>){
2+
//this works just like the other for loops
3+
itemToGreet.forEach { itemToGreet ->
4+
//itemToGreet is defined in the main function
5+
println("$greeting $itemToGreet")
6+
}
7+
}
8+
//start going down here before top function
9+
fun main(){
10+
//as soon as string is added via "" array can defer that the values are strings
11+
val interestingThings = arrayOf("kotlin", "programming", "chicken nuggets")
12+
//this prints size of array
13+
println(interestingThings.size)
14+
//calls num 2 in array
15+
println(interestingThings[2])
16+
//works same as above 1
17+
println(interestingThings.get(0))
18+
//for loop to print our array
19+
for (interestingThings in interestingThings){
20+
println(interestingThings)
21+
}
22+
//This works much like a for loop, however it is a pre made function in the base library
23+
//interestingThings.forEach {it: String
24+
//println(it)
25+
//}
26+
27+
//you can also rename the it value to make it less confusing e.g.
28+
interestingThings.forEach {interestingThing -> //this will rename the 'it' to a string of your choice
29+
println(interestingThing)
30+
}
31+
//However what if we want to find out what index our strings are in? well we use the for each index to do this e.g. below:
32+
interestingThings.forEachIndexed { index, interestingThingIndexed ->
33+
println("$interestingThingIndexed is at index $index")
34+
}
35+
//all that can be used in arrays can be used in lists as well
36+
val interestingThingsList = mutableListOf("example 1", "example 2", "example 3") //was originally listOf changed to mutableListOf
37+
interestingThingsList.get(2)
38+
interestingThingsList[0]
39+
interestingThingsList.forEach { interestingList ->
40+
println(interestingList)
41+
}
42+
43+
//We can also use a type called a map. a map works by taking pair values e.g. below:
44+
//This means that you can pair two varaibels together e.g. 1 can = a meaning that the value 1 has a string value of a
45+
val map = mapOf(1 to "a", 2 to "b", 3 to "c")
46+
//when it comes to finding in the index the number is the space in the index and the string is what will be shown
47+
//e.g. if i called index 0, it would be null because i havent assigned a pair value to it
48+
//but if i call index 1 it will display an 'a'
49+
println(map[1])
50+
51+
map.forEach { key, value -> println("$key -> $value") }
52+
53+
//Now lets look at how to add to the index
54+
//first to do this it has to be a mutable of e.g.lists: mutableListOf this shows that it can mutate, or change
55+
interestingThingsList.add("example 4")
56+
//same works for map however instead of add its put
57+
//this is a list i made to use this
58+
val otherFunctionList = listOf("Chicken", "Dog", "Cat")
59+
//Now lets use it with a function the function that this will work with has already been pre called above
60+
sayHello("Hello", otherFunctionList)
61+
//This can be used with an array but will have to be defined in function parameters
62+
63+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//This is an example of a function, you can assign the return type just like you assign a var
2+
//You can also set parameters inside ()
3+
fun getGreeting(): String {
4+
return "Hello Kotlin!"
5+
}
6+
//How do you change from a function? you use function parameters e.g. below
7+
fun sayHello(itemToGreet:String) {
8+
//as you can see itemToGreet was set as a string above, this means that msg when printed will print the value plus pre defined text. This can be altered n this function or
9+
//the main funciton
10+
//val msg = "Hello" + itemToGreet This is one way to do it but kotlin allows you to input parameter values into a string e.g.
11+
val msg = "Hello $itemToGreet"//this is for convenience and a way of shortening code
12+
println(msg)
13+
14+
}
15+
16+
//Multiple parameters in function Below:
17+
fun multiHello(first:String, second:String) = println("$first $second")
18+
19+
fun main(){
20+
println("Hello World!")
21+
//You can call a function just like you would in java, here we are printing what it returns
22+
println(getGreeting())
23+
//Here it has been edited
24+
sayHello(itemToGreet = "Myself")
25+
26+
//setting multi param here. You will have to set both if they are not already set
27+
multiHello(first = "Hello", second = "Universe")
28+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
val toplvl: String = "Example of top level"
2+
//You could also place variables outside of the main function. these are called top level. Top lvl var's can also be altered inside of the main. they work just like they do
3+
//inside the main
4+
5+
var nullstring: String? = "mike"
6+
//above is an example of a null string. in kotlin variables are all set to non null unless you make it so YOU CAN STILL ASIGN VALUES TO NULL STRINGS!
7+
8+
//kotlin can figure out what type of varaible your using so technically you dont have to use it e.g. is below
9+
val num1 = 2
10+
//as you can see it doesnt have the type of var but it still works if run
11+
fun main(){
12+
//Var works to define variables that can be change
13+
14+
//val variables cannot be changed
15+
val name: String = "Daniel"
16+
17+
//now the name variable cannot be re defined
18+
var varname: String = "Mike"
19+
varname = "Shaun"
20+
println(name)
21+
22+
println(varname)
23+
//Once Run you can see that the Mike value has changed to shaun. InteliJ helps you see this by greying out the Mike string
24+
println(toplvl)
25+
println(nullstring)
26+
nullstring = null
27+
println(nullstring)
28+
println(num1)
29+
//now lets look at control flow
30+
var controltest: String? = null
31+
if(controltest != null){
32+
println(controltest)
33+
} else{
34+
println("Test for control")
35+
}
36+
//this is an example of if/else statements to control when variables are called.
37+
//When statements
38+
//When statements work just like switch statements from java example below
39+
var whentest: String? = null
40+
when(whentest){
41+
null -> println("Hi")
42+
else -> println(whentest)
43+
}
44+
//when whentest is null it will print hi. otherwise it will print whatever string is assigned
45+
46+
//If and when can be used as expression to assign a value depending on conditions e.g. below
47+
var assignTest: String? = null
48+
// assignTest = "It Works" Remove this test and '//' to prove it works
49+
val assignTestToPrint = if (assignTest != null) assignTest else "Value Is NULL"
50+
println(assignTestToPrint)
51+
//this is another way we can assign different values depending on parameters the same works for when and is layed out the same
52+
53+
54+
55+
56+
57+
}

0 commit comments

Comments
 (0)