Showing posts with label puzzler. Show all posts
Showing posts with label puzzler. Show all posts

Monday, August 27, 2007

Java Puzzlers, Episode 6

I am a big fan of Java Puzzlers.

Another part of the Java Puzzler series appeared on Google Video couple days ago. This is one named Advanced Topics in Programming Languages: Java Puzzlers, Episode VI and is a repeat of a talk given at Google in May and at JavaOne 2007.

Josh Bloch and special guest star Bill Pugh present yet another installment in the continuing saga of Java Puzzlers, consisting of eight more programming puzzles for your entertainment and enlightenment.

I really enjoyed watching it and also learnt a thing or two.

Funny thing was when Bill Pugh said in the conclusion:

Use FindBugs: it finds all 8 bugs in this talk!

I use FindBugs everyday and I have to tell you, our codebase (JIRA) is quite clean and FindBugs helps us to keep it that way.

Alright! Enough said. Go on! Watch it!

Related posts: Java Puzzlers

Tuesday, August 22, 2006

Tricky instanceof operator

Let's start with a little puzzle.

Object obj = ...
System.out.println(obj instanceof Object);

How do you initialize the obj in order to print "false"?

Well, aren't all possible Java objects instances of Object? To answer this question one must understand how instanceof operator works. The answer is that it would print "true" for any Object. The only way to make it false is not to give it an object, give it a null reference.

The tricky bit about instanceof operator can be that if object on the left side is null, the condition evaluates to false. Therefore there is no need for null check after a class cast as in the following example.

public boolean equals(Object o) {
if (o instanceof MyClass) {
MyClass mc = (MyClass) o;
if (mc == null) // never null
return false;
else
return ... // compare members of mc
}
return false;
}

This can be simplified as shown in the following code snippet

public boolean equals(Object o) {
if (o instanceof MyClass) {
MyClass mc = (MyClass) o;
return ... // compare members of mc
}
return false;
}

So the moral if this excercise is that instanceof operator works as you would expect with objects, and returns false when given a null.

And remember that equals() method is probably the only reasonable place for instanceof operator. If you do it elsewhere and use it to create an alternative flow (e.g. if-else, switch) it is a bad smell and should be replaced with polymorphism. Read more about Swich Statement code smell and Polymorphism

BTW the previous example was not a very nice example of how to implement equals method, so do not copy it ;-) Usually we would do something like this

public boolean equals(Object o) {
if (o == null) return false;
if (o == this) return true;
if (!(o instanceof MyClass)) return false;
MyClass mc = (MyClass) o;
return ... // compare members of mc
}

Sunday, March 19, 2006

Java Puzzlers

I have been working with Java since 1997 and after all that time I thought I new Java. I also managed to earn few Java certifications. So I was quite confident I could cut good code that would run as expected.

That was until I read Java Puzzlers written by Joshua Bloch and Neal Gafter. Boy, I was wrong! There were so many examples of Java code that I expected to produce different results. I strongly recommend reading this book. It will give you an insight into the core of Java classes, objects, primitive types, libraries, and more...

Contents:

  • Introduction
  • Expressive Puzzlers
  • Puzzlers with Character
  • Loopy Puzzlers
  • Exceptional Puzzlers
  • Classy Puzzlers
  • Library Puzzlers
  • Classier Puzzlers
  • More Library Puzzlers
  • Advanced Puzzlers
  • Catalog of Traps and Pitfalls
  • Notes on the Illusions
  • References
  • Index

Serious geek fun!

Moreover I recently visited Javalobby website and found an interactive Yet More Java Puzzlers talk slideshow that illustrates eight more Java puzzlers that were presented on JavaPolis 2005 conference.

If you don't have the book yet, have a look at the free Sample Chapter
or Yet More Java Puzzlers for a hint of Java flavour. It's goof fun, plus you learn something from it.


Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.