creative logical thinking

Problem Solving Deja Vu

I’ve spent a lifetime programming in Java, C, C++ and a plethora of languages, for programs ranging from small, in-house tools to giant, multinational enterprise systems, and  although I’m marketing my current product, I miss the days when I’d  see a programming challenge and simply couldn’t resist the urge to solve it.

(That itch still strikes me, though I’m hard-pressed to find time to scratch it.)

Everywhere that I’ve worked, I’ve always looked to hire people who had the same love of problem solving – people who, upon seeing a problem that required logical, creative and tenacious skills, just wanted to solve it. The problem didn’t have to be technical or programming – it could be a Sudoku or Mensa challenge, a crossword or IQ puzzle or game requiring creative thinking, such as metal disentanglement and sliding block puzzles.

In the same vein, here are some weird, funny and cool things that can be done in Java. Can you figure out how they work?

Java Data Types

int i = (byte) + (char) - (int) + (long) - 1;



Java Random Numbers

Random random = new Random(-6732303926L);
 for(int i=0;i<10;i++)
 System.out.println(random.nextInt(10)+" ");


0 1 2 3 4 5 6 7 8 9

More Java Random Numbers

Random random = new Random(441287210);
 for(int i=0;i<10;i++)
 System.out.print(random.nextInt(10)+" ");


1 1 1 1 1 1 1 1 1 1

Java Hello World

public static void main(String ... args) {
 System.out.println(randomString(-229985452)+' '+randomString(-147909649));

public static String randomString(int seed) {
 Random rand = new Random(seed);
 StringBuilder sb = new StringBuilder();
 for(int i=0;;i++) {
 int n = rand.nextInt(27);
 if (n == 0) break;
 sb.append((char) ('`' + n));
 return sb.toString();


hello world

More Java Hello World

public static void main(String... args) {
 System.out.println("Hello World");

 static {
 try {
 Field value = String.class.getDeclaredField("value");
 value.set("Hello World", value.get("Namaskar."));
 } catch (Exception e) {
 throw new AssertionError(e);



Another cool way of doing things in Java are with regular expressions. For example, how would you check if a string is an IPV4 or IPV6 compliant IP address? The traditional method (that most interviewed candidates have given me) involves one or more loops, string/character comparison and if statements – all of which bloat the code and render it practically useless.

But with regular expressions, here’s the code:

private static final Pattern IPV4_PATTERN =

 private static final Pattern IPV6_STD_PATTERN =

 private static final Pattern IPV6_HEX_COMPRESSED_PATTERN =

 public static boolean isIPv4Address(final String input) {
 return IPV4_PATTERN.matcher(input).matches();

 public static boolean isIPv6StdAddress(final String input) {
 return IPV6_STD_PATTERN.matcher(input).matches();

 public static boolean isIPv6HexCompressedAddress(final String input) {
 return IPV6_HEX_COMPRESSED_PATTERN.matcher(input).matches();

 public static boolean isIPv6Address(final String input) {
 return isIPv6StdAddress(input) || isIPv6HexCompressedAddress(input);

See the improvement? This code runs much faster, is much sleeker and much more efficient.

One comment

Whaddya think? Leave a Reply