Award-winning Top 25 JUG!

Book Reviews

This book review was submitted by a DenverJUG member as part of the Book Review Program.

BOOK DETAILS

Book cover
Java 1.5 Tiger: A Developer's Notebook
Authors: Brett McLaughlin and David Flanagan
Publisher: O'Reilly
Publish Date: June, 2004
Pages: 200
ISBN: 0-596-00738-8
Publisher's Book Description

Review Date: December, 2004
 

REVIEWER

Krishnan Kannan

REVIEW

Summary

If you are looking for a book that covers only the new features of the Java 1.5 Tiger release, this is the right book. It is a small and focused book that tells you what are the new features and provides examples to highlight the features. If you are new to Java or beginning to experiment with Java, don't get this book. If you are a seasoned Java programmer and want to know what has changed in JDK 1.5, this book is pretty helpful.

Developer notes

Check the Errata first if you are using the sample programs shown in the text as there are few errors in each chapter. Some of the explanations are also redundant (chapter 10).

I found a plugin for Eclipse to compile the JDK 1.5 code. Read the instructions carefully to install this plugin and especially configuring it. You need to have both the (src/bin) as well as the (src15/bin15) directories for this plugin to work as I spent few hours trying to make this work and emailing the author of the plugin).

Chapter 1

This chapter jumps right into the new facilities added in the java.util package. The Arrays class now has the toString and deepToString to print the contents of the passed in arrays in a nice format. The newly introduced Queue interface and the PriorityQueue class offer the developer to use a nice FIFO collection. Overriding the return type of overridden method is also introduced here. All these concepts are shown in this chapter with an example program so the user can see how they can be used.

Chapter 2

This chapter covers the much publicized feature of the Tiger release -- Generics. It shows with a simple example how a LinkedList collection can now take advantage of Generics to check the data types at compile time. Not only the collections, but the Iterators are also now a Generic type so this chapter shows how they have to be paired together during declaration and the pitfalls of declaring only one of them as a generic type. It talks about how the parameters and return types of a method can use the generics type with examples. The "Lint" option to the compiler shows the warnings where generics are not being used in the code, which may cause a potential problem during runtime. The authors explain how to write your own class that takes a generic type as a parameter, which will be very useful practically. Restricting the generic types to certain subclass types is shown with examples, and as the authors say, the syntax of these declarations gets really weirder. Another thing I learned is the wildcard operator for generics and how to use it in the declarations of methods.

This chapter is pretty interesting. It starts from the basics and ends with more complex examples. If you want to get a good handle on the important concept of Generics don't skip this chapter.

Chapter 3

This chapter is about the Enum data types introduced in Tiger. It starts with a simple example of how to declare and use Enum types in a Java program. Then the authors show how to iterate over the values of the Enum and highlight how the Enum data type is very different from the primitive data type int. Enums have their own constructors etc. Couple of examples show how they can be used in the switch statement and the subtle difference s between using them and integers. The new java.util classes EnumMap and EnumSet are described with good examples and I see them being very useful. There is no way to define your own Enum type or extend the java.lang num class according to the authors. Some of the advanced Enum topics like the value specific Enum class bodies where each value can define its own method body is shown with a small example. More explanation about this advanced topic would have helped me understand it more clearly. I thought Enum would be a simple data type like how it's used in C or C++. But Java 1.5 has added lot more power to the Enum data type so it can be very useful.

Chapter 4

Another interesting topic is covered in this chapter -- Autoboxing and Unboxing. It's very nice to have this feature so you don't have to convert back and forth between the objects and its primitive types when used in a Collection. This chapter shows this with examples how simple and easy this autoboxing and unboxing is done. The very tricky feature where the JVM stores the immutable values for some primitive data types in the same object will cause bugs definitely, especially when the "==" operator is used as the authors described. It explains how the tertiary operator (?:) has been modified to accommodate this feature and how the method overloading algorithm has changed slightly. There is no mention of any performance degradation due to this autoboxing and unboxing in this chapter. I found an interesting article about Autoboxing and performance hits.

Chapter 5

This chapter explains the "Varargs" feature of Tiger. It begins by showing how Varargs would benefit us by showing multiple versions of a constructor for a class and then describes the syntax of using Varargs. It shows how the Varargs parameter is just treated like an array and how you can iterate over those variable argument values in the program using some good examples. If you are passing in an Object array to a Varargs method, it shows what precautions you need to take to treat the array appropriately.

