During the presentation by William Pugh at JavaOne, he mentioned this interesting puzzle
public class Puzzle {
public static void main(String[] args) {
Set s = new HashSet();
for(short i = 0; i < 100; i++) {
s.add(i);
s.remove(i-1);
}
System.out.println("Size of set = " + s.size());
}
}
Now the question is, what will the above program print out as size of set? Options are
- 1
- 100
- Throws Exception
- None of above
My answer at first was option (1) as we are adding and removing everything except 99. But as this is a puzzle the answer should be different. Here goes the explanation ….
The answer is option (2). The reason is that the statement s.remove(i-1) is operating on integer. This might look strange but in Java 1 is int and even if i is short (i-1) is int. And due to autoboxing (i-1) becomes Integer object. Even though set s takes Short as the type when declared, remove method on an object of Set class takes in an Object and not the generic type. So the result is we are removing an element which isn’t there in the set. So we are just keeping on adding all the 100 elements but removing none. Hence the answer (2).
So to correct the above program to get the expected result i.e., option (1), we need to cast (i-1) to short and then remove from the set.
But the interesting question is how to detect such kind of bugs. There comes the use of FindBugs a static code analyzer which is very slick.
java, programming
java, puzzle
I am a big fan of books, blogs, screencasts and articles which are related to computer programming and software design. I spend almost 90% of my productive time with computers that are connected to internet.
I have noticed that buying e-books is the right way for me. This can be attributed to my way of living on the edge and the volatile nature of the latest programming languages. E-books provide an easy way to upgrade the book and not to mention greatly reduce the usage of paper (so eco friendly ;)). I know purists will hate to give up the paper book but I am not a purist.
I have recently bought couple of rails books, pickaxe (Ruby) book and Erlang book from The Pragmatic Programmers and have been very satisfied. I get regular email alerts whenever there is an update or new version of the above books and its very easy to get hold of the new copy for no extra charge. However, I am not sure whats the deal with other e-book sellers.
Other than the above there are plenty of other uses like, hyperlinked resources/references which are just a click away from the book, one can copy/paste the sample code instead of laboriously typing the same stuff, no need to context switch between computer and book, no need to switch between keyboard/mouse and pen/pencil, easy to take notes, easy to share snippets of books with friends and many more.
E-books are also advantageous to authors as they can bring the social networking aspect in editing of book. This is suitable in computer programming related books which would require lot of useful code snippets and examples. There are many people who want to contribute for this cause but are not ready to write a book! One of the recent experiences, where e-book might be the only way to go was Rails Recipes by Chad Fowler, this book as the title says is destined to constantly evolve and its not desirable for the reader to buy a copy of this book with each new recipe added or get a seperate addendum for each revision. With e-books its just a click away.
I am sure there are many out there who would want to adopt e-books. So a clear indication of when to buy an e-book (I would say always but …) is if you know or feel that a book will have more revisions in near future.
programming
e-books