Java Day Tokyo 2013 - Java Puzzlers

Java Day Tokyo 2013 - Java Puzzlers

759f46b226f3fc59661883355503a874?s=128

Takayuki Okazaki

May 14, 2013
Tweet

Transcript

  1. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 1 ؼ͖ͬͯͨ+BWBύζϥʔ ೔ຊ+BWBάϧʔϓ
  2. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 2 3 public class JavaPuzzlers { public static void main(String... args) { http://www.java-users.jp System.out.println(“Japan Java User Group Presents!”); } } Java SE 8 : Rules of Java Puzzlers 1) Japan Java User Group Presents! 2) Java Puzzlers 3) 0xcafebabe 4) ͦͷଞ όʔδϣϯࢦఆ͕͋Γ·͢ ͢΂ͯ୒Ͱ͢ શһࢀՃͰ͢ʂ
  3. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 3 3 public class JavaPuzzlers { public static void main(String... args) { http://www.java-users.jp System.out.println(“Japan Java User Group Presents!”); } } Java SE 8 : Rules of Java Puzzlers 1) Japan Java User Group Presents! 2) Java Puzzlers 3) 0xcafebabe 4) ͦͷଞ όʔδϣϯࢦఆ͕͋Γ·͢ ͢΂ͯ୒Ͱ͢ શһࢀՃͰ͢ʂ
  4. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 4 import java.math.BigDecimal; import java.util.Arrays; public class BalanceSheet { public BigDecimal asset, liabilities, equity; public BalanceSheet(BigDecimal asset, BigDecimal liabilities, BigDecimal equity) { this.asset = asset; this.liabilities = liabilities; this.equity = equity; } public static void main(String... args) { long c = Arrays.asList( new BalanceSheet(new BigDecimal("100"), new BigDecimal(50.75f), new BigDecimal("49.25")), new BalanceSheet(new BigDecimal("10000"), new BigDecimal(5_075), new BigDecimal(4_925.0))) .stream() .parallel() .filter(bs -> bs.asset.equals(bs.equity.add(bs.liabilities))) .count(); System.out.println(c); } } Java SE 8: Balance Sheet 1) 0 2) 1 3) 2 4) ෆఆ
  5. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 5 ਖ਼ղ
  6. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 6 import java.math.BigDecimal; import java.util.Arrays; public class BalanceSheet { public BigDecimal asset, liabilities, equity; public BalanceSheet(BigDecimal asset, BigDecimal liabilities, BigDecimal equity) { this.asset = asset; this.liabilities = liabilities; this.equity = equity; } public static void main(String... args) { long c = Arrays.asList( new BalanceSheet(new BigDecimal("100"), new BigDecimal(50.75f), new BigDecimal("49.25")), new BalanceSheet(new BigDecimal("10000"), new BigDecimal(5_075), new BigDecimal(4_925.0))) .stream() .parallel() .filter(bs -> bs.asset.equals(bs.equity.add(bs.liabilities))) .count(); System.out.println(c); } } Java SE 8: Balance Sheet 1) 0 2) 1 3) 2 4) ෆఆ
  7. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 7 long c = Arrays.asList( new BalanceSheet(new BigDecimal("100"), new BigDecimal(50.75f), new BigDecimal("49.25")), new BalanceSheet(new BigDecimal("10000"), new BigDecimal(5_075), new BigDecimal(4_925.0))) .stream() // ίϨΫγϣϯΛStreamʹม׵ .parallel() // ฒྻ࣮ߦՄೳ .filter(bs -> bs.asset.equals(bs.equity.add(bs.liabilities))) // StreamʹϑΟϧλΛద༻ .count(); // ετϦʔϜͷཁૉ਺ΛΧ΢ϯτ Java SE 8: Balance Sheet +BWB4&Ͱ͸৽͘͠ϥϜμࣜͱίϨΫγϣϯ ૢ࡞"1*͕ಋೖ͞Ε·ͨ͠ ղઆ
  8. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 8 System.out.println(new BigDecimal("100")); // 100 System.out.println(new BigDecimal(50.75f)); // 50.75 System.out.println(new BigDecimal("49.25")); // 49.25 System.out.println(new BigDecimal(50.75f) .add(new BigDecimal("49.25"))); // 100.00 System.out.println(new BigDecimal("10000")); // 10000 System.out.println(new BigDecimal(5_075)); // 5075 System.out.println(new BigDecimal(4_925.0)); // 4925 System.out.println(new BigDecimal(5_075) .add(new BigDecimal(4_925.0))); // 10000 Java SE 8: Balance Sheet ͱ͜ΖͰɺͦΕͧΕͷ#JH%FDJNBM͸ ͲͷΑ͏ͳ஋Ͱ͠ΐ͏͔ ղઆ
  9. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 9 BigDecimal a = new BigDecimal("10"); BigDecimal b = new BigDecimal("10.0"); System.out.println(a.equals(b)); // false Java SE 8: Balance Sheet #JH%FDJNBM͸খ਺఺ҎԼͷܻ਺Λ؅ཧ͍ͯͯ͠ɺ #JH%FDJNBMFRVBMT͸ͱΛ۠ผ͠·͢ ղઆ
  10. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 10 Java SE 8: Balance Sheet ղઆ ஋Λൺֱ͢Δʹ͸࣍ͷΑ͏ʹ #JH%FDJNBMDPNQBSF5PΛ࢖͍·͢ long c = Arrays.asList( new BalanceSheet(new BigDecimal("100"), new BigDecimal(50.75f), new BigDecimal("49.25")), new BalanceSheet(new BigDecimal("10000"), new BigDecimal(5_075), new BigDecimal(4_925.0))) .stream() .parallel() .filter(bs -> bs.asset.compareTo(bs.equity.add(bs.liabilities)) == 0) .count();
  11. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 11 Java SE 8: Balance Sheet ڭ܇ #JH%FDJNBMͰͷൺֱ͸ɺFRVBMTɺ DPNQBSF5PͦΕͧΕҙຯ͕ҟͳΓ·͢ ൺֱͷҙਤΛҙࣝͯ͠࢖͍෼͚·͠ΐ͏
  12. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 12 import java.util.*; public class Html5 { public static void main(String... args) { description("applet"); } public static void description(String tag) { String key = tag.startsWith("<") ? tag : decorate(tag); Map<String,String> status = new HashMap<>(); status.put("<progress>", "new"); status.put("<tt>", "deprecated"); status.put("<applet>", "removed"); switch (status.get(key)) { case "removed": System.out.println(key + "͸HTML5͔Β࡟আ͞Ε·ͨ͠"); case "new": System.out.println(key + "͸HTML5Ͱ৽͘͠௥Ճ͞Ε·ͨ͠"); case "deprecated": System.out.println(key + "͸HTML5͔Βඇਪ঑ʹͳΓ·ͨ͠"); default: // Կ΋͠ͳ͍ } } public static String decorate(String name) { StringBuilder n = new StringBuilder('<'); n.append(name); n.append('>'); return n.toString(); } } Java SE 7: HTML5 1) <applet>͸HTML5͔Β࡟আ͞Ε·ͨ͠ 2) Կ΋ग़ྗ͞Εͳ͍ 3) ྫ֎͕εϩʔ͞ΕΔ 4) ίϯύΠϧΤϥʔ
  13. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 13 ਖ਼ղ
  14. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 14 import java.util.*; public class Html5 { public static void main(String... args) { description("applet"); } public static void description(String tag) { String key = tag.startsWith("<") ? tag : decorate(tag); Map<String,String> status = new HashMap<>(); status.put("<progress>", "new"); status.put("<tt>", "deprecated"); status.put("<applet>", "removed"); switch (status.get(key)) { case "removed": System.out.println(key + "͸HTML5͔Β࡟আ͞Ε·ͨ͠"); case "new": System.out.println(key + "͸HTML5Ͱ৽͘͠௥Ճ͞Ε·ͨ͠"); case "deprecated": System.out.println(key + "͸HTML5͔Βඇਪ঑ʹͳΓ·ͨ͠"); default: // Կ΋͠ͳ͍ } } public static String decorate(String name) { StringBuilder n = new StringBuilder('<'); n.append(name); n.append('>'); return n.toString(); } } Java SE 7: HTML5 1. <applet>͸HTML5͔Β࡟আ͞Ε·ͨ͠ 2. Կ΋ग़ྗ͞Εͳ͍ 3) ྫ֎͕εϩʔ͞ΕΔ 4. ίϯύΠϧΤϥʔ
  15. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 15 switch (status.get(key)) { case "removed": System.out.println(key + "͸HTML5͔Β࡟আ͞Ε·ͨ͠"); break; case "new": System.out.println(key + "͸HTML5Ͱ৽͘͠௥Ճ͞Ε·ͨ͠"); break; case "deprecated": System.out.println(key + "͸HTML5͔Βඇਪ঑ʹͳΓ·ͨ͠"); break; default: // Կ΋͠ͳ͍ } Java SE 7: HTML5 CSFBL͕͋Γ·ͤΜͶ 1) <applet>͸HTML5͔Β࡟আ͞Ε·ͨ͠ 2) Կ΋ग़ྗ͞Εͳ͍ 3) ྫ֎͕εϩʔ͞ΕΔ 4) ίϯύΠϧΤϥʔ ղઆ
  16. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 16 switch (status.get(key)) { case "removed": System.out.println(key + "͸HTML5͔Β࡟আ͞Ε·ͨ͠"); break; case "new": System.out.println(key + "͸HTML5Ͱ৽͘͠௥Ճ͞Ε·ͨ͠"); break; case "deprecated": System.out.println(key + "͸HTML5͔Βඇਪ঑ʹͳΓ·ͨ͠"); break; default: // Կ΋͠ͳ͍ } Java SE 7: HTML5 +BWB4&͔ΒTXJUDIʹจࣈྻ͕ ࢖͑ΔΑ͏ʹͳΓ·ͨ͠ ίϯύΠϧΤϥʔʹ͸ͳΓ·ͤΜ 1) <applet>͸HTML5͔Β࡟আ͞Ε·ͨ͠ 2) Կ΋ग़ྗ͞Εͳ͍ 3) ྫ֎͕εϩʔ͞ΕΔ 4) ίϯύΠϧΤϥʔ ղઆ
  17. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 17 StringBuilder n = new StringBuilder(99); // '<' (\u003c) => 99 (int)ʹΩϟετ͞ΕΔɺ͢ͳΘͪ 99จࣈ෼ͷόοϑΝ֬อ n.append(name); n.append('>'); return n.toString(); // applet> ͱͳΔ Java SE 7: HTML5 4USJOH#VJMEFS 4USJOH#VGGFS ʹ͸DIBSܕͷ ίϯετϥΫλ͕͋Γ·ͤΜ 1) <applet>͸HTML5͔Β࡟আ͞Ε·ͨ͠ 2) Կ΋ग़ྗ͞Εͳ͍ 3) ྫ֎͕εϩʔ͞ΕΔ 4) ίϯύΠϧΤϥʔ ղઆ
  18. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 18 switch (status.get(key)) { case "removed": System.out.println(key + "͸HTML5͔Β࡟আ͞Ε·ͨ͠"); case "new": System.out.println(key + "͸HTML5Ͱ৽͘͠௥Ճ͞Ε·ͨ͠"); case "deprecated": System.out.println(key + "͸HTML5͔Βඇਪ঑ʹͳΓ·ͨ͠"); default: // Կ΋͠ͳ͍ } Java SE 7: HTML5 TUBUVTʹ͸ΩʔBQQMFU͕͋Γ·ͤΜͷͰɺ TUBUVTHFU LFZ ͸OVMMΛฦ͠·͢ 1) <applet>͸HTML5͔Β࡟আ͞Ε·ͨ͠ 2) Կ΋ग़ྗ͞Εͳ͍ 3) ྫ֎͕εϩʔ͞ΕΔ 4) ίϯύΠϧΤϥʔ ˠ null ղઆ
  19. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 19 switch (status.get(key)) { Java SE 7: HTML5 TXJUDIͷ͕ࣜOVMMͷ৔߹ɺ /VMM1PJOUFS&YDFQUJPO͕εϩʔ͞Ε·͢ +-4 1) <applet>͸HTML5͔Β࡟আ͞Ε·ͨ͠ 2) Կ΋ग़ྗ͞Εͳ͍ 3) ྫ֎͕εϩʔ͞ΕΔ 4) ίϯύΠϧΤϥʔ ˠ NullPointerException Exception in thread "main" java.lang.NullPointerException at Html5.description(Html5.java:10) at Html5.main(Html5.java:3) ղઆ
  20. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 20 Java SE 7: HTML5 DIBSܕ͔Βͷ҉໧ม׵ͳͲ͸੩తղੳ΍*%&ͷܯ ࠂͳͲͰݕग़Ͱ͖·͢ͷͰ׆༻͠·͠ΐ͏ TXJUDIߏจͰ͸ࣄલʹOVMMνΣοΫ͕ඞཁͰ͢ TXJUDIͰ͸CSFBLΛ๨Εͳ͍Α͏ʹ͠·͠ΐ͏ ڭ܇
  21. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 21 public class NationalDebt { public static void main(String... args) { long japanNationalDebt = -1_239_572_984_276_101L; long usNationalDebt = -16_841_611_990_513L; float usdJpy = 101.77f; System.out.println( Math.abs( Math.round( Math.min(usNationalDebt * usdJpy, japanNationalDebt)))); } } Java SE 7: ࠃ࠴ 1) 1.71397086E15ɹ 2) 1.23957298E15ɹ 3) ྫ֎͕εϩʔ͞ΕΔ 4) ͦΕҎ֎
  22. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 22 ਖ਼ղ
  23. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 23 public class NationalDebt { public static void main(String... args) { long japanNationalDebt = -1_239_572_984_276_101L; long usNationalDebt = -16_841_611_990_513L; float usdJpy = 101.77f; System.out.println( Math.abs( Math.round( Math.min(usNationalDebt * usdJpy, japanNationalDebt)))); } } Java SE 7: ࠃ࠴ 1) 1.71397086E15ɹ 2) 1.23957298E15ɹ 3) ྫ֎͕εϩʔ͞ΕΔ 4) ͦΕҎ֎
  24. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 24 Math.min(usNationalDebt * usdJpy, japanNationalDebt) ܕ͸ͦΕͧΕͲ͏ͳΔͰ͠ΐ͏͔ ղઆ long float long Java SE 7: ࠃ࠴
  25. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 25 Math.min(usNationalDebt * usdJpy, japanNationalDebt) MPOH qPBUͷܭࢉ͸ɺࣜதͰͻͱͭͰ΋ qPBU͕͋Ε͹qPBUʹม׵͞Εܭࢉ͞Ε·͢ +-4 ղઆ long * float long float Java SE 7: ࠃ࠴
  26. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 26 Math.min(usNationalDebt * usdJpy, japanNationalDebt) .BUIΫϥεʹ .BUINJO qPBU MPOH ͕ͳ͍ͨΊ ಉ༷ʹqPBUʹม׵͞Ε·͢ ղઆ long * float long float Math.min(float, float) float Java SE 7: ࠃ࠴
  27. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 27 Math.min(usNationalDebt * usdJpy, japanNationalDebt) ͜ͷ݁Ռ͸࣍ͷΑ͏ʹͳΓ·͢ ղઆ Math.min(-1.71397086E15, -1.23957298E15) -1.71397086E15 Java SE 7: ࠃ࠴
  28. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 28 .BUISPVOE qPBU ͷ໭Γ஋͸JOUͰ͢ SPVOE͸ɺ*OUFHFS."9@7"-6&ɺ *OUFHFS.*/@7"-6&Λ௒͑Δ஋ͷ৔߹ ࠷େ஋·ͨ͸࠷খ஋ʹਾ͑ஔ͖·͢͜ͷͨΊɺ ղઆ Math.round(-1.71397086E15) -1.71397086E15 < -2147483648 -2147483648 Java SE 7: ࠃ࠴
  29. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 29 .BUIBCT JOU ͸ઈର஋ΛͱΓ·͕͢ *OUFHFS.*/@7"-6&  ͷ৔߹ɺ ."9@7"-6&  ʹ͸ͳΒͣ *OUFHFS.*/@7"-6&  Λ ͦͷ··ฦ͠·͢ ղઆ Math.abs(-2147483648) -2147483648 Java SE 7: ࠃ࠴
  30. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 30 ղઆ -2147483648 public class NationalDebt { public static void main(String... args) { long japanNationalDebt = -1_239_572_984_276_101L; long usNationalDebt = -16_841_611_990_513L; float usdJpy = 101.77f; System.out.println( Math.abs( Math.round( Math.min(usNationalDebt * usdJpy, japanNationalDebt)))); } } 1) 1.71397086E15ɹ 2) 1.23957298E15ɹ 3) ྫ֎͕εϩʔ͞ΕΔ 4) ͦΕҎ֎ Java SE 7: ࠃ࠴
  31. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 31 .BUIBCT JOU ɺ.BUIBCT MPOH ͸ෛͷ஋Λ ฦ͢έʔε͕͋Δ EPVCMF΍ɺ#JH%FDJNBMͳͲΛར༻ ڭ܇ Java SE 7: ࠃ࠴
  32. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 32 public class DualPersonality { private int count = 0; public DualPersonality() { Runnable task = () -> { System.out.print(count+ " "); count++; if (count < 5) { run(); } }; task.run(); } private static void run() { System.out.println("RUN "); } public static void main(String... args) { new DualPersonality(); } } Java SE 8: Dual Personality 1) 0 1 2 3 4 2) 0 RUN 3) RUN 4) RUN RUN RUN RUN RUN
  33. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 33 ਖ਼ղ
  34. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 34 public class DualPersonality { private int count = 0; public DualPersonality() { Runnable task = () -> { System.out.print(count+ " "); count++; if (count < 5) { run(); } }; task.run(); } private static void run() { System.out.println("RUN "); } public static void main(String... args) { new DualPersonality(); } } Java SE 8: Dual Personality 1) 0 1 2 3 4 2) 0 RUN 3) RUN 4) RUN RUN RUN RUN RUN
  35. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 35 public class DualPersonality { private int count = 0; public DualPersonality() { Runnable task = () -> { System.out.print(count+ " "); count++; if (count < 5) { run(); // 1) DualPersonality#run } }; task.run(); // 2) Runnable#run } private static void run() { System.out.println("RUN "); } public static void main(String... args) { new DualPersonality(); } }  ͷSVO ͸%VBM1FSTPOBMJUZͷSVO͕ݺͼग़ ͞Εɺ ͷUBTLSVO ͸3VOOBCMFΠϯλϑΣ ʔεͷSVO͕ݺͼग़͞Ε·͢ ղઆ Java SE 8: Dual Personality
  36. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 36 public class DualPersonality { private int count = 0; public DualPersonality() { Runnable task = new Runnable() { @Override public void run() { System.out.print(count+ " "); count++; if (count < 5) { run(); } } }; task.run(); } private static void run() { System.out.println("RUN "); } public static void main(String... args) { new DualPersonality(); } } ΋͠+BWB4&·ͰͷΑ͏ʹແ໊ΫϥεΛ࢖ͬͨ ॻ͖ํΛͨ͠৔߹͸Ͳ͏Ͱ͠ΐ͏͔ ղઆ Java SE 8: Dual Personality
  37. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 37 public class DualPersonality { private int count = 0; public DualPersonality() { Runnable task = new Runnable() { @Override public void run() { System.out.print(count+ " "); count++; if (count < 5) { run(); } } }; task.run(); } private static void run() { System.out.println("RUN "); } public static void main(String... args) { new DualPersonality(); } } ͜ͷ৔߹͸ɺ࣍ͷ݁Ռ͕ಘΒΕ·͢ ղઆ Java SE 8: Dual Personality 0 1 2 3 4
  38. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 38 Runnable t1 = () -> { System.out.println(this); }; Runnable t2 = new Runnable() { @Override public void run() { System.out.println(this); } }; t1.run(); t2.run(); ࣍ͷΑ͏ʹUIJT͕ԿΛ͍ͯ͞͠Δ͔ௐ΂ͯΈΔͱΘ ͔Γ΍͍͢Ͱ͠ΐ͏ ղઆ Java SE 8: Dual Personality DualPersonality@5ca881b5 DualPersonality$1@6f496d9f 3VOOBCMFΛ࣮૷ͨ͠ແ໊Ϋϥε͸ΠϯφʔΫϥε Λࢦ͠ɺ-BNCEBΛ࢖ͬͨ৔߹͸΋ͱͷΫϥεΛ ࢦ͠·͢ +434FDUJPO
  39. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 39 -BNCEBࣜͷதͰͷUIJTʹؾΛ͚ͭΔ -BNCEBࣜͰॻ͘ͱ͸͍͑Ͳ΋ɺ࢖ΘΕ͍ͯΔΠ ϯλϑΣʔεΛཧղͯ͠࢖༻͢ΔΑ͏ʹ͢Δ Java SE 8: Dual Personality ڭ܇
  40. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 40 class Foo<T> { public void foo() { System.out.println("Foo"); } } public class WhosFoo { public static void bar1(Foo foo) { foo.foo(); } public static void bar2(Foo<?> foo) { foo.foo(); } public static void bar3(Foo<String> foo) { foo.foo(); } public static void main(String[] args) { bar1(new Foo<>()); bar2(new Foo<>()); bar3(new Foo<>()); } } Java SE 7: Who's Foo? 1) Foo Foo Fooɹ 2) Fooɹ 3) Warning:WhosFoo.javaͷૢ࡞͸ɺະνΣοΫ·ͨ͸҆શͰ͸͋Γ·ͤΜɻ 4) ίϯύΠϧΤϥʔ
  41. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 41 ਖ਼ղ
  42. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 42 class Foo<T> { public void foo() { System.out.println("Foo"); } } public class WhosFoo { public static void bar1(Foo foo) { foo.foo(); } public static void bar2(Foo<?> foo) { foo.foo(); } public static void bar3(Foo<String> foo) { foo.foo(); } public static void main(String[] args) { bar1(new Foo<>()); bar2(new Foo<>()); bar3(new Foo<>()); } } Java SE 7: Who's Foo? 1) Foo Foo Fooɹ 2) Fooɹ 3) Warning:WhosFoo.javaͷૢ࡞͸ɺະνΣοΫ·ͨ͸҆શͰ͸͋Γ·ͤΜɻ 4) ίϯύΠϧΤϥʔ
  43. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 43 WhosFoo.java:13: Τϥʔ: Ϋϥε WhosFooͷϝιου bar3͸ࢦఆ͞Εͨܕʹద༻Ͱ͖·ͤΜɻ bar3(new Foo<>()); ^ ظ଴஋: Foo<String> ݕग़஋: Foo<Object> ཧ༝: ࣮Ҿ਺Foo<Object>͸ϝιουݺग़ม׵ʹΑͬͯFoo<String>ʹม׵Ͱ͖·ͤΜ Τϥʔ1ݸ +BWB4&Ͱ͸ίϯύΠϧΤϥʔͱͳΓ·͢ ղઆ Java SE 7: Who's Foo? +BWB4&Ͱ͸ΤϥʔͱͳΒ࣮ͣߦ͞Ε·͢ % java -version java version "1.8.0-ea" Java(TM) SE Runtime Environment (build 1.8.0-ea-b88) Java HotSpot(TM) 64-Bit Server VM (build 25.0-b30, mixed mode) % java WhosFoo Foo Foo Foo
  44. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 44 Map<String, Map<Integer, List<String>>> complexMap = new HashMap<>(); +BWB4&Ͱ͸μΠϠϞϯυԋࢉࢠ͕ಋೖ͞Ε ͯɺΦϒδΣΫτΛੜ੒͢Δ৔߹ʹδΣωϦΫεͷ ύϥϝʔλΛهड़͢Δඞཁ͕ͳ͘ͳΓ·ͨ͠ ղઆ Java SE 7: Who's Foo? μΠϠϞϯυԋࢉࢠΛ࢖༻͢Δͱɺ ίϯύΠϥ͕ܕਪ࿦Λߦͳ͍ɺ ద੾ͳܕΛิ׬͠·͢
  45. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 45 boolean useArrayList = true; List<String> list = useArrayList ? new ArrayList<>(): new LinkedList<>(); // ERROR ͔͠͠ɺ+BWB4&Ͱ͸ܕਪ࿦ʹ੍ݶ͕͋Γ·͢ Ұൠతʹ͸ม਺΁ͷ୅ೖͰͷΈ࢖༻Ͱ͖·͢ ͨͱ͑͹ɺ௚઀ม਺ʹ୅ೖΛߦͳΘͳ͍ࡾ߲ԋࢉࢠ Ͱ͸μΠϠϞϯυԋࢉࢠΛ࢖༻Ͱ͖·ͤΜ ղઆ Java SE 7: Who's Foo?
  46. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 46 ಉ༷ʹϝιουͷҾ਺ʹ΋μΠϠϞϯυԋࢉࢠΛ࢖ ༻͢Δ͜ͱ͕Ͱ͖·ͤΜ ͕ͨͬͯ͠ɺCBSɺCBSɺCBSͷίʔϧ͕͍ͣ Ε΋ίϯύΠϧΤϥʔʹͳΓͦ͏Ͱ͢ ղઆ Java SE 7: Who's Foo? public static void bar1(Foo foo) { foo.foo(); } public static void bar2(Foo<?> foo) { foo.foo(); } public static void bar3(Foo<String> foo) { foo.foo(); } public static void main(String[] args) { bar1(new Foo<>()); // ERROR? bar2(new Foo<>()); // ERROR? bar3(new Foo<>()); // ERROR? }
  47. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 47 ࣮ࡍʹ͸CBSͷϝιουίʔϧ͚͕ͩ ίϯύΠϧΤϥʔʹͳΓ·͢ ղઆ Java SE 7: Who's Foo? public static void bar1(Foo foo) { foo.foo(); } public static void bar2(Foo<?> foo) { foo.foo(); } public static void bar3(Foo<String> foo) { foo.foo(); } public static void main(String[] args) { bar1(new Foo<>()); // OK bar2(new Foo<>()); // OK bar3(new Foo<>()); // ERROR? }
  48. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 48 ϝιουͷҾ਺ͰͷμΠϠϞϯυԋࢉࢠ͸ܕਪ࿦ʹ ࣦഊ͢ΔͨΊɺδΣωϦΫεύϥϝʔλʹ0CKFDU ΫϥεΛ࢖༻͍ͯ͠Δ͜ͱͱಉ͡ʹͳΓ·͢ ղઆ Java SE 7: Who's Foo? public static void bar1(Foo foo) { foo.foo(); } public static void bar2(Foo<?> foo) { foo.foo(); } public static void bar3(Foo<String> foo) { foo.foo(); } public static void main(String[] args) { bar1(new Foo<>()); bar2(new Foo<>()); bar3(new Foo<>()); } bar3(new Foo<Object>())
  49. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 49 CBSϝιου͸δΣωϦΫεΛ࢖༻͍ͯ͠ͳ͍ SBXܕͷͨΊɺCBSϝιουͷίʔϧʹ͸ δΣωϦΫε͕ඞཁ͋Γ·ͤΜ ղઆ Java SE 7: Who's Foo? public static void bar1(Foo foo) { foo.foo(); } public static void bar2(Foo<?> foo) { foo.foo(); } public static void bar3(Foo<String> foo) { foo.foo(); } public static void main(String[] args) { bar1(new Foo<>()); bar2(new Foo<>()); bar3(new Foo<>()); } bar1(new Foo())
  50. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 50 CBSϝιου͸δΣωϦΫεʹϫΠϧυΧʔυΛ ࢖༻͍ͯ͠·͢ɻ͜ͷ৔߹ະ஌ͷܕͷΠϨΠδϟʔ ͱΈͳ͞Εɺ੍໿ͳ͠ϫΠϧυΧʔυͷ৔߹ɺ 0CKFDUʹͳΓ·͢ ղઆ Java SE 7: Who's Foo? public static void bar1(Foo foo) { foo.foo(); } public static void bar2(Foo<?> foo) { foo.foo(); } public static void bar3(Foo<String> foo) { foo.foo(); } public static void main(String[] args) { bar1(new Foo<>()); bar2(new Foo<>()); bar3(new Foo<>()); } bar2(new Foo<Object>()) ͨ·ͨ·OFX'PP0CKFDU ͱҰக͢ΔͨΊ ίϯύΠϧΤϥʔʹͳΓ·ͤΜ
  51. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 51 δΣωϦΫεΛ࢖༻͢ΔΫϥεͰ͸SBXܕΛ࢖༻ ͤͣʹɺඞͣδΣωϦΫεΛ࢖༻͢Δ μΠϠϞϯυԋࢉࢠͷܕਪ࿦ͷ੍ݶΛཧղͯ͠࢖༻ ͢Δ +BWB4&Ͱܕਪ࿦ͷ੍ݶ͕؇࿨͞Ε·͢ɻ͜ͷ ໰୊΋ίϯύΠϧΤϥʔʹͳΒͣʹ࣮ߦ͢Δ͜ͱ͕ Ͱ͖·͢ ղઆ Java SE 7: Who's Foo?
  52. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 52 import java.io.*; public class OutOfService { public static void main(String... args) { try (BufferedOutputStream bos = new BufferedOutputStream(System.out); OutputStreamWriter osw = new OutputStreamWriter(bos)) { osw.write("Hello"); } catch (IOException e) { System.out.print("Java"); } finally { System.out.print("World"); } } } Java SE 7: Ξ΢τɾΦϒɾαʔϏε 1) Helloɹ 2) HelloWorldɹ 3) HelloJavaWorldɹ 4) Կ΋ग़ྗ͞Εͳ͍
  53. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 53 ਖ਼ղ
  54. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 54 import java.io.*; public class OutOfService { public static void main(String... args) { try (BufferedOutputStream bos = new BufferedOutputStream(System.out); OutputStreamWriter osw = new OutputStreamWriter(bos)) { osw.write("Hello"); } catch (IOException e) { System.out.print("Java"); } finally { System.out.print("World"); } } } Java SE 7: Ξ΢τɾΦϒɾαʔϏε 1) Helloɹ 2) HelloWorldɹ 3) HelloJavaWorldɹ 4) Կ΋ग़ྗ͞Εͳ͍
  55. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 55 +BWB4&ΑΓɺUSZXJUISFTPVSDFT +-4  ͕ಋೖ͞ΕDMPTFΛ໌ࣔతʹݺͼग़͢ඞ ཁ͕ͳ͘ͳΓ·ͨ͠ ղઆ Java SE 7: Ξ΢τɾΦϒɾαʔϏε import java.io.*; public class OutOfService { public static void main(String... args) { try (BufferedOutputStream bos = new BufferedOutputStream(System.out); OutputStreamWriter osw = new OutputStreamWriter(bos)) { osw.write("Hello"); } catch (IOException e) { System.out.print("Java"); } finally { System.out.print("World"); } } }
  56. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 56 +BWB4&ҎલͰ͸࣍ͷΑ͏ʹDMPTF ͷεϩʔ͢ Δ*0&YDFQUJPOΛΩϟονͯ͠ॲཧ͕ඞཁͰͨ͠ ղઆ Java SE 7: Ξ΢τɾΦϒɾαʔϏε BufferedOutputStream bos = new BufferedOutputStream(System.out); OutputStreamWriter osw = null; try { osw = new OutputStreamWriter(bos); osw.write("Hello"); } catch (IOException e) { System.out.println("Java"); } finally { if (osw != null) { try { osw.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } else { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("World"); }
  57. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 57 ΈΜͳେ޷͖KBWBQDͰUSZXJUISFTPVSDFTͷ ॲཧΛݟͯΈ·͠ΐ͏ ղઆ Java SE 7: Ξ΢τɾΦϒɾαʔϏε % javap -c OutOfService Compiled from "OutOfService.java" public class OutOfService { public OutOfService(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String...); Code: 0: new #2 // class java/io/BufferedOutputStream 3: dup 4: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 7: invokespecial #4 // Method java/io/BufferedOutputStream."<init>":(Ljava/io/OutputStream;)V 10: astore_1 11: aconst_null 12: astore_2 13: new #5 // class java/io/OutputStreamWriter 16: dup 17: aload_1 18: invokespecial #6 // Method java/io/OutputStreamWriter."<init>":(Ljava/io/OutputStream;)V 21: astore_3 22: aconst_null 23: astore 4 25: aload_3 26: ldc #7 // String Hello 28: invokevirtual #8 // Method java/io/OutputStreamWriter.write:(Ljava/lang/String;)V 31: aload_3 32: ifnull 112 35: aload 4 37: ifnull 59 40: aload_3 41: invokevirtual #9 // Method java/io/OutputStreamWriter.close:()V 44: goto 112 47: astore 5 49: aload 4 51: aload 5 53: invokevirtual #11 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 56: goto 112 59: aload_3 60: invokevirtual #9 // Method java/io/OutputStreamWriter.close:()V 63: goto 112 66: astore 5 68: aload 5 70: astore 4 72: aload 5 74: athrow 75: astore 6 77: aload_3 78: ifnull 109 81: aload 4 83: ifnull 105 86: aload_3 87: invokevirtual #9 // Method java/io/OutputStreamWriter.close:()V 90: goto 109 93: astore 7 95: aload 4 97: aload 7 99: invokevirtual #11 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 102: goto 109 105: aload_3 106: invokevirtual #9 // Method java/io/OutputStreamWriter.close:()V 109: aload 6 111: athrow 112: aload_1 113: ifnull 183 116: aload_2 117: ifnull 136 120: aload_1 121: invokevirtual #12 // Method java/io/BufferedOutputStream.close:()V 124: goto 183 127: astore_3 128: aload_2 129: aload_3 130: invokevirtual #11 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 133: goto 183 136: aload_1 137: invokevirtual #12 // Method java/io/BufferedOutputStream.close:()V 140: goto 183 143: astore_3 144: aload_3 145: astore_2 146: aload_3 147: athrow 148: astore 8 150: aload_1 151: ifnull 180 154: aload_2 155: ifnull 176 158: aload_1 159: invokevirtual #12 // Method java/io/BufferedOutputStream.close:()V 162: goto 180 165: astore 9 167: aload_2 168: aload 9 170: invokevirtual #11 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 173: goto 180 176: aload_1 177: invokevirtual #12 // Method java/io/BufferedOutputStream.close:()V 180: aload 8 182: athrow 183: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 186: ldc #13 // String World 188: invokevirtual #14 // Method java/io/PrintStream.print:(Ljava/lang/String;)V 191: goto 227 194: astore_1 195: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 198: ldc #16 // String Java 200: invokevirtual #14 // Method java/io/PrintStream.print:(Ljava/lang/String;)V 203: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 206: ldc #13 // String World
  58. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 58 ݟ΍͘͢+%ͰٯίϯύΠϧͯ͠Έ·͠ΐ͏ ղઆ Java SE 7: Ξ΢τɾΦϒɾαʔϏε try { BufferedOutputStream localBufferedOutputStream = new BufferedOutputStream(System.out); Object localObject1 = null; try { OutputStreamWriter localOutputStreamWriter = new OutputStreamWriter(localBufferedOutputStream); Object localObject2 = null; try { localOutputStreamWriter.write("Hello"); } catch (Throwable localThrowable4) { localObject2 = localThrowable4; throw localThrowable4; } finally { } } catch (Throwable localThrowable2) { localObject1 = localThrowable2; throw localThrowable2; } finally { if (localBufferedOutputStream != null) { if (localObject1 != null) { try { localBufferedOutputStream.close(); } catch (Throwable localThrowable6) { localObject1.addSuppressed(localThrowable6); } } } else { localBufferedOutputStream.close(); } } } catch (IOException localIOException) { System.out.print("Java"); } finally { System.out.print("World"); } ࡞੒ͨ͠ϦιʔεΛDMPTF͢Δ Α͏ίϯύΠϥ͕ࣗಈతʹิ׬ ͯ͘͠Ε͍ͯΔͷ͕Θ͔Γ·͢
  59. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 59 ͯ͞ɺ0VUQVU4USFBNDMPTF͸ ग़ྗετϦʔϜΛด͡ ͜ͷετϦʔϜʹؔ࿈͢Δ͢΂ͯͷ γεςϜϦιʔεΛղ์͠·͢ ղઆ Java SE 7: Ξ΢τɾΦϒɾαʔϏε try (BufferedOutputStream bos = new BufferedOutputStream(System.out); OutputStreamWriter osw = new OutputStreamWriter(bos)) { osw.write("Hello"); // Hello͕όοϑΝ͞ΕΔ } catch (IOException e) { System.out.print("Java"); } finally { // osw͕try-with-resourcesʹΑΓcloseࡁΈ // → System.out΋ಉ࣌ʹϦιʔε͕ղ์͞Ε͍ͯΔ System.out.print("World"); // Ϧιʔε͕ղ์ࡁΈͷͨΊग़ྗ͞Εͳ͍ }
  60. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 60 ศརͰ҆શʹॻ͚ΔUSZXJUISFTPVSDFTߏจΛ ࢖͍·͠ΐ͏ ؒҧͬͯ4ZTUFNPVUΛΫϩʔζ͠ͳ͍Α͏ʹ ͝஫ҙ Java SE 7: Ξ΢τɾΦϒɾαʔϏε ڭ܇
  61. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 61 www.java-users.jp
  62. Japan Java User Group (JJUG) Presents - Java Puzzlers, Java

    Day Tokyo 2013. © JJUG 62 List<String> msgs = Arrays.asList( "Thank you!", "by Japan Java User Group" ); msgs.forEach(System.out::println);