java gotcha's - arizona computer science · 1 java gotcha's most of these examples are...
TRANSCRIPT
![Page 1: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/1.jpg)
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
![Page 2: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/2.jpg)
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
![Page 3: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/3.jpg)
3
Oddity
// Does this method return true if n is odd?
public boolean isOdd(int n) {
return n % 2 == 1;
}
![Page 4: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/4.jpg)
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
![Page 5: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/5.jpg)
5
Simple Arithmetic
@Test
public void simpleArithmetic() {
// Does this assertion pass?
assertEquals(444, 123 + 32l);
}
![Page 6: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/6.jpg)
6
Ö
Eyes Deceive
It is 123 + 32L, which is 155
Use L instead of l for Long
![Page 7: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/7.jpg)
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.
}
![Page 8: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/8.jpg)
8
Ö
Answer
Only a.
java.lang.AssertionError:
expected:<Ha> but was:<169>
![Page 9: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/9.jpg)
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);
}
![Page 10: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/10.jpg)
10
Ö
Neither
Precedence rules have + evaluating before ==. The actual value is"Animals equal: " + pig == dog
"Animals equal: pig" == dog
![Page 11: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/11.jpg)
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.
}
![Page 12: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/12.jpg)
12
Ö
d only
== compares reference values so you would think all 3 fail, but....
java.sun.com/docs/books/jls/download/langspec-3.0.pdf explicitly states that wrappers for values in the range -128 to 127 will be interned by any JVM.
![Page 13: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/13.jpg)
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.
}
![Page 14: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/14.jpg)
14
Ö
assertTrue(a == b); // d. Does not pass
The == compares reference values, not the numeric values
Changing Double to double does what?
![Page 15: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/15.jpg)
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
![Page 16: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/16.jpg)
Ö
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
![Page 17: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/17.jpg)
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
![Page 18: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/18.jpg)
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 €
![Page 19: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/19.jpg)
19
assertEquals('@', '\u0040');
assertEquals('A', '\u0041');
assertEquals('B', '\u0042');
assertEquals('`', '\u0060');
assertEquals('a', '\u0061');
assertEquals('b', '\u0062');
assertEquals('€', '\u20ac');
![Page 20: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/20.jpg)
20
Is true true?
@Test
public void trueOr() {
// Does this assertion pass?
assertEquals("Compare 5 to 4", "true", 5 > 4);
}
![Page 21: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/21.jpg)
21
Ö
Answer
No
"true" is not true
JUnit show this:
java.lang.AssertionError:
Compare 5 to 4 expected:<true> but was:<true>
![Page 22: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/22.jpg)
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");
}
}
![Page 23: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/23.jpg)
23
Ö
two
three
Add breaksint 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;
}
![Page 24: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/24.jpg)
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);
}
![Page 25: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/25.jpg)
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++;
![Page 26: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/26.jpg)
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]); //? _____
![Page 27: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/27.jpg)
O
0
1
1
With array
0 5
1 4
27
![Page 28: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/28.jpg)
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);
}
}
![Page 29: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/29.jpg)
29
Ö
Answer: YesHello world 0 null
This is an initializer, a method with no heading; { }
![Page 30: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/30.jpg)
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);
}
![Page 31: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/31.jpg)
31
Ö
No
BigInteger, like String is immutable
This will passBigInteger total = BigInteger.ZERO;
total = total.add(five);
total = total.add(fifty);
total = total.add(fiveHundred);
// Does this assertion pass
assertEquals(555, total);
![Page 32: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/32.jpg)
32
No Warning
@Test
public void testHashMap() {
HashMap<String, BigInteger> hm =
new HashMap<String, BigInteger>();
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());
}
![Page 33: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/33.jpg)
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);
![Page 34: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/34.jpg)
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");
![Page 35: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/35.jpg)
35
Ö
A
B
C
Finally blocks always execute unless System.exit(0) is encountered first
![Page 36: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/36.jpg)
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");
![Page 37: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/37.jpg)
37
Ö
B
C
![Page 38: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/38.jpg)
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");
![Page 39: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/39.jpg)
39
Ö
B
C
![Page 40: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/40.jpg)
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");
![Page 41: Java Gotcha's - Arizona Computer Science · 1 Java Gotcha's Most of these examples are from this book: Java™ Puzzlers: Traps, Pitfalls, and Corner Cases Author(s): Joshua Bloch](https://reader031.vdocuments.us/reader031/viewer/2022022508/5acf00597f8b9a8b1e8c2c11/html5/thumbnails/41.jpg)
41
Ö
A