Solving Puzzles

Today I read another chapter from a free PDF downloadable book, whose chapters are all from other books for sale. Nice marketing tactic. This chapter was from a book called Java Puzzlers by Joshua Bloch. There were a number of words of wisdom contained inside.

For example, the + operator means string concatenation only when at least one of the operands is itself a String. Otherwise you get addition.

Here is another example stressed by my Java programming class instructor. The == operator tests whether two object references refer to the same object. It does not test whether the two objects contain an equivalent String. To do that requires the equals method.

We really did not get down to how the Java compiler works in my class or the book I read. However I found out that there are multiple passes. One of the first passes is translation of escape sequences. After that tokenization takes place.

Let me end with some tips that are not specific to the Java programming language. One is to not leave code commented out. Just delete it to prevent comprehension problems. The other is to avoid case statements within a switch statement to fall through to the next case. That is just bad practice.

Effective Java

While browsing around the net, I came across this free Java book. It was a PDF whose chapters each contain samples from other books. One such sample chapter was from Effective Java by Joshua Bloch. The second edition of this book came out last year. The first edition was from back in 2001.

Although Bloch now works for Google, he came from Sun Microsystems. This is why he is intimately familiar with Java and the Java API. The book has a list price of $49.99 like the first edition. How you can find discounts at many places like Amazon dot com.

Here are some interesting ideas I got from Chapter 2 of the book. The first is that of a static factory method. This is a method which is static. It returns an instance of the class. This technique has a number of advantages over a normal constructor. This is not to be confused with the Factory Method design pattern.

One thing I found surprising was talk about the protected access modifier. When I learned Java, I was only taught about public and private. However now that I think back upon it, someone in my class who already knew some Java asked whether "protected" had been removed from Java. Unfortunately the instructor did not know the answer.

Another pattern described is the Builder pattern. This is a three part process to initialize objects with a subset of all possible parameters. First you get a builder object. You then call setters on this object for the optional parameter you wish to set. Then you call the build object which uses the properties you set, and generates an object for you.

Finally there is a call for developers to remember to null out references to objects that are obsolete. This is essential to prevent memory leaks. Yes there is potential for memory leaks in Java, especially when you are managing your own objects as in a stack.

Code Viewers

I just a neat blog entry by SteveChy on an alternative way to display Java code. Check out the example image he provided. Better yet, read the post yourself at his Free Ideo Monoid blog yourself.

The general idea is to avoid colorization to specify things in your code. Instead he will use size and positioning to convey meaning. Some of the ideas looked good.

It appears that this is just a prototype. Let's hope this guy follows through and releases an app to view and maybe even edit Java code. Good stuff.

File Parsing

I have an idea for a new Java program which requires information about my colleges schedule of classes. Specifically I want to know what all the Computer Science classes are, and what the prerequisites for each are. This information is readily available in the latest schedule of classes. The chore is to get that information into my program. How do you do that? You write a program of course.

To start with, the schedule of classes is a huge PDF file. I used Adobe Acrobat to export this file to plain ASCII text. Then I wrote a small Java program to read in the file one line at a time. I coded in some rules to parse the lines to figure out which ones were computer science courses. So far I can detect all the CIS courses fine. For now I have the code just grabbing the text of the prerequisites for each class.

The next step is for me to build some structure which can easily represent the relationship between classes. Perhaps this is a big tree. I don't know. I do know that I must first read in all the information. Then I can make the connections in my tree. I already have a Java class called Course. And I have coded most of the logic into my Java class Parser. There are a lot more Java class before I have all the information that I want.

Sorry I have been a little cryptic about my eventual application which will require all this information. Perhaps when I am done I will upload the application here. For now I am keeping it on the down low. I am finding it hard to schedule time to work on my Java programming. As soon as my Java college class ended, there was nothing else driving me to code. And get this. The university does not offer a Java Programming 2 class. Go figure.

The Use of super()

This week I wrap up my college class on Java programming. We spent a lot of time on some basic Java concepts in class. That meant we had to rush through a lot of the final chapters in the book. Unfortunately we rushed through inheritance, and skipped many sections. One such section we skipped was the use of super() to reach up to a superclass.

On first glass, I assumed you had to prepend any methods of the base class when you call them from a child class. However that is not normally the case. You can just call a method. If it is defined in the super class, Java will know what code to execute normally. That is because subclasses are extensions of the superclass. That is, they have access to the same public methods defined in the superclass.

There are two exceptions which require the use of super. One of them is the constructor of the child class. If you do nothing special in this constructor, Java will call the default constructor of the parent class for you. However if you want to call one of the other constructors of the parent class, you must make that call with super().

The other occasion when you need to use super is when you have a method that is overridden in the child class. If you make a call to an overridden method in the child class, Java chooses the overridden method to execute. You might want to execute the code for the overridden method in the base class. To do that, you have to put the super keyword before the method name.