Java Pitfalls - University of Arizona

Download Report

Transcript Java Pitfalls - University of Arizona

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
1




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
2
Oddity
// Does this method return true if n is odd?
public boolean isOdd(int n) {
return n % 2 == 1;
}
3
Ö

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
4
Simple Arithmetic
@Test
public void simpleArithmetic() {
// Does this assertion pass?
assertEquals(444, 123 + 32l);
}
5
Ö



Eyes Deceive
It is 123 + 32L, which is 155
Use L instead of l for Long
6
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.
}
7
Ö

Answer

Only a.
java.lang.AssertionError:
expected:<Ha> but was:<169>
8
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);
}
9
Ö


Neither
Precedence rules have + evaluating before
==. The actual value is
"Animals equal: " + pig == dog
"Animals equal: pig" == dog
10
To intern or not to intern
@Test
public void
Integer a
Integer b
Integer c
Integer d
Integer e
Integer f
testIntegerInterns() {
= -128;
= -128;
= 127;
= 127;
= 345;
= 345;
// Which, if
assertTrue(a
assertTrue(c
assertTrue(e
assertTrue(e
}
any, of these assertions fail?
== b); // a.
== d); // b.
>= f); // c.
== f); // d.
11
Ö

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.

12
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.
}
13
Ö
assertTrue(a == b); // d.


Does not pass
The == compares reference values, not
the numeric values
Changing Double to double does what?
14
From a 227 Student
// Part of escape obstacle course in findExit
if(escape = false)
findExit(r-1, c);
if(escape = false)
findExit(r, c+1);

// row above
// col to the right
Can this recursive solution ever work?
15
Ö

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
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
17
Ö

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 €
18
assertEquals('@', '\u0040');
assertEquals('A', '\u0041');
assertEquals('B', '\u0042');
assertEquals('`', '\u0060');
assertEquals('a', '\u0061');
assertEquals('b', '\u0062');
assertEquals('€', '\u20ac');
19
Is true true?
@Test
public void trueOr() {
// Does this assertion pass?
assertEquals("Compare 5 to 4", "true", 5 > 4);
}
20
Ö

Answer



No
"true" is not true
JUnit show this:
java.lang.AssertionError:
Compare 5 to 4 expected:<true> but was:<true>
21
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");
}
}
22
Ö
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;
}
23
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);
}
24
Ö

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++;
25
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]);
//? _____
26
O
0
1
1
With array
0 5
1 4
27
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);
}
}
28
Ö

Answer: Yes
Hello world 0 null

This is an initializer, a method with no
heading;

{ }
29
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);
}
30
Ö



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);
31
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());
}
32
Ö

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);
33
Output
int n = 0;
try {
n = n / 0;
} catch (Exception e) {
System.out.println("A");
} finally {
System.out.println("B");
}
System.out.println("C");
34
Ö
A
B
C

Finally blocks always execute unless
System.exit(0) is encountered first
35
Output
int n = 0;
try {
n = n / 999;
} catch (Exception e) {
System.out.println("A");
} finally {
System.out.println("B");
}
System.out.println("C");
36
Ö
B
C
37
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");
38
Ö
B
C
39
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");
40
Ö
A
41