Recent Posts
Archives

Posts Tagged ‘DevoxxFR2013’

PostHeaderIcon [DevoxxFR2013] Strange Loops: A Mind-Bending Journey Through Java’s Hidden Curiosities

Lecturers

Guillaume Tardif has been crafting software since 1998, primarily in the Java and JEE ecosystem. His roles span technical leadership, agile coaching, and architecture across consulting firms and startups. Now an independent consultant, he has presented at Agile Conference 2009, XP Days 2009, and Devoxx France 2012, blending technical depth with philosophical inquiry.

Eric Lefevre-Ardant began programming in Java in 1996. His career alternates between Java consultancies and startups, currently as an independent consultant. Together, they explore the boundaries of code, inspired by Douglas Hofstadter’s Gödel, Escher, Bach.

Abstract

Guillaume Tardif and Eric Lefevre-Ardant invite you on a disorienting, delightful promenade through the strangest corners of the Java language — a journey inspired by Douglas Hofstadter’s exploration of self-reference, recursion, and emergent complexity. Through live-coded puzzles, optical illusions in syntax, and meta-programming mind-benders, they reveal how innocent-looking code can loop infinitely, reflect upon itself, or even generate its own source. The talk escalates from simple for loop quirks to genetic programming, culminating in a real-world example of self-replicating machines: the RepRap 3D printer. This is not a tutorial — it is a meditation on the nature of code, computation, and creation.

The Hofstadter Inspiration

Douglas Hofstadter’s Gödel, Escher, Bach explores strange loops — hierarchical systems that refer to themselves, creating emergent meaning. The presenters apply this lens to Java: a language designed for clarity, yet capable of profound self-referential trickery. They begin with a simple puzzle:

for (int i = 0; i < 10; i++) {
  System.out.println(i);
  i--;
}

What does it print? The answer — an infinite loop — reveals how loop variables can be manipulated in ways that defy intuition. This sets the tone: code is not just logic; it is perception.

Syntactic Illusions and Parser Tricks

The duo demonstrates Java constructs that appear valid but behave unexpectedly due to parser ambiguities. Consider:

label: for (int i = 0; i < 5; i++) {
  if (i == 3) break label;
  System.out.println(i);
}

The label: seems redundant — until combined with nested loops and continue label to skip outer iterations. They show how the most vexing parse confuses even experienced developers:

new Foo(new Bar());
// vs
new Foo(new Bar()); // same?

Subtle whitespace and operator precedence create optical illusions in code readability.

Reflection and Meta-Programming

Java’s reflection API enables programs to inspect and modify themselves at runtime. The presenters write a method that prints its own source code — a quines-like construct:

public static void printSource() throws Exception {
  String path = Quine.class.getProtectionDomain().getCodeSource().getLocation().getPath();
  Files.lines(Paths.get(path)).forEach(System.out::println);
}

They escalate to bytecode manipulation with Javassist, generating classes dynamically. This leads to a discussion of genetic programming: modeling source code as a tree, applying mutations and crossovers, and evolving solutions. While more natural in Lisp, Java implementations exist using AST parsing and code generation.

The Ultimate Strange Loop: Self-Replicating Machines

The talk culminates with the RepRap project — an open-source 3D printer designed to print its own parts. Begun in 2005, RepRap achieved partial self-replication by 2008, printing about 50% of its components. The presenters display a physical model, explaining how the printer’s design files, firmware, and mechanical parts form a closed loop of creation.

They draw parallels to John von Neumann’s self-replicating machines and Conway’s Game of Life — systems where simple rules generate infinite complexity. In Java terms, this is the ultimate quine: a program that outputs a machine that runs the program.

Philosophical Implications

What does it mean for code to reflect, replicate, or evolve? The presenters argue that programming is not just engineering — it is art, philosophy, and exploration. Strange loops remind us that:

  • Clarity can mask complexity
  • Simplicity can generate infinity
  • Code can transcend its creator

They close with a call to embrace curiosity: write a quine, mutate an AST, print a 3D part. The joy of programming lies not in solving known problems, but in discovering new ones.

Links

Hashtags: #StrangeLoops #JavaPuzzlers #SelfReference #GeneticProgramming #RepRap #GuillaumeTardif #EricLefevreArdant