1
1
package me.kotlindemo
2
2
3
+ import me.kotlindemo.classes.Person
3
4
import java.lang.NumberFormatException
4
5
import kotlin.IllegalArgumentException
5
6
@@ -21,7 +22,10 @@ fun main() {
21
22
/* *
22
23
* FUNCTIONS
23
24
* Functions in kotlin are 'first class objects', ie the can be assigned to variables, passed as params
24
- * to other functions, and returned as a result from higher order functions.
25
+ * to other functions, and returned as a result from higher order functions. They do not need to be defined
26
+ * in a class scope.
27
+ *
28
+ * expression body vs
25
29
*/
26
30
fun functions () {
27
31
println (" \n >>>>> Some functions" )
@@ -40,7 +44,7 @@ fun functions() {
40
44
}
41
45
42
46
// functions are first class and do not need to be part of a class
43
- // expression body syntax, return type is inferred. single line only (no {})
47
+ // this is expression body syntax, return type is inferred. single line only (no {})
44
48
fun add (a : Int , b : Int ) = a + b
45
49
46
50
// return type is required when using {}, multiline
@@ -51,28 +55,27 @@ fun addWithBody(a: Int, b: Int): Int {
51
55
// no return type, //implicitly returns Unit
52
56
fun addAndPrint (a : Int , b : Int ) = println (" fun with expression body: printing sum: ${a + b} " )
53
57
54
-
58
+ // return type not needing to be specified, so it's implicit Unit
55
59
fun addAndPrintBody (a : Int , b : Int ) {
56
60
println (" \n fun with block body: printing from a body with no return: ${a + b} " )
57
61
return Unit // not required, can be ignored
58
62
}
59
63
60
64
fun paramsWithDefaultValues (one : String = "blah", two : Boolean = false, three : Int = 0,
61
- fourNullable : String? = null){ // one, two, three are not nullable. four is
65
+ fourNullable : String? = null) { // one, two, three are not nullable. four is
62
66
println (" \n fun params with default values: one: $one two:$two three: $three , fourNullable: $fourNullable " )
63
67
}
64
68
65
69
/* *
66
70
* Lambdas
67
- * Similar to the way java does them for the most part
71
+ * Similar to the way java does them for the most part, with a bit less verbage
68
72
*/
69
- // Lambda Expressions
70
73
// return type is inferred, ie we don't explicitly say 'Int'
71
74
val sumTypeInferred = {x: Int , y: Int -> x + y}
72
75
val aResult = sumTypeInferred(3 , 4 )
73
76
74
77
// Explicit type declaration
75
- // Format: (input params) -> return type = implementation
78
+ // Format: (input params) -> return type = { implementation}
76
79
val printNumber : (Int ) -> Unit = {num -> println (" explioct type declared for lambda num: $num " )}
77
80
// the following doesn't work even tho I found lots of examples with it
78
81
// val printNumberErrors : Int -> Unit = {num -> println(" explioct type declared for lambda num: $num")}
@@ -82,16 +85,27 @@ fun doSomethngWithLambdas() {
82
85
println (" invoke a lambdaExpression 3 + 4 = ${sumTypeInferred(3 , 4 )} " )// need ${} since params, even tho it is a 'property'
83
86
printNumber(14 )
84
87
}
88
+
85
89
/* *
86
90
* VARIABLES
91
+ * types do not need to be specified, they can be determined at time of assignment. You can explicitly define types tho
92
+ * once a type has been set, it can't be chanved, ie a val/var set to a string cannot be assigned to an int
93
+ * everything is an object, and properties are references
94
+ *
95
+ * using 'val' means once assigned a value reference, it cannot change (immutable)
96
+ * using 'var' means it is a mutable reference
87
97
*/
88
98
fun someVariableStuff () {
89
99
println (" \n >>>>> Variables" )
100
+
90
101
val implicitInt = 32 // type is implicit Int, val means the reference can only be set once
91
102
// implicitInt = 33 //compile time fail uncomment to show
92
103
// implicitInt = "string" //compile time reassign val error AND type mismatch error
93
- var varInt = implicitInt // valid, var can have it's reference changed
104
+ var varInt = implicitInt // var has mutable reference, but once assigned a type it can't change
94
105
println (" varInt = $varInt " )
106
+ // varInt = "blah" //type mismatch, once a var has a type assigned it can't change
107
+ varInt = implicitInt * 2 // var has mutable reference
108
+ println (" after changing reference varInt = $varInt " )
95
109
println (" varInt with ++ = ${++ varInt} " ) // need {} to get ++ to work, ie an expression
96
110
println (" varInt after increment = $varInt " )
97
111
}
@@ -118,13 +132,24 @@ fun simpleWhenAsExpression(a: Int) =
118
132
else -> " range: a is out of range $a "
119
133
}
120
134
121
- // implicitly returns Unit
135
+ /* *
136
+ * implicitly returns Unit
137
+ * no need for casting after figuring type, ie no if(instanceof String) {String x - (String)obj
138
+ */
122
139
fun whenWithSmartCast (a : Any ) =
123
140
when (a) {
124
141
is String -> println (" smartcast: got a string: $a " ) // in block we don't have to do typcast, its automagic
125
142
else -> println (" smartcast: not a string $a " )
126
143
}
127
144
145
+ fun someFunWithEquals () {
146
+ println (" Showing equals" )
147
+ val personOne = Person (" blah" , " Johnny" , " Walker" )
148
+ val personTwo = Person (" blah" , " Johnny" , " Walker" )
149
+ println (" using == in place of .equals structural equality: ${personOne == personTwo} " )
150
+ println (" using === in place of .equals refernentioal equality: ${personOne == = personTwo} " )
151
+ }
152
+
128
153
// implicitly returns Unit
129
154
fun conditionals () {
130
155
println (" \n >>>> Conditionals" )
@@ -137,6 +162,7 @@ fun conditionals() {
137
162
println (" simple when as an expression out of range ${simpleWhenAsExpression(44 )} " )
138
163
whenWithSmartCast(" smartCast with string 1" )
139
164
whenWithSmartCast(1 )
165
+ someFunWithEquals()
140
166
}
141
167
142
168
/* *
@@ -153,10 +179,33 @@ fun progression() {
153
179
for (counter in 1 .. 10 step 2 ) println (" progression of 2: $counter " )
154
180
}
155
181
182
+ /* *
183
+ * >>>>> Ranges
184
+ */
185
+ fun doingStuffWithRanges () {
186
+ println (" \n >>>> stuff with ranges" )
187
+ print (" typical for loop\n " )
188
+ for (i in 1 .. 20 ) { print (" $i ," ) }
189
+
190
+ print (" \n using until for loop\n " )
191
+ for (i in 0 until 20 ) { print (" $i ," ) }
192
+
193
+ print (" \n\n using a progression of 2 for loop\n " )
194
+ for (i in 2 .. 10 step 2 ) { print (" $i ," ) }// progression of 2
195
+
196
+ print (" \n\n Counting downward\n " )
197
+ for (i in 10 downTo 1 ) { print (" $i ," ) }
198
+
199
+ print (" \n\n using range in a if statement\n " )
200
+ val somenum = 8
201
+ if (somenum in 1 .. 10 ) { println (" $somenum is in the range" ) }
202
+ }
203
+
156
204
fun loops () {
157
205
println (" \n >>>> Loops" )
158
206
simpleForLoopOverRange()
159
207
progression()
208
+ doingStuffWithRanges()
160
209
}
161
210
162
211
/* *
0 commit comments