Posts

Showing posts with the label exception

NPE as well as Null check free code ... Really ?

Image
NPE as well as Null check free code … Really ? Is this how your Java code review start ? NPE is always nightmare for a Java developer. Lets not discussion to much and jump on an usual Java code snippet public interface Service { public boolean switchOn(int timmer); public boolean switchOff(int timmer); //Other controls } public class RefrigeratorService implements Service { // ... } public class HomeServices { private static final int NOW = 0; private static HomeServices service; public static HomeServices get() { //Null Check #1 if(service == null) { service = new HomeServices(); } return service; } public Service getRefrigertorControl() { return new RefrigeratorService(); } public static void main(String[] args) { /* Get Home Services handle */ HomeServices homeServices = HomeServices.get(); //Null Check #2 if(homeServices != null) { Serv...

WeakHashMap by example

Usually we read following explanation about java.util.WeakHashMap “A hashtable-based Map implementation with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use” Unlike java.util.HashMap, WeakHashMap maintain weak reference for the all keys objects and strong reference of value objects. As per features of weak reference, they are more prone to be collection by GC. WeakHashMap runs some kind of unknown services to remove all the entries from collection whose keys are already garbage collected. It means entries in weak hash map may changes over the time. Whenever there  is a need of more memory to your application, GC cycle will execute and its job is to collect all un-referenced objects or weak references. In some cases GC may collected keys of weak hash map as those are a weak references. Once keys are garbage collected , WeakHashMap’s implementation job is to clear all such entries.   See the following program, I...

Improved Exception handling in Java 7

Java 7 has improved the exception handling by ease of development, reduced code duplication  and intelligent throws clause. 1. More than one  exception types in single catch block: Java 7 onwards single catch block can handle multiple exception types. Now Catch block can specify ‘|’ separated exception types instead of only one exception. It prevent code duplication in case when same operation has to perform for all of set of types of exceptions and improve code readability. Old style : try { //your code here } catch (IOException ex) { log(ex); throw ex; catch (SQLException ex) { log(ex); throw ex; } New style : try { //your code here } catch (IOException|SQLException ex) { logger.log(ex); throw ex; } 2. Intelligent throws clause : Method declaration has to include the list of exception its is going to throws using throws clause. These exception types has to be matched with catch block throw statement. Java 7 onwards compiler ...

Understanding Java Garbage Collector

Image
     Java’s garbage collection system reclaims objects automatically occurring transparently, behind the scenes, without any programmer intervention. It works like this: when no references to an object exist, that object is assumed to be no longer needed, and the memory occupied by the object is released. This recycled memory can then be used for a subsequent allocation. Although it complete transparent to developer, understanding how GC works helps developer to improve efficiency of Java Program. It is also helpful to improve performance of the program. JDK provides many runtime options to analysis GC and analysis your program behavior. Here is the simple java program to understand GC behavior 1: public class GCTest { 2: public GCTest() { 3: } 4: 5: public static void main(String[] args) { 6: int count = 10000; 7: long startTime = System.currentTimeMillis(); 8: System.out.println(" Total Count = " + count...

How to avoid NPE while Auto-Unboxing

Auto-Boxing/Unboxing is introduced in Java 5 version. It supports automatic conversion of primitive type (int, boolean, float, double) to their equivalent Objects ( Integer, Boolean, Float, Double) in assignment , method and constructor invocation and other way also. Here is the examples Auto-Boxing Integer i = 10; // Auto-boxing converts primitive int value to Integer Object Auto-Unboxing int i = 0; i= new Integre(10); // Auto-Unboxing converts Integer Object to primitive int There is serious problem with using Auto-bocing/Unboxing, it may result null pointer exception. Take an example of following program package testproject; public class TestAutoUnboxing { private static TestAutoUnboxing test= new TestAutoUnboxing(); //cause Null Pointer Exception public static final Boolean YES = true; // Autoboxing for private boolean amIHappy = YES; // Instance Variable public static void main(String[] args){ System.o...