A programmer's tale

Monday, October 15, 2012

I do not write for long

I do not write for long

Tuesday, July 31, 2007

making class final in fantastic way! - java.lang.Math revisited

Utility classes should be final. Utility classes generally contains static constants and static utility methods .It should not be instantiated nor it should be inherited.
Now how to make this class final and make it non instantiating? Simple question. And the answer is also as simple as the question. Declare the class with final keyword make the constructor private like the Math class in java.lang.Math.
But here the final is redundant, no need to declare the class with final, if the constructor of a class is private, then the class is implicitly final. No need to declare it with the final keyword. Look at the following code:

public class FooUtils {

private FooUtils() { }

}

Here FooUtils is our util class. If we sunclass it as:

public class Bar extends FooUtils {
}


we will get compilation error. Hence no need of final keyword. For the same reason the final keyword in java.lang.Math class in redundant.

Sunday, July 29, 2007

My SCJP 1.5 Experiences

After two months solid preparation, I completed my SCJP 1.5(CX-310-055) on July 21, 2007. I finished up with 90%; answered 65 questions correctly out of 72 questions.

I’ve been programming in Java for around two years. But without the preparation I couldn’t able to enter the deeper corner of J2SE, this preparation not only increase my Java knowledge but also sharpen my skills in Java. With your daily Java works in office it’s not always needed to know some of the most subtleties and sophisticated techniques in Java. But when you prepare for the SCJP, it is more likely that you will learn some very uncommon and usually overlooked Java techniques. For this I really enjoyed the preparation phase.

How I prepared?

I read the SCJP 1.5 book by Katherine Sierra and Bert Bates thoroughly and rigorously. It is best book for SCJP I think. I took every SELF-TEST in this book with a paper and pencil and when the answer went wrong, I took the note in a separate place that became my tips and tricks for the final exam. These tips definitely vary from person to person depending upon their Java knowledge and experiences. So it is always better to prepare this for every individual. CD contents of this book are very useful for preparation. It contains the entire book in PDF format and a mock exam that they call master exam. Take this master exam only after you finished your preparation. You can download one bonus exam from the book site with the CD in your CD-ROM. Don’t take this exam in earlier stage of your preparation.

I also follow the book by Philip Heller, but I took only the exams and SELF-TEST in this book. It is also good book.

I took the help of many successful SCJP candidates all over the world in many SCJP forums and communities. It was really a great help. I took nearly every mock test I encountered online. The javaranch SCJP mock test repository is very good resource for online SCJP mock tests. Take a look at the SCJP FAQ section in javaranch also. All these and many other resources are tagged here.

During my preparation I also took the exams from javablackbelt you can see my status here. My advice is took the exam with paper and pencil and make your own trips and trick repository.

I purchased the exam voucher about 2 and ½ months ago. Some offer was going on and I purchased it by 5,000 INR, but the validity of my voucher was up to 31st August, 2007. The one year validity voucher was 150 USD that time as I saw in Sun site. But it is now 200 USD i.e. more than Rs. 8,000. I am lucky, na!! ;)

Experiences during exam:

You are not allowed to bring any paper or pencil inside the exam room. You will be provided with a small white board or plastic writing pad with a marker pen. You have to make any calculation on that, you may be provided with extra pad if you need that.

Every information about the question types and duration can be available in Sun certification site. But during exam you may face a little problem for the question type. Some questions show you “task” button, you have to click it to see the actual content of the question and after answering you press the “Done” button in the new child window. For some questions you have to press the “Exhibit” button in the lower portion of the main window. It will bring a child window with the question contents. After seeing the question, you “close” the window and select the correct answer. You can mark a particular question for further review during the exam, but before pressing the “End” button remember to remove all the marks in your marked questions.

If you are well prepared, it will be not very difficult to pass the exam, but if you want to score above 85% or 90%, it may be very tricky and tough. The questions appears in the exams are very balanced, they are from beginner level to expert level. So some questions may take time and some are easy to answer in few seconds. Always expect some surprise i.e. some innovative and new types of questions you have never encountered before. But hopefully the number of this type of question is small. That’s good as far as you are well prepared. Don’t make any hurry during the exam, the time is not at all a problem; rather concentrate to your question with reasoning. Look for the syntax and semantic very carefully because many question results in compilation errors. If the answer includes “compilation error” option, then look for compilation error very carefully and then after ensuring it you try to understand which Java concept the question is asking. This can minimize you time and increase accuracy. If you have any confusion and queries you can reply to this post. I will try to clarify that.

