@@ -174,11 +174,6 @@ fun simpleForLoopOverRange() {
174
174
for (counter in 1 .. 5 ) println (" Counter $counter " )
175
175
}
176
176
177
- fun progression () {
178
- println (" \n loop with progression of 2 and a range" )
179
- for (counter in 1 .. 10 step 2 ) println (" progression of 2: $counter " )
180
- }
181
-
182
177
/* *
183
178
* >>>>> Ranges
184
179
*/
@@ -196,6 +191,12 @@ fun doingStuffWithRanges() {
196
191
print (" \n\n Counting downward\n " )
197
192
for (i in 10 downTo 1 ) { print (" $i ," ) }
198
193
194
+ print (" \n\n Counting downward with a step\n " )
195
+ for (i in 10 downTo 1 step 2 ) { print (" $i ," ) }
196
+
197
+ print (" \n\n Counting downward differently doesn't work\n " )
198
+ for (i in 10 .. 1 ) { print (" $i ," ) } // note the intellij highlighting asking to use a downTo
199
+
199
200
print (" \n\n using range in a if statement\n " )
200
201
val somenum = 8
201
202
if (somenum in 1 .. 10 ) { println (" $somenum is in the range" ) }
@@ -204,23 +205,45 @@ fun doingStuffWithRanges() {
204
205
fun loops () {
205
206
println (" \n >>>> Loops" )
206
207
simpleForLoopOverRange()
207
- progression()
208
208
doingStuffWithRanges()
209
209
}
210
210
211
211
/* *
212
212
* >>>> NULLABLE STUFF
213
+ * by default kotlin expects everything to be not null. it won't let you assign stuff to be null
214
+ *
215
+ * if you want something nullable you have to expliclity indicate a property is nullable. Kotlin will then
216
+ * require any code using it to declare nullability
213
217
*/
214
218
215
219
fun nullableParams (one : String? = "blah", two : String? = null) { // ? means this param is nullable,
216
220
println (" nullableParamsToFun: fun with nulllable props one: $one two: $two " )
217
221
}
218
222
223
+ // val blah: String = null //compile error since blah is defined by default as non-null
224
+ fun nullableAssignments () {
225
+ var nullableString: String? = " blah"
226
+ // nullableString.length //compile error about nullableString can be null and you need to do stuff
227
+ var lengthOfString = nullableString?.length // use safecall to access length
228
+
229
+ // Elvis operator ?: used for null checks
230
+ val stringAllCaps = nullableString?.capitalize() ? : " not today"
231
+ }
232
+
219
233
fun safeCallOperator (one : String? ) {
220
234
// println("one: ${one.toUpperCase()}") //can throw an NPE since one is nullable, compile time warning
221
235
println (" safeCallOperator: one: ${one?.toUpperCase()} " ) // ? is safecall, ie if not null then toUpperCase(), can be chained ie x?.y?.z?
222
236
}
223
237
238
+ /* *
239
+ * Kotlin requires you to explicitly allow an NPE to be thrown, by default it forces you to deal with it
240
+ * using the safecall/elvis (or let/apply blocks) which do not throw an NPE
241
+ */
242
+ fun allowNPE (nulledString : String? = null) {
243
+ // nulledString?.length ?: throw java.lang.NullPointerException() //same as following line
244
+ nulledString!! .length // throw the npe same as previous line
245
+ }
246
+
224
247
fun nullableStuff () {
225
248
println (" \n >>>>Nullable stuff" )
226
249
nullableParams(one = null )
@@ -232,10 +255,6 @@ fun nullableStuff() {
232
255
}
233
256
}
234
257
235
- fun allowNPE (nulledString : String? = null) {
236
- nulledString!! .length // throw the npe
237
- }
238
-
239
258
/* *
240
259
* EXCEPTIONS
241
260
* kotlin does not support Checked Exceptions
0 commit comments