O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

It's a trap - java pitfalls

33 visualizações

Publicada em

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.

Publicada em: Tecnologia
  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

It's a trap - java pitfalls

  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. Where is my uppercase? •No uppercase for some ligatures: ß SS, ffl FFL •No uppercase for some precomposed characters: ΐ (1 character) Ϊ́ (3 characters) "ß".toUpperCase() == "SS"; "dzwon".length() == 4;
  8. 8. 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]
  9. 9. Source: DailyHaha
  10. 10. 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;
  11. 11. 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
  12. 12. 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
  13. 13. Source: java-gaming.org
  14. 14. 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();
  15. 15. 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); } }
  16. 16. Abnormal finally block completion public static void main(String[] args) { System.out.println(foo()); } private static boolean foo() { try { return false; } finally { return true; } }
  17. 17. Exceptional exceptions public static <T extends Throwable> void sneakyThrow(Throwable t) throws T { throw (T) t; } public static void main(String[] args) { sneakyThrow(new IOException()); }
  18. 18. Surprising escapes /* String specialChars = "!@#$%*/()"; */ String x = "au0022; String x = "a"; //TODO add u000A support
  19. 19. 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; }
  20. 20. 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
  21. 21. THANKS! ANY QUESTIONS? karol.wrotniak@droidsonroids.pl koral-- karol-wrotniak @karol.wrotniak Karol Wrótniak Android Dev @Droids On Roids Co-organizer @GDG Wrocław

×