Post certification Task:
You can use Sun certified programmer brand logo in your resume for your marketing after completing the exam. I have got my logo. That's look good, na!

Thursday, April 19, 2007

Hadling views in JDBC: better way :)

I've been programming in JDBC for last few months. During this course of programming I'm happy and at the same time unhappy with this API.
Happy because it's just vendor independent. You write your program and run anywhere with modifying the code.
But this are several very annoying issues I've encounter during JDBC programming.
Specially I'm uncomfortable with the DatabaseMetaData in JDBC. It does not provide and abstraction over the ResultSet. There are good and bad of that. But from a programmer point of view, I'm very unhappy with handling ResultSet and it's long list of getters . SQLException is another annoyance to me. Every time I have to handle it or bubble up. But as far as SQLException is concerned I don't think the exception is so much important to another programmer who is going to use your API. Instead I like to write robust fallback code if SQLException occurs. For all these I have written better abstraction of JDBC metadata by which a programmer can retrieve database objects like catalogs, schemas, table etc. more object-oriented way. I will post the API designs, usages etc. of my abstraction later .
For now I'll mention some tricks of handling database views in JDBC. If you are programming in MySQL4.0 with JDBC, you will find that you get all the tables present in the database if you want to retrieve the views. Actually MySQL 4.0 does not support views. But you get tables as views that is not expected. Note some other databases don't support views. Now how to handle this better way:

For getting view you need to call
ResultSet getTables(String catalog, String schemaPattern,
String tableNamePattern, String types[]) throws SQLException in DatabaseMetaData.
But you need to double check that the underlying database really supports views or not.
Now first call ResultSet getTableTypes() throws SQLException to get the table types supported by the database. From the returning ResultSet get the String array that contains the supported table types.
Write a utility method that returns true if an array contains all the elements of another array i.e. if an array be a subset of another array like:

public static boolean containsAll(Object[] array, Object[] arrayToFind) {
for (Object objectToFind : arrayToFind) {
if (!org.apache.commons.lang.ArrayUtils.contains(array, objectToFind))
return false;
}

return true;
}

You can also write little hepler method like :

private boolean supportsView() {
return containsAll(tableTyeps, {"VIEW"});
}

where tableTypes is a array of String before by calling getTableTypes() . like {"VIEW", "TABLE", "SYSTEM TABLE"} etc.

Now before actually calling the getTables(....) method on DatabaseMetaData call the above helper method. If it returns false, then the database does not support views. Hence you can easy return empty array without hitting the database. I have tested this technique several places and it just fly.

Monday, February 12, 2007

Pain of IntelliJ IDEA upgrade: Why this way?

I have read many posts supporting IntelliJ IDEA and lots of it's good features. Inspired by these posts I've decided to work with IntelliJ IDEA. I have IntelliJ IDEA 6.0.1 installed. So far so good. But when I run my IDE, it has come up with the upgradation information that new version 6.0.4 is available. See the dialog:



When I click the Updates link, then another dialog will open :


This is all about the settings of upgrade option. That's good. I'm quite happy with that. But first of all, I don't find any express upgrade option. It's even ok. I will upgrade manually. I click "More Info..." button to upgrade manually and I expect only the upgrade not full download of the IDE. But clicking that button will open the JetBrain site to download the whole package.



I don't have any option to download only the upgrade. I have to download the whole 65.7 MB, again install it and provide the license. One thing I don't understand that why the upgrade process of such leading Java IDE is so tedious. As far as I know the versioning convention of any well established software is like XXX.YYY.ZZZ where XXX is the major version number, YYY is minor and ZZZ is Bug fix or some patches. So if we see this convention the upgrade option of IntelliJ IDEA is nothing but Big Fix or Patches. Why I go for the full installation or download for only the patches? Can anybody tell me the actual working or I'm wrong?

Wednesday, February 07, 2007

Desperately Learning AJAX

I’ve mentioned earlier that AJAX is one of my admiring technologies in the current year. I started my voyage for AJAX; I’m learning it, thinking it, eating it and sleeping it for several days. The main book I’m consulting is AJAX In Action from Manning publication. It’s a fantastic book for learning AJAX; but it demands some good knowledge of JavaScript, CSS and DOM (Document Object Model). You are also expected to be acquainted with Design Pattern, my favorite weapon to fight against bad code. I’m really excited that most of the basic Design Patten and many more AJAX specific pattern can be implemented prudently in AJAX and you can build reusable, robust and manageable AJAX component by using that. I’m also having some, not fully taste of dynamic programming during AJAX coding. Dynamic programming like Ruby, JRuby(100% java implementation of Ruby) etc is coming in front now-a-days. Bye the way, I’m also consulting online resources of AJAX that is my "external force" for "accelerating" my AJAX learning. All these can be found here.
Hopefully within next 2 weeks I will finish up the book and acquire some professional AJAX skill. Then I will go further into deeper AJAX space like popular AJAX framework like dojo, prototype etc

