Javaͷίʔυ
public class JInts {
public static Integer getWrapperInteger() {
return /*ུ*/;
}
}
Slide 36
Slide 36 text
Kotlinͷίʔυ
// numPlatform TypeͷInt!ܕ
val num = JInts.getWrapperInteger()
ѻ͍ํΛؒҧ͑ͨΒIllegalStateExcep,onʹͳͬͨΓɺNullPointerExcep,onʹͳͬͨΓ
Slide 37
Slide 37 text
ଞͷPla$orm(Typeͱಉ༷ʹ
@Nullable@NotNullΛ͓͏
Slide 38
Slide 38 text
Javaͷίʔυ
public class JInts {
public static Integer getWrapperInteger() { return /*ུ*/; }
@Nullable
public static Integer getWrapperIntegerNullable() { return /*ུ*/; }
@NotNull
public static Integer getWrapperIntegerNotNull() { return /*ུ*/; }
}
Slide 39
Slide 39 text
Kotlinͷίʔυ
// numPlatform TypeͷInt!ܕ
val num = JInts.getWrapperInteger()
// numInt?ܕ
val num = JInts.getWrapperIntegerNullable()
// numIntܕ
val num = JInts.getWrapperIntegerNotNull()
Slide 40
Slide 40 text
ͳΔ΄Ͳ
Slide 41
Slide 41 text
ͰٯʹɺKotlinͷInt?Double?Java͔ΒͲ͏ݟ͑Δʁ
Slide 42
Slide 42 text
KotlinͷInt?
JavaͰInterger
Slide 43
Slide 43 text
Kotlinͷίʔυ
class KInts {
companion object {
@JvmStatic
fun getInt(): Int = 0
@JvmStatic
fun getNullableInt(): Int? = 0
}
}
Slide 44
Slide 44 text
Javaͷίʔυ
public class KIntsUsage {
public static void main(String[] args) {
int num0 = KInts.getInt();
Integer num1 = KInts.getNullableInt();
// ΞϯϘΫγϯάͰ͜͏͔͚Δ
int num2 = KInts.getNullableInt();
}
}
Kotlinͷίʔυ
class KLists {
companion object {
@JvmStatic
fun getStringList(): List = listOf("")
@JvmStatic
fun getMutableStringList(): MutableList = mutableListOf("")
}
}
Slide 56
Slide 56 text
Javaͷίʔυ
List list0 = KLists.getStringList();
List list1 = KLists.getMutableStringList();
Slide 57
Slide 57 text
ͰJavaͷjava.u'l.List
Kotlin͔ΒͲ͏ݟ͑Δ?
Slide 58
Slide 58 text
Javaͷίʔυ
public class JLists {
public static List getList() {
return new ArrayList<>();
}
}
Slide 59
Slide 59 text
Kotlinͷίʔυ
// IDEͰͷදࣔʰ(Mutable)List!ʱܕ
val list = JLists.getList()
println(list.count)
// MutableListͷϝιου͕asͳ͠ͰݺΔ
list.clear()
println(list.count)
Slide 60
Slide 60 text
MutableList͡Όͳͯ͘Listͱͯ͠ѻ͍͍ͨ߹ʁ
Slide 61
Slide 61 text
@ReadOnly
Slide 62
Slide 62 text
@ReadOnlyͱ@MutableΛ༩ͨ͠Javaͷίʔυ(@NotNull)
public class JLists {
@NotNull
@ReadOnly
public static List getReadonlyList() {
return new ArrayList<>();
}
@NotNull
@Mutable
public static List getMutableList() {
return new ArrayList<>();
}
}
Slide 63
Slide 63 text
Kotlinͷίʔυ
// kotlin.collections.List
val list = JLists.getReadonlyList()
// ͜ΕOK
val list : List = JLists.getReadonlyList()
// ίϯύΠϧΤϥʔ
val list : MutableList = JLists.getReadonlyList()
println(list.count()) // OK
list.clear() // ίϯύΠϧΤϥʔ
@ReadOnlyͱ@MutableΛ͚ͭΔ͜ͱͰมԽ
Slide 64
Slide 64 text
// kotlin.collections.MutableList
val list = JLists.getMutableList()
// ͜ΕOK
val list : MutableList = JLists.getMutableList()
println(list.count()) // OK
list.clear() // OK
// MutableListListΛܧঝ͍ͯ͠ΔͷͰɺͭ͜͡ΕOK
// ͨͩclearͱ͔addݺͳ͍
// val list : List = JLists.getMutableList()
@ReadOnlyͱ@MutableΛ͚ͭΔ͜ͱͰมԽ