Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch and Neal Gafter Publisher:

Similar presentations


Presentation on theme: "1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch and Neal Gafter Publisher:"— Presentation transcript:

1 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch and Neal Gafter Publisher: Addison Wesley Professional Date: 24 June 2005

2 2 Every programming language has its quirks. Are you a code sleuth? Have you ever spent days chasing a bug caused by a trap or pitfall in Java or its libraries? Here are some diabolical puzzles

3 3 Oddity // Does this method return true if n is odd? public boolean isOdd(int n) { return n % 2 == 1; }

4 4 Ö Yes, but it's wrong when n is negative // Try this return n % 2 != 0; // or bitmask with and operation '&' (faster) return (n & 1) == 1; // n & 1 ands the rightmost bit with 1 // if n is 25, n & 1 is 1 // 00011001 & 00000001 is 00000001 // Expression is 0 unless n is odd // if n is 24, n & 1 is 0 // 00011000 & 00000001 is 00000000

5 5 Simple Arithmetic @Test public void simpleArithmetic() { // Does this assertion pass? assertEquals(444, 123 + 32l); }

6 6 Ö Eyes Deceive It is 123 + 32L, which is 155 Use L instead of l for Long

7 7 The laughs are on me @Test public void simpleChars() { // Which, if any, of these two assertions pass? assertEquals("Ha", "H" + "a"); // a. assertEquals("Ha", 'H' + 'a'); // b. }

8 8 Ö Answer Only a. java.lang.AssertionError: expected: but was:

9 9 String constants interned @Test public void stringIntern() { String pig = "length: 10"; String dog = "length: " + pig.length(); // Which, if any, of these two assertions pass? assertEquals("Animals equal: false", "Animals equal: " + pig == dog); assertEquals("Animals equal: true", "Animals equal: " + pig == dog); }

10 10 Ö Neither Precedence rules have + evaluating before ==. The actual value is Precedence rules "Animals equal: " + pig == dog "Animals equal: pig" == dog

11 11 To intern or not to intern @Test public void testIntegerInterns() { Integer a = -128; Integer b = -128; Integer c = 127; Integer d = 127; Integer e = 345; Integer f = 345; // Which, if any, of these assertions fail? assertTrue(a == b); // a. assertTrue(c == d); // b. assertTrue(e >= f); // c. assertTrue(e == f); // d. }

12 12 Ö d only == compares reference values so you would think all 3 fail, but.... java.sun.com/docs/books/jls/download/langspe c-3.0.pdf explicitly states that wrappers for values in the range -128 to 127 will be interned by any JVM.

13 13 Are Doubles interned? @Test public void testEqualEquals() { Double a = 4.2; Double b = 4.2; // Which, if any, of these assertions fail? assertTrue(a >= b); // a. assertTrue(a <= b); // b. assertTrue(a.equals(b)); // c. assertTrue(a == b); // d. assertTrue(a.compareTo(b) == 0); // e. assertTrue(a.compareTo(4.2) == 0); // f. }

14 14 Ö assertTrue(a == b); // d. Does not pass The == compares reference values, not the numeric values Changing Double to double does what?

15 From a 227 Student // Part of escape obstacle course in findExit if(escape = false) findExit(r-1, c); // row above if(escape = false) findExit(r, c+1); // col to the right Can this recursive solution ever work? 15

16 Ö No The boolean expressions are ALWAYS false An assignment statement evaluates to the value of the right value (expression to the right of =) What is the value of booleanVar = true 16

17 17 Output from this program? \u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020\u0020 \u0063\u006c\u0061\u0073\u0073\u0020\u0055\u0067\u006c\u0079 \u007b\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020 \u0020\u0020\u0020\u0020\u0073\u0074\u0061\u0074\u0069\u0063 \u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028 \u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u0020\u0020 \u0020\u0020\u0020\u0020\u0061\u0072\u0067\u0073\u0029\u007b \u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074 \u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0020 \u0022\u0048\u0065\u006c\u006c\u006f\u0020\u0077\u0022\u002b \u0022\u006f\u0072\u006c\u0064\u0022\u0029\u003b\u007d\u007d

18 18 Ö Answer: Hello World \u0070 in hexadecimal is 112 in decimal or the character 'c' Unicode not very readable Suggest: avoid Unicode unless you need it System.out.println(123.45 + " \u20ac" ); 123.45 €

19 19 assertEquals('@', '\u0040'); assertEquals('A', '\u0041'); assertEquals('B', '\u0042'); assertEquals('`', '\u0060'); assertEquals('a', '\u0061'); assertEquals('b', '\u0062'); assertEquals('€', '\u20ac');

20 20 Is true true? @Test public void trueOr() { // Does this assertion pass? assertEquals("Compare 5 to 4", "true", 5 > 4); }

21 21 Ö Answer No "true" is not true JUnit show this: java.lang.AssertionError: Compare 5 to 4 expected: but was:

22 22 Output? @Test public void simpleChars() { int choice = 2; switch (choice) { case 1: System.out.println("one"); case 2: System.out.println("two"); case 3: System.out.println("three"); }

23 23 Ö two three Add breaks int choice = 2; switch (choice) { case 1: System.out.println("one"); break; case 2: System.out.println("two"); break; case 3: System.out.println("three"); break; }

24 24 Java plus plus @Test public void simpleChars() { int j = 0; for (int i = 0; i < 10; i++) j = j++; // Does this assertion pass? assertEquals(10, j); }

25 25 Ö Answer: No j = j++; is postfix increment operator j is 0 after the loop When you use a postfix operator as part of a larger expression, the expression's value is returned before the postfix operator is processed Use j++;

26 26 Output? int j = 0; int k = 0; System.out.println(j++); //? _____ System.out.println(++k); //? _____ System.out.println(j); //? _____ int[] x = { 5, 4, 3 }; int i = 0; System.out.println(i + " " + x[i++]); //? _____ System.out.println(i + " " + x[i]); //? _____

27 O 0 1 With array 0 5 1 4 27

28 28 Is there any Output? public class Huh { public static void main(String[] args) { new B(); } public class B { int j; String s; { System.out.println("Hello world " + j + " " + s); }

29 29 Ö Answer: Yes Hello world 0 null This is an initializer, a method with no heading; { }

30 30 Add to 0 three times @Test public void testBigInt() { BigInteger five = new BigInteger("5"); BigInteger fifty = new BigInteger("50"); BigInteger fiveHundred = new BigInteger("500"); BigInteger total = BigInteger.ZERO; total.add(five); total.add(fifty); total.add(fiveHundred); // Does this assertion pass? assertEquals(555, total); }

31 31 Ö No BigInteger, like String is immutable This will pass BigInteger total = BigInteger.ZERO; total = total.add(five); total = total.add(fifty); total = total.add(fiveHundred); // Does this assertion pass assertEquals(555, total);

32 32 No Warning @Test public void testHashMap() { HashMap hm = new HashMap (); hm.put("a", new BigInteger("123456")); hm.put("b", new BigInteger("1234567")); hm.put("c", new BigInteger("1234567")); hm.put("a", new BigInteger("654321")); BigInteger aBigInt = hm.get("a"); // Does this assertion pass? assertEquals(123456, aBigInt.intValue()); }

33 33 Ö No, the first mapping was destroyed // Return old value if the key exists // return null if there was no mapping to the key hm.put("a", new BigInteger("123456")); BigInteger bi = hm.put("a", new BigInteger("9999")); int actual = bi.intValue(); assertFalse(9999 == actual); assertTrue(123456 == actual); // Return null if there was no mapping with the key BigInteger bi2 = hm.put("key", new BigInteger("123")); assertNull(bi2);

34 34 Output int n = 0; try { n = n / 0; } catch (Exception e) { System.out.println("A"); } finally { System.out.println("B"); } System.out.println("C");

35 35 Ö A B C Finally blocks always execute unless System.exit(0) is encountered first

36 36 Output int n = 0; try { n = n / 999; } catch (Exception e) { System.out.println("A"); } finally { System.out.println("B"); } System.out.println("C");

37 37 Ö BCBC

38 38 Output int n = 0; try { n = n / 999; } catch (Exception e) { System.out.println("A"); System.exit(0); } finally { System.out.println("B"); } System.out.println("C");

39 39 Ö BCBC

40 40 Output int n = 0; try { n = n / 0; } catch (Exception e) { System.out.println("A"); System.exit(0); } finally { System.out.println("B"); } System.out.println("C");

41 41 Ö A


Download ppt "1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch and Neal Gafter Publisher:"

Similar presentations


Ads by Google