Tuesday, August 30, 2016

Adapter Design Pattern in Java with Example

Adapter design pattern in Java, also known as the Wrapper pattern is another very useful GOF pattern, which helps to bridge the gap between two classes in Java. As per the list of Gang of Four patterns, Adapter is a structural pattern, much like Proxy, Flyweight, Facade, and Decorator pattern in Java. As the name suggest, Adapter allows two classes of a different interface to work together, without changing any code on either side. You can view Adapter pattern is a central piece of the puzzle, which joins two pieces, which can not be directly joined because of different interfaces. I see a couple of reference of Adapter design pattern in one of my favorite book Clean Code, but the idea is very well explained in Head First Design Pattern, the image which they show to illustrate Adapter design pattern is worth all the talk.

Saturday, August 27, 2016

5 Free OCAJP 7 and OCPJP7 Mock Exams - Online Practice Test (1Z0-803 and 1Z0-804

Hello guys, today I am going to share a list of free OCAJP7 and OCPJP7 mock exams and online practice tests. If you are preparing for Java SE 7 Programmer 1 or 2 certification or upgrading your existing OCJP 6 exam then this will help you to gauge your progress. As per my experience, the three keys to success in any Java certification is a selection of good books, writing small Java programs to understand concepts, and practicing mock exams. You can see that mock exams are very important to do well on the real exam. They give you much-needed practice as well as some idea about what kind of questions you can expect on real exams. Unfortunately, there are not many good quality free mock exams available for OCAJP7 but I have managed to collect some of them. Earlier, I have shared some OCAJP8 mock exams and received a lot of feedback from my readers about a similar list for OCAJP7 and OCPJP7 exams. This is a smaller list as compared to the previous one but I'll keep adding new mock exams if I come across.

java.lang.numberformatexception for input string null - Cause and Solution

The java.lang.NumberFormatException comes when you try to parse a non-numeric String to Number e.g. Short, Integer, Float, Double etc. For example, if you try to convert . "null" to an integer then you will get NumberFormatException. The error "Exception in thread "main" java.lang.NumberFormatException: For input string: "null" is specifically saying that the String you receive for parsing is not numeric and it's true, "null" is not numeric. Many Java methods which convert String to numeric type e.g. Integer.parseInt() which convert String to int, Double.parseDoble() which convert String to double, and Long.parseLong() which convert String to long throws NumberFormatException to inform that the input String is not numeric.

Saturday, August 20, 2016

5 Essential difference between Callable and Runnable interface in Java?

The difference between Callable and Runnable is one of the most frequently asked multi-threading and concurrency interview question in Java world. I remember, it was 2007 when I first heard about Callable interface and that too on a telephonic interview. Till then, I was happy using Runnable to implement threads and just started paying attention to Java 1.5, as most of the application by then using Java 1.4. That one interview question encouraged me to learn more about several other useful features introduced in Java 5 concurrency library e.g. CountDownLatch, CyclicBarrier, Semaphore, Atomic variables, and Thread pool. This is one of the reasons I always encourage Java developer to give/take regular interviews, just to update your knowledge.

Wednesday, August 17, 2016

InOrder traversal of Binary tree in Java using Recursion and Iteration

This is the second article about tree traversal algorithms using Java. In the first part, we have seen the pre-order algorithm for visiting all nodes of the binary tree and today we'll learn about the InOrder traversal. As I told you before, unlike array and linked list, binary tree has several ways of traversal. The traversal algorithms are broadly divided into depth first and breadth first traversal algorithms depending upon how algorithm actually works. As the name suggest, depth first explores binary tree towards depth before visiting sibling, while breath first visits all nodes in the same level before going to next level, hence it is also known as level order traversal. Both PreOrder and InOrder tree traversal algorithms are depth first and the only difference between pre-order and in-order algorithm is the order on which root, left node, and right node of the binary tree is visited.

Monday, August 15, 2016

Why Timestamp cannot be used in place of Date in Java?

One of the tricky question from Java Interview is, "Can we pass a Timestamp instance to a method expecting java.util.Date?", it's a tricky question because the answer is both Yes and No. You can, of course, pass a Timestamp object to a method with the argument as Date because first, Timestamp is a subclass of java.util.Date and second it contains both date and time values which are missing in either java.sql.Date and java.sql.Time. So there is more reason to pass a Timestamp value to Date but you should not be doing that. Why? because Timestamp is not exactly Date. It's a composite type of java.util.Date and an additional nanosecond value which is fitted there to confirm database DATETIME data type, which supports nanosecond precision. If you look at the implementation of java.sql.Timestamp class, you will find that the long value supplied by Date is stored separately then this nanosecond value.

Monday, August 8, 2016

How to Iterate through ConcurrentHashMap and print all keys and values in Java

Suppose you have a ConcurrentHashMap of String and Integer and you want to print all keys and values, how do you that? This is a common, day to day programming task for Java programmer and there are many ways to do it. The Map interface provides several view methods e.g. keySet(), values(), and entrySet() to retrieve all keys, values, and all key and value pairs as entries. You can use respective methods to print all keys, all values, or all key values pairs. For printing, you also have multiple choice e.g. you can either use enhanced for loop or Iterator, though later also provide you the facility to remove key value pairs while printing if needed. Though you should remember that these views are backed by Map, so when you remove a key-value pair from entry set it will also be removed by the ConcurrentHashMap.

Tuesday, August 2, 2016

Top 10 Maven Plugins Every Java Developer Should Know

In the last couple of years, Maven has become the de-facto build tool for Java applications. Though there are challenges exists from tools like Gradle, but I think the dominance of Maven will help it to win the final battle. When I started with Maven, I was happy with just dependency management but then I come to know about maven plugins which let you customize your build up to the level you can do with ANT. These plugins provide true power to Maven to automate most of the build related task e.g. compilation, testing, packaging, deployment, and even committing the source code into the remote source repository. Many Java developers who use Maven daily are usually not aware of these essential Maven plugins mostly because  Maven just works, or someone has already done the hard work for them.