Skip to content

Commit a2cc64c

Browse files
committed
tweeked extensions to call from java, cleaned up ranges a bit, and cleaned up some of the null stuff
1 parent 232eb65 commit a2cc64c

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

src/main/java/me/kotlinintegration/UseAKotlinClass.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package me.kotlinintegration;
22

33
import me.kotlindemo.classes.Person;
4+
import me.kotlindemo._03_extensionsKt;
45

56
public class UseAKotlinClass {
67
public static void main(String[] args) {
78
UseAKotlinClass useAKotlinClass = new UseAKotlinClass();
89
useAKotlinClass.printADataClass();
10+
useAKotlinClass.useAnExtensionFunction();
911
}
1012

1113
private void printADataClass() {
@@ -18,7 +20,8 @@ private void printADataClass() {
1820
}
1921

2022
private void useAnExtensionFunction() {
23+
System.out.println("\n Use a kotlin extension function from java");
2124
String blah = "blah";
22-
// blah.doo
25+
System.out.println("Using a kotlin extension function: " +_03_extensionsKt.doodify(blah));
2326
}
2427
}

src/main/kotlin/me/kotlindemo/01_IntroToKotlin.kt

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,6 @@ fun simpleForLoopOverRange() {
174174
for (counter in 1..5) println(" Counter $counter")
175175
}
176176

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-
182177
/**
183178
* >>>>> Ranges
184179
*/
@@ -196,6 +191,12 @@ fun doingStuffWithRanges() {
196191
print("\n\n Counting downward\n ")
197192
for (i in 10 downTo 1) { print("$i,") }
198193

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+
199200
print("\n\n using range in a if statement\n ")
200201
val somenum = 8
201202
if (somenum in 1..10) { println("$somenum is in the range") }
@@ -204,23 +205,45 @@ fun doingStuffWithRanges() {
204205
fun loops() {
205206
println("\n>>>> Loops")
206207
simpleForLoopOverRange()
207-
progression()
208208
doingStuffWithRanges()
209209
}
210210

211211
/**
212212
* >>>> 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
213217
*/
214218

215219
fun nullableParams(one: String? = "blah", two: String? = null) { //? means this param is nullable,
216220
println(" nullableParamsToFun: fun with nulllable props one: $one two: $two")
217221
}
218222

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+
219233
fun safeCallOperator(one: String?) {
220234
// println("one: ${one.toUpperCase()}") //can throw an NPE since one is nullable, compile time warning
221235
println(" safeCallOperator: one: ${one?.toUpperCase()}") //? is safecall, ie if not null then toUpperCase(), can be chained ie x?.y?.z?
222236
}
223237

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+
224247
fun nullableStuff() {
225248
println("\n>>>>Nullable stuff")
226249
nullableParams(one = null)
@@ -232,10 +255,6 @@ fun nullableStuff() {
232255
}
233256
}
234257

235-
fun allowNPE(nulledString: String? = null) {
236-
nulledString!!.length //throw the npe
237-
}
238-
239258
/**
240259
* EXCEPTIONS
241260
* kotlin does not support Checked Exceptions

0 commit comments

Comments
 (0)