Tuesday, January 30, 2007

Java 1.5 Bug or What? Java 1.6 really rocks - A real life finding

Recently I have found a little but very interesting real life reason that why Java 6.0 is better, robust and stable JDK than Java 5.0; better to say any previous version.
I have written a dialog in Swing that accept user key for my current project REPro- a 100% pure Java reporting solution. We had migrated our project code base to Java 1.5 quite ago. That’s why the initial target Java platform for my Swing Dialog was Java1.5. Look at the sample code for that here:

public class LicenseDialog extends JDialog {

   public LicenseDialog(String title) {
      //..
      setTitle(title);
      setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
      setSize(400, 300);
      //..
   }
}

And the sample main class:

public class Main {
   public static void main(String[] args) {
      JDialog dlg = new LicenseDialog ("License Key");
      dlg.setVisible(true);
   }
}

Now our dialog is shown correctly but when I click the close button, it does nothing. The program doesn’t exit as expected. First time I’m very surprised, but that behavior meets our requirement, because it’s quite common to many License entry dialog; user can exit by clicking only “cancel” button. Hence I didn’t change my code, it remains unnoticed. But recently I tested our code from java 1.5 to Java 1.6. The problem arises. When I compiled and run the same code in Java 1.6, the program fails during run time giving the following error:

Exception in thread "main" java.lang.IllegalArgumentException:
defaultCloseOperation must be one of:
DO_NOTHING_ON_CLOSE,
HIDE_ON_CLOSE, or DISPOSE_ON_CLOSE

The cause: The method actually works correctly for only three parameter WindowConstants.DO_NOTHING_ON_CLOSE , WindowConstants.HIDE_ON_CLOSE or WindowConstants.DISPOSE_ON_CLOSE. If you put any other parameter, then it the method doesn’t tell you anything but doesn’t work correctly in Java 1.5. The method in Java 1.5 is :

public void setDefaultCloseOperation(int operation) {
   this.defaultCloseOperation = operation;
}

When you put JDialog.EXIT_ON_CLOSE in the method it doesn’t match the required parameter and nothing will happen. But dude, see the same method in Java 1.6:

public void setDefaultCloseOperation(int operation) {
   if (operation != DO_NOTHING_ON_CLOSE &&
    operation != HIDE_ON_CLOSE &&
    operation != DISPOSE_ON_CLOSE) {
    throw new IllegalArgumentException("defaultCloseOperation
    must be one of: DO_NOTHING_ON_CLOSE,
    HIDE_ON_CLOSE,
    or DISPOSE_ON_CLOSE");
   }
   int oldValue = this.defaultCloseOperation;
   this.defaultCloseOperation = operation;
   firePropertyChange("defaultCloseOperation", oldValue, operation);
}


It now handles the parameter correctly and gives the above run time error. It also change the "defaultCloseOperation" property of java.awt.Component by invoking firePropertyChange with required method. When I googled it I found the Bug entry for JDialog.

Java 1.6 is really the best JDK ever and it rocks. Some interesting facts about Java 1.6 are:
1. It is developed and tested on nearly 600 machines worldwide, most in a grid.
2. It covered three architectures: x86, AMD64, SPARC
3. 24 operating system including Red Hat linux 7.2, 9.0, RHAS 2.1, 3.0, 4.0, Fedora Core 3, 4, Solaris 8, 9, 10, 11, SuSE Linux 8.1, 0.9, 0.3, Windows 98 SE, ME, NT, 2000, 2000 Pro, 2000 Server, 2003, 2003 AS, XP Home, XP Pro and Vista, etc.
4. 84 tested configurations, 92,316 conformance tests (JCK)
5. 20,244 functional tests
6. 6,930 unit and regression tests
7. It’s test cycle length: 5 weeks – Tier 1, 10 weeks – All configurations.
8. For quality, compatibility and stability, Java 1.6 under gone continuous multi-platform testing.
9. It has community engagement including crack-the-verifier contest. There were 5 submissions but no successful attempts, regressions challenges; 133 submissions resulting in 72 bugs fixed.
You can see the other interesting things about Java 1.6 here.

Labels: