Thursday, November 27, 2014

How to create User Defined Exception class in Java

Java has very good support of handling Error and Exception, It has a well-defined Exception hierarchy and language level support to throw and catch Exception and deal with them. Java Programmers often deals with built-in exceptions from java.lang package and several others which are already defined in JDK API e.g. NullPointerException. If you have read Effective Java, you may remember advice of Joshua Bloch regrading Exception. According to him you should try to reuse the Exception classes provided in the JDK, e.g., IndexOutOfBoundException, ArithmeticException, IOException, and java.lang.ArrayIndexOutOfBoundsException , instead of creating new ones for similar purpose. But there are cases when a user defined, custom exception provides better opportunity to deal with special cases. So remember, you can always create you own Exception classes by extending from the class Exception or one of its sub classes. Also note that RuntimeException and its sub classes are not checked by the compiler and need not be declared in the method's signature. Therefore, use them with care, as you will not be informed and may not be aware of the exceptions that may occur by using that method (and therefore do not have the proper exception handling codes) – a bad software engineering practice. Once again, even Joshua Bloch has advised to use standard exception in Effective Java.

Tuesday, November 25, 2014

Java 8 Comparator Example Using Lambda Expressions

What is the best way to learn lambda expression of Java 8? Of course by using it on your day to day programming task. Since implementing equals, hashcode, compareTo, and compare methods are some of the most common task of a Java developer, it make sense to learn how to use lambda expression to implement custom Comparator and Comparable in Java. One question comes in mind, can we use lambda expression with Comparator, because it's an old interface and may not implement functional interface annotated with @FunctionalInterface annotation? Answer to this question is Yes, you can use lambda expression to implement Comparator and Comparable interface in Java, and not just these two but to implement any interface, which just got one abstract method, remember from Java 8 interface can have non abstract methods as well e.g. default and static methods. That's why lambda expression in Java 8 is known as SAM type, where SAM stands for Single Abstract Method. This was a very important decision Java designers made, which makes it lambdas even more useful. Due to this you can use lambda expressions with Runnable, ActionListener and several other interface which got just one abstract method. By the way, you need not to worry in case of Comparator, because it is made to implement @FunctionalInterface as shown below :

Sunday, November 23, 2014

How to test if an Array contains a value in Java - Linear Search

One of the common coding question from Java interviews is how to test if an Array contains a certain value or not? This is simple question but some time interview pressure makes candidates nervous. Since array in Java doesn't have any inbuilt method for search, interviewer prefer to ask this question, to see how candidate deal with such situation. If you have good knowledge of Java API then you will immediately come to know that there are alternatives available e.g. binary search of Arrays class or taking advantage of ArrayList contains method by first converting your array to ArrayList. If you come up with those solution, Interviewer will surely ask you to write down a method to search an element in array without using any library method. You can easily solve this question if you know linear search or binary search algorithm. Linear search is very simple to implement, all you need to do is loop over array and check each value if that is the one or not. Binary search is little tricky but not too difficult either, recursive version is very natural as well. In this tutorial, though I have given two solution, one is using ArrayList, and second is using linear search, leaving binary search an exercise for you. But you must remember to sort array before using binary search. By the way to make question more challenging, I usually asked candidate to write a parametric method using generic so that it will work for any type of object array in Java.

Java67 Headline Animator