Chapter 6

This chapter talks about the Annotation feature available in Tiger. It introduces the three standard annotations: Override, Deprecated and SuppressWarnings first with some examples and then shows how to create custom annotation types. If you want to annotate your customer annotations this chapter shows how to do that pretty extensively using the meta-annotation types Target, Inherited, Documented and Retention. How these meta-annotation types affect the Javadoc output is also explained very well.

Chapter 7

After reading this chapter I get the feeling the author do not like this enhanced for loop functionality of the Tiger. The reason is they talk about what all you can't do with this loop and stress the point that this new for loop syntax doesn't add any value to the language. I think it's a good enhancement and saves typing while iterating over collections but I do agree we lose some flexibility as the authors highlight -- you can tell the position or index within the loop, can't use the remove method on the collection classes within this loop, can't call the get method on a List, iterating backward on a List or array etc. It shows with some simple examples how this enhanced for loop can be used along with generics. It also shows how to iterate over arrays and collections and how to avoid the extra casting that we are used to until the Tiger release.

Chapter 8

This chapter talks about the Static Imports feature in Tiger. Static imports simplify the typing of the class names repeatedly and make the code shorter. For example you can static import the java.lang.System.out and java.lang.System.err in your class and use only err.println or out.println to print a message. Wildcards are supported in the static imports but as the author points out it's good practice to have the imports stated explicitly. Enumerated types can also be statically imported which is very convenient if those enumerated values are used a lot in the code -- no prefix of the enumerated type is necessary to refer the values. Then this chapter shows how multiple member methods with the same name can be declared and what errors might be encountered. You can also mask or shadow the static import with a local variable so the static imports don't take effect. As the authors say, this is not a good practice but to be aware of when tracking bugs.

Chapter 9

This chapter describes the new formatting classes and options available in Tiger. For C/C++ fans, the printf method is now available in the PrintStream and PrintWriter classes so you can start using System.out.printf in the code with similar formatting options. A nice improvement to the printf method is you can reuse the previous values by using the "<" sign in the format. This will reduce the clutter in the printf statement. This chapter lists all the types of formatting options (%a, %b, %c etc) that can be used in the new Formatter class and the printf statements. Referring the argument position by numbering is also supported. For example to refer the 3rd argument in the printf list, you can say %3$. Finally this chapter shows that the format method in the PrintStream and PrintWriter is synonymous with the printf method in those classes.

Chapter 10

I have to warn you -- this chapter covers a lot of ground about the new Thread features and the authors don't explain the concepts very well. Some of the concepts are repeated (printing error) for a couple of pages (e.g. pages 155 and 160 about the FutureTask class). There is an entire book about the new threading features, for which the authors frequently provide a plug. Anyway this chapter starts with the example of catching uncaught exceptions in each thread with the new Thread.UncaughtExceptionHandler interface. Then it shows the new thread-safe collections like ConcurrentHashMap etc. The ConcurrentHashMap class as I understand (from the 1.5 Javadocs and the source code) is a replacement for the Hashtable class instead of the HashMap class as the authors led to believe in the explanation. Since HashMap is not synchronized I don't think there will be much difference by changing it to ConcurrentHashMap class. I have sent this question to the authors for clarification and updates to the book.

Next this chapter shows a variation of the Queue interface -- the BlockingQueue sub interface and various implementations -- where a consumer and a producer will block or timeout. Only the LinkedBlockingQueue is used in the example. Brief descriptions are given for other implementations like ArrayBlockingQueue, PriorityBlockingQueue, DelayQueue and SynchronousQueue.

Another new concept called Executer and ExecutorServices is described next in this chapter with a simple example. How a Callable interface is used along with the ExecutorServices is also shown. You can schedule a task to run in the future with the ScheduledExecutorService class.

Advanced synchronizing topics like semaphores, CountDownLatch, Exchanger, and CyclicBarrier were explained in this chapter briefly. AtomicTypes are explained with a simple example to show how they can be incremented or compared atomically. Finally the new Locking interfaces -- Lock and Condition -- are shown with an example which is little complex to understand.

Overall I feel this is the worst chapter in the book where it deserved lot more attention because of the importance of threads in Java and its complexity. Well, since there is a boatload of new classes in the new java.util.concurrent package with advanced concepts this developers notebook can't explain everything.