Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaからみたKotlin

 JavaからみたKotlin

Amane Nikaido

October 17, 2017
Tweet

More Decks by Amane Nikaido

Other Decks in Programming

Transcript

  1. +BWB͔Βݟͨ
    ,PUMJO

    ೋ֊ಊɹว

    View full-size slide

  2. w ϓϩϑΟʔϧ
    w ,PUMJOָ͍͠
    w +BWB͔Βݟͨ,PUMJO
    w ·ͱΊ
    ΋͘͡

    View full-size slide

  3. ೋ֊ಊว ʹ͔͍Ͳ͏͋·Ͷ

    !BLBJEP!BOJLBJEP
    ,PUMJO͕޷͖Ͱ͢
    ϓϩϑΟʔϧ
    ͨ·ʹ,PUMJOʹ13ૹͬͯ·͢

    View full-size slide

  4. ,PUMJOͷ͕͜͜޷͖
    ϘΠϥʔϓϨʔτΛݮΒͤΔ
    /VMM4BGFUZ
    XIFO TXJUDI
    ͷCSFBLॻ͔ͳ͍͍ͯ͘
    4NBSU$BTU
    ηϛίϩϯॻ͔ͳ͍͍ͯ͘
    είʔϓؔ਺
    ͔Θ͍͍
    ॻָ͍͍͍ͯͯ͠
    4".ม׵
    σϑΥϧτҾ਺

    View full-size slide

  5. ,PUMJOͷ͕͜͜޷͖
    ϘΠϥʔϓϨʔτΛݮΒͤΔ
    /VMM4BGFUZ
    XIFO TXJUDI
    ͷCSFBLॻ͔ͳ͍͍ͯ͘
    4NBSU$BTU
    ηϛίϩϯॻ͔ͳ͍͍ͯ͘
    είʔϓؔ਺
    ͔Θ͍͍
    ॻָ͍͍͍ͯͯ͠
    4".ม׵
    σϑΥϧτҾ਺

    View full-size slide

  6. 5IFGVOLFZXPSEJT
    VTFEUPEFDMBSFB
    GVODUJPO
    1SPHSBNNJOHJO
    ,PUMJOJTMPUTPGGVO
    JOEFFE

    View full-size slide

  7. ,PUMJOָ͍͠ʂ

    View full-size slide

  8. ,PUMJOָ͍͠ʂ

    View full-size slide

  9. ͚Ͳɺ+BWBͷؾ࣋ͪ
    ΋ߟ͑ͯ͋͛Αʁ

    View full-size slide

  10. +BWB͔Βݟͨ
    ,PUMJO

    ೋ֊ಊɹว

    View full-size slide

  11. w ,PUMJOͱ+BWBͷޓ׵ੑ
    w +BWB͔Β,PUMJOͷίʔυΛݺͼग़ͤΔ
    w ,PUMJO͔Β+BWBͷίʔυΛݺͼग़ͤΔ
    w ,PUMJOͱ+BWBͷݴޠ࢓༷ࠩ
    w PCKFDU
    w UPQMFWFMGVODUJPO
    w EFGBVMUQBSBNFUFS
    w FUD
    ,PUMJOͱ+BWB

    View full-size slide

  12. 0CKFDU
    5PQMFWFMGVODUJPO
    &YUFOTJPO
    %FGBVMU"SHVNFOU
    %FGBVMU*NQMFNFOUBUJPOJO*OUFSGBDF
    ࠓ೔+BWB͔ΒݟΔ΋ͷ

    View full-size slide

  13. ਐΊํ
    ୯ମςετॻ͘
    ίʔυॻ͘

    View full-size slide

  14. 0CKFDU
    object KotlinObject {
    fun hello() : String {
    return "Hello Kotlin!"
    }
    fun hello2() = "Hello Kotlin!"
    }
    ˞γϯάϧτϯͰ͢
    ,PUMJOͷ0CKFDU
    ˞IFMMP
    ͱIFMMP
    ͸CZUFDPEFʹ͢Δͱશ͘ಉ͡Ͱ͢

    View full-size slide

  15. 0CKFDU
    import jp.a2kaido.skfj.KotlinObject as KO
    class KotlinObjectFromKotlinTest {
    @Test
    fun KotlinObject() {
    Assert.assertEquals("Hello Kotlin!", KO.hello())
    Assert.assertEquals("Hello Kotlin!", KO.hello2())
    }
    }
    ,PUMJO͔Βݟͨ৔߹

    View full-size slide

  16. 0CKFDU
    public class KotlinObjectFromJavaTest {
    @Test
    public void KotlinObject() {
    Assert.assertEquals("Hello Kotlin!", KotlinObject.INSTANCE.hello());
    Assert.assertEquals("Hello Kotlin!", KotlinObject.INSTANCE.hello2());
    }
    }
    +BWB͔Βݟͨ৔߹

    View full-size slide

  17. 0CKFDU
    public class KotlinObjectFromJavaTest {
    @Test
    public void KotlinObject() {
    Assert.assertEquals("Hello Kotlin!", KotlinObject.INSTANCE.hello());
    Assert.assertEquals("Hello Kotlin!", KotlinObject.INSTANCE.hello2());
    }
    }
    +BWB͔Βݟͨ৔߹
    ˞*/45"/$&ʜ

    View full-size slide

  18. 0CKFDU
    ,PUMJOͷ0CKFDU
    object KotlinObject2 {
    @JvmStatic
    fun hello() : String {
    return "Hello Kotlin!"
    }
    @JvmStatic
    fun hello2() = "Hello Kotlin!"
    }

    View full-size slide

  19. 0CKFDU
    +BWB͔Βݟͨ৔߹
    public class KotlinObjectFromJavaTest {
    @Test
    public void KotlinObject() {
    Assert.assertEquals("Hello Kotlin!", KotlinObject.INSTANCE.hello());
    Assert.assertEquals("Hello Kotlin!", KotlinObject.INSTANCE.hello2());
    }
    @Test
    public void KotlinObject2() {
    Assert.assertEquals("Hello Kotlin!", KotlinObject2.hello());
    Assert.assertEquals("Hello Kotlin!", KotlinObject2.hello2());
    }
    }
    ,PUMJO0CKFDUHFU*OTUBODF
    IFMMP
    ͱॻ͖͍ͨ

    View full-size slide

  20. 0CKFDU
    5PQMFWFMGVODUJPO
    &YUFOTJPO
    %FGBVMU"SHVNFOU
    %FGBVMU*NQMFNFOUBUJPOJO*OUFSGBDF
    ࠓ೔+BWB͔ΒݟΔ΋ͷ

    View full-size slide

  21. 5PQMFWFMGVODUJPO
    ,PUMJOͷ5PQMFWFMGVODUJPO
    fun hello() : String {
    return "Hello Kotlin!"
    }
    ,PUMJO'VODUJPOLU
    ˞Ϋϥεʹଐ͞ͳ͍ؔ਺

    View full-size slide

  22. 5PQMFWFMGVODUJPO
    ,PUMJO͔Βݟͨ৔߹
    class KotlinFunctionFromKotlinTest {
    @Test
    fun KotlinFunction() {
    Assert.assertEquals("Hello Kotlin!", hello())
    }
    }

    View full-size slide

  23. 5PQMFWFMGVODUJPO
    +BWB͔Βݟͨ৔߹
    public class KotlinFunctionFromJavaTest {
    @Test
    public void KotlinFunction() {
    Assert.assertEquals("Hello Kotlin!", KotlinFunctionKt.hello());
    }
    }
    ,PUMJO'VODUJPO,Uͱ͍͏Ϋϥε͕Ͱ͖ͯΔ
    ͦͷΫϥεʹTUBUJDϝιου͕ੜ͑ͯΔ

    View full-size slide

  24. 5PQMFWFMGVODUJPO
    ,PUMJOͷ5PQMFWFMGVODUJPO
    fun hello() : String {
    return "Hello Kotlin!"
    }
    ,PUMJO'VODUJPOLU
    ,PUMJO'VODUJPO,Uͱ͍͏໊લ͕ؾ࣋ͪѱ͍ͷͰɺ

    View full-size slide

  25. 5PQMFWFMGVODUJPO
    ,PUMJOͷ5PQMFWFMGVODUJPO
    @file:JvmName("KotlinFunctionRefine")
    package jp.a2kaido.skfj.function
    fun helloRefine() : String {
    return "Hello Kotlin!"
    }
    ,PUMJO'VODUJPO3FpOFLU
    ໊લΛ͚ͭΔ

    View full-size slide

  26. 5PQMFWFMGVODUJPO
    +BWB͔Βݟͨ৔߹
    public class KotlinFunctionFromJavaTest {
    @Test
    public void KotlinFunction() {
    Assert.assertEquals("Hello Kotlin!", KotlinFunctionKt.hello());
    }
    @Test
    public void KotlinObjectRefine() {
    Assert.assertEquals("Hello Kotlin!", KotlinFunctionRefine.helloRefine());
    }
    }
    ,Uͷ··ͩͱؾ࣋ͪѱ͍ͷͰΞϊςʔγϣϯͰ

    ໊લΛ͚ͭͯ͋͛·͠ΐ͏
    +BWB͔ΒͰ΋໰୊ͳ͘ݺͼग़ͤΔʂ

    View full-size slide

  27. 0CKFDU
    5PQMFWFMGVODUJPO
    &YUFOTJPO
    %FGBVMU"SHVNFOU
    %FGBVMU*NQMFNFOUBUJPOJO*OUFSGBDF
    ࠓ೔+BWB͔ΒݟΔ΋ͷ

    View full-size slide

  28. &YUFOTJPO
    ,PUMJOͷ&YUFOTJPO
    fun String.hello() = "Hello Kotlin!"
    4USJOH&YUFOTJPOLU
    @file:JvmName("String")
    package jp.a2kaido.skfj.extension
    fun String.helloRefine() = "Hello Kotlin!"
    4USJOH&YUFOTJPO3FpOFLU

    View full-size slide

  29. &YUFOTJPO
    ,PUMJO͔Βݟͨ৔߹
    class StringExtensionFromKotlinTest {
    @Test
    fun StringExtension() {
    Assert.assertEquals("Hello Kotlin!", "hoge".hello())
    }
    @Test
    fun StringExtensionRefine() {
    Assert.assertEquals("Hello Kotlin!", "hoge".helloRefine())
    }
    }

    View full-size slide

  30. &YUFOTJPO
    +BWB͔Βݟͨ৔߹
    public class StringExtensionFromJavaTest {
    @Test
    public void StringExtension() {
    Assert.assertEquals("Hello Kotlin!", StringExtensionKt.hello("hoge"));
    }
    @Test
    public void StringExtensionRefile() {
    Assert.assertEquals("Hello Kotlin!", String.helloRefine("hoge"));
    }
    }
    4USJOH&YUFOTJPO,Uͱ͍͏Ϋϥε͕Ͱ͖ͯΔ
    ϨγʔόʔΛҾ਺Ͱड͚औΔ
    ݺͼग़ͤΔ͚Ͳʜ&YUFOTJPOͷ͝ར༻͸ܭըతʹ

    View full-size slide

  31. 0CKFDU
    5PQMFWFMGVODUJPO
    &YUFOTJPO
    %FGBVMU"SHVNFOU
    %FGBVMU*NQMFNFOUBUJPOJO*OUFSGBDF
    ࠓ೔+BWB͔ΒݟΔ΋ͷ

    View full-size slide

  32. %FGBVMU"SHVNFOU
    ,PUMJOͷ%FGBVMU"SHVNFOU
    class KotlinDefaultArgument {
    fun hello(s : String = "Kotlin") = "Hello $s!"
    }
    class KotlinDefaultArgumentRefine {
    @JvmOverloads
    fun hello(s : String = "Kotlin") = "Hello $s!"
    }

    View full-size slide

  33. %FGBVMU"SHVNFOU
    ,PUMJO͔Βݟͨ৔߹
    class KotlinDefaultArgumentFromKotlinTest {
    @Test
    fun kotlinDefaultArgument() {
    val target = KotlinDefaultArgument()
    Assert.assertEquals("Hello Kotlin!", target.hello())
    Assert.assertEquals("Hello Java!", target.hello("Java"))
    }
    @Test
    fun kotlinDefaultArgumentRefine() {
    val target = KotlinDefaultArgumentRefine()
    Assert.assertEquals("Hello Kotlin!", target.hello())
    Assert.assertEquals("Hello Java!", target.hello("Java"))
    }
    }

    View full-size slide

  34. %FGBVMU"SHVNFOU
    +BWB͔Βݟͨ৔߹
    public class KotlinDefaultArgumentFromJavaTest {
    @Test
    public void KotlinDefaultArgument() {
    KotlinDefaultArgument target = new KotlinDefaultArgument();
    // cannot compile
    // Assert.assertEquals("Hello Kotlin!", target.hello());
    Assert.assertEquals("Hello Java!", target.hello("Java"));
    }
    @Test
    public void KotlinDefaultArgumentRefine() {
    KotlinDefaultArgumentRefine target = new KotlinDefaultArgumentRefine();
    Assert.assertEquals("Hello Kotlin!", target.hello());
    Assert.assertEquals("Hello Java!", target.hello("Java"));
    }
    }
    @JvmOverloadsΛ͚ͭͳ͍ͱҾ਺ͳ͠ͷݺͼग़͕͠Ͱ͖ͳ͍

    View full-size slide

  35. 0CKFDU
    5PQMFWFMGVODUJPO
    &YUFOTJPO
    %FGBVMU"SHVNFOU
    %FGBVMU*NQMFNFOUBUJPOJO*OUFSGBDF
    ࠓ೔+BWB͔ΒݟΔ΋ͷ

    View full-size slide

  36. %FGBVMU*NQMFNFOUBUJPO
    interface DefaultInterface {
    fun hello() = "Hello Kotlin!"
    fun hello2() : String
    }
    ,PUMJOͷ%FGBVMU*NQMFNFOUBUJPO

    View full-size slide

  37. %FGBVMU*NQMFNFOUBUJPO
    class DefaultInterfaceKotlinImpl : DefaultInterface {
    override fun hello2() = "Hello Kotlin!"
    }
    ,PUMJOͰ࣮૷

    View full-size slide

  38. %FGBVMU*NQMFNFOUBUJPO
    public class DefaultInterfaceJavaImpl implements DefaultInterface {
    @NotNull
    @Override
    public String hello() { // ࣮૷ඞਢ
    return DefaultImpls.hello(this); // DefaultImpls͕ੜ͑ͯΔ
    }
    @NotNull
    @Override
    public String hello2() {
    return "Hello Kotlin!";
    }
    }
    +BWBͰ࣮૷
    ஌Βͳ͍ͱແཧήʔɻσϑΥϧτΛແࢹ࣮ͯ͠૷ͦ͠͏ɻ

    View full-size slide

  39. 0CKFDU
    5PQMFWFMGVODUJPO
    &YUFOTJPO
    %FGBVMU"SHVNFOU
    %FGBVMU*NQMFNFOUBUJPOJO*OUFSGBDF
    ࠓ೔+BWB͔ΒݟΔ΋ͷ

    View full-size slide

  40. w ,PUMJOָ͍͠ʂ
    w +BWB͔Βݺͼग़͞ΕΔॴ͸গ͠ߟྀͯ͠
    ͋͛ͯͶ
    w 'VMM,PUMJOͳΒ+BWBͷؾ࣋ͪߟ͑ͳͯ͘
    ͍͍
    ·ͱΊ

    View full-size slide

  41. w αϯϓϧίʔυ
    w IUUQTHJUIVCDPNBLBJEPTFF
    LPUMJOGSPNKBWBQSFTFOUBUJPO
    ࢿྉ

    View full-size slide

  42. ͝੩ௌ͋Γ͕ͱ͏

    ͍͟͝·ͨ͠

    View full-size slide