Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

It's a trap

114 vues

Publié le

There are many traps and pitfalls in Java. Things which looks obvious at a glance may be tricky. Take a look how is it possible that 4+1=15 or how to read from non-existent file.
Slides from 119. WroclawJUG

Publié dans : Logiciels
  • Soyez le premier à commenter

  • Soyez le premier à aimer ceci

It's a trap

  1. 1. It’s a trap Karol Wrótniak
  2. 2. Ramazan wanted to write "You change the topic every time you!run out of arguments" (sounds familiar enough) but what Emine read was, "You change the topic every time!they are fucking you" (sounds familiar too.) Source: Jesus Diaz, A Cellphone's Missing Dot Kills Two People, Puts Three More in Jail (2008) Retrieved from: https://gizmodo.com/382026/a-cellphones-missing-dot-kills-two-people-puts-three-more-in-jail
  3. 3. When using! LOWER_CASE_WITH_DASHES!or! LOWER_CASE_WITH_UNDERSCORES! as FieldNamingPolicy it could happen that certain fields won't get serialized/ deserialized. I've seen that behavior on devices with a turkish (tr-TR) locale.! Source: Sebastian Clan, Fix issues if runing in an environment with a Turkish locale (2015) Retrieved from: https://github.com/google/gson/pull/652/files
  4. 4. Dotted and dotless I Lowercase Uppercase I ! I i i ı English Turkish Turkish dotted dotless
  5. 5. Casing done right • Default (or specific) locale for UI • ROOT locale for machine to machine communication like JSON keys or HTTP headers value.toUpperCase(Locale.ROOT);
  6. 6. Casing depends on context • Dotted and dotless I in Turkish: !i, Iı • Final and non-final small Greek sigmas with single capital letter: ", #, " • Retained dot in a lowercase i and j when followed by accents in Lithuanian: i #$ Unicode casing: https://unicode.org/Public/UNIDATA/ SpecialCasing.txt
  7. 7. Collator String[] words = {"!ma", "kres", "czas", "chyba"}; Arrays.sort(words); System.out.println(Arrays.toString(words)); //[chyba, czas, kres, !ma] Arrays.sort(words, Collator.getInstance()); • Collator PL: [chyba, czas, %ma, kres] • Collator CS: [%ma, czas, chyba, kres] • Hungarian SZ+SZ=SSZ • [kaszinó, kassza]
  8. 8. Source: DailyHaha
  9. 9. Number pitfalls x == -x && x != 0 int x = Integer.MIN_VALUE; x != x float x = Float.NaN; x = x + y; x += y; short x = 1; int y = 2;
  10. 10. The missing days Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime()); //Sat Oct 06 12:25:24 CEST 2018 calendar.add(YEAR, -436); System.out.println(calendar.getTime()); //Sat Oct 16 12:25:24 CET 1582
  11. 11. 1-1 = ? Calendar calendar = Calendar.getInstance(); calendar.set(YEAR, 1); System.out.println(calendar.get(YEAR)); //1 calendar.add(YEAR, -1); System.out.println(calendar.get(YEAR)); //1
  12. 12. Source: java-gaming.org
  13. 13. java.io.File internals file.exists() == true; file.isDirectory() == false; file.isFile() == false; File file = new File("/dev/null"); InputStream stream = new FileInputStream(file); file.delete(); stream.read();
  14. 14. Initialization order abstract class Parent { Parent() { log(); } abstract void log(); } class Child extends Parent { final String foo; Child() { foo = "bar"; } @Override protected void log() { System.out.println(foo); } }
  15. 15. Abnormal finally block completion public static void main(String[] args) { System.out.println(foo()); } private static boolean foo() { try { return false; } finally { return true; } }
  16. 16. Surprising escapes /* String specialChars = "!@#$%*/()"; */ String x = "au0022; String x = "a"; //TODO add u000A support
  17. 17. Volatile private static boolean isReady; public static void main(String[] args) throws Exception { new Thread(() -> { while (!isReady); System.out.println("Ready!"); }).start(); Thread.sleep(1000); isReady = true; }
  18. 18. REFERENCES • Java Puzzlers • Edge Cases to Keep in Mind. Part 1 – Text • Edge Cases to Keep in Mind. Part 2 – Files • Unicode casing • Java Magic. Part 4: sun.misc.Unsafe

×