Java
Puzzlers
Technical Presentation, 2014
Mikaël Donikian
Puzzler?

© 2013 Mikaël DONIKIAN
Read /ˈpʌzləʳ/ (src : wordreference.com)

“a problem that cannot be
easily or readily solved”
© 2013 Mikaël DONIKIAN
Who’s behind?

This was first introduced by the designer of the
Collections API, Joshua Bloch, and his coworker
at SUN, Neal Gafter.
Java Puzzlers: Traps, Pitfalls, and Corner Cases, Addison-Wesley
Professional (July 4, 2005)

© 2013 Mikaël DONIKIAN
The Byte Iterator

public class ABiteOfByte
{
public static void main(String... args)
{
for (byte a = Byte.MIN_VALUE; a <= Byte.MAX_VALUE; a++)
{
System.out.println("a= " + a);
}
}
© 2013 Mikaël DONIKIAN
}
The Byte Iterator : Solution
Once “a” has reached the Byte.MAX_VALUE, “a++”
operation returns “a” with the Byte.MIN_VALUE.
We then get an infinite loop.

© 2013 Mikaël DONIKIAN
Odd or Even?
public static void main(String[] args)
{
String a = null;
int n = "number".hashCode();
switch( n % 2 )
{
case 0: a = "even"; break;
case 1: a = "odd"; break;
}
}

System.out.println( a );
© 2013 Mikaël DONIKIAN
Odd or Even? : Solution
The result is null. Because in this particular case n is negative and
n % 2 will then be negative and will never be caught by any of the
two cases.
A solution would be to apply modulus on the absolute value of n.

© 2013 Mikaël DONIKIAN
Vector of Integer
public static void main(String[] args)
{
Vector<Integer> numbers = new Vector<Integer>();
int min = 128;
int max = 255;
for( int i = min; i <= max; ++i )
{
numbers.add(i);
}
int element = 100;
numbers.remove(element);
element = 255;

}

if( numbers.contains(element) )
{
numbers.remove(element);
}
© 2013 Mikaël DONIKIAN
Vector of Integer: Solution
The previous code is crashing on the last instruction (IndexOutOfBoundException).
In a vector, the method « remove » is overloaded, it takes an « int » to remove the
element at the index represented by the « int » value, and takes an « object » to
remove the particular object within the vector.
In the case of the Integer, if we don’t define the value as an Integer, the remove
method in that exemple wants to remove the object at index 255.

© 2013 Mikaël DONIKIAN
The Match Game
public class Match
{
public static void main(String[] args)
{
Pattern p = Pattern.compile("(aa|aab?)+");
int count = 0;
for(String s = ""; s.length() < 200; s += "a")
if (p.matcher(s).matches())
count++;
System.out.println(count);
}
}

© 2013 Mikaël DONIKIAN
The Match Game: Solution
Pattern p = Pattern.compile("(aa|aab?)+");

the « ? » provokes a catastrophic backtracking that goes exponential with
increasing the length of the string analyzed.

Make absolutely sure that there is only one way to match the same match.
In that case remove the “?”. It’s because you can that you should, this is a
common regex development mistake.

© 2013 Mikaël DONIKIAN
Want some more?
Look at Quines, (introduced by
Douglas Hofstadter in the
honor of W. Van Orman Quine
who studied self-referencing)

© 2013 Mikaël DONIKIAN
References
1.

http://www.programmez.com/magazine_articles.php?titre=Java-Puzzlers--decouvrez-les-pieges-etcuriosites-du-langage-Java-!&id_article=1777&magazine=156

2.

https://github.com/elefevre/strange-loops/blob/master/puzzlers/src/ABiteOfByte.java

3.

http://en.wikipedia.org/wiki/Quine_(computing)

4.

http://www.billthelizard.com/2009/08/solutions-to-two-more-java-puzzlers.html

5.

http://strangeloop2010.com/system/talks/presentations/000/014/450/BlochLee-JavaPuzzlers.pdf

6.

http://blog.ninja-squad.com/2012/10/12/java-puzzler/

7.

http://www.codinghorror.com/blog/2006/01/regex-performance.html

© 2013 Mikaël DONIKIAN

Java Puzzlers

Editor's Notes

  • #6 Solution suggested by the picture