Posts

Showing posts from September, 2011

Learning JDK 7 Features Part 3: Try With Resource

Today we follow some standard coding conventions so that code should easy to read and maintainable or   i can say " Code should look beautiful  ". But there are some cases where we find our code is cluttered or duplicated. Its more often with Exception Handlers , Streams and JDBC  calls i.e. opening and closing streams/connections and multiple catch handlers. Java 7 comes to rescue us from this duplication and is succeeded in it. Before Java 7, to cloase a resource, we write a final block and close every resource in it. but some times the finally block become large and cluttered. Take a look at the code below:- finally { try { if (resultSet != null) resultSet.close(); } catch(Exception e) {} finally { try { if (statement != null) statement.close(); } catch(Exception e) {} finally {

Why java supports Multiple Interface Inheritance

Java does not support multiple class inheritance i.e. we cannot extend multiple classes at one time in a class. But we can implememt multiple interfaces in a class. Lets start with the benefits of inheritance. Interfaces are user for two reasons :- 1) Contract 2) Communication Contract ,is an aggrement between the interface and the implementing class, that the class will provide the implementation of the methods of the interface's. On has to adhere to the contract. Collections in java has many interfaces like Set, List, Map. These interfaces provide the basic set of methods for the class depending on its interface type.We have their implementation classes like HashSet for Set, ArrayList for List and HashMap for Map. Communication , is used to communicate certain functionality of a class to the outside world. This is very usefull when you want to expose a class but we dont want that one should know all the methods , variables and other class information. Instead w

Fix double check locking in Java 5

We can fix the double check locking  using volatile keyword. In Java 5, the JRE system will not allow a write of volatile to be reordered i.e read and write of volatile cannot be reordered w.r.t any previous read and write. So the volatile always gives the latest value. class Foo{ private volatile Helper helper=null; public Helper getHelper(){ if(helper=null){ synchronized(this){ if(helper=null) helper=new Helper(); } } return helper; } }

Learning JDK 7 Features Part 2: Updated Generic Instance Creation

Prior to Java 7, we create the generic instance as follows:- List<String> list = new ArrayList<String>( ); This syntex tunrs urgly in complex situations in which the length of type parameters increases. As mentioned in Effective Java (2nd Edition) chapter 2 , Joshua suggest us a way which can reduce the verbosity of generic instance creation; as follows public class TypeInference {                public static void main(String[ ] args)                     {                       List list = TypeInference.newInstance(); list.add("str");                     }                public static ArrayList newInstance( )                    {                        return new ArrayList( );                   }   } Even the previous method has an limitation that u have to insert a static method for each implementaion class. Oracle with Java 7 comes out a way which take of this redundancy with some limitations. As suggested in oracle documenation , we can

Learning JDK 7 Features Part 1 : Strings in switch Statements

Switch statement is enhanced in JDK 7 with the support of String Object. Prior to it JDK 7 release, switch only supports  byte , short , char , and int   litearls. With this release onwards, we use string objects in switch expressions. Its is same as comparing String.equals method for each case. Please note the comparison is CASE- SENSITIVE.   Let see how it works :- private static String getTypeOfSeasonFromSwitch(String monthArg) { String typeOfSeason = null; switch (monthArg) { case "December": case "January": case "Febuary": typeOfSeason = "Winter"; break; case "March": case "April": case "May": typeOfSeason = "Spring"; break; case "June": case "July": case "August": typeOfSeason = "Summer"; b

Generate Java POJO and Hibernate XML from database tables

Today we have frameworks to persist java objects to database without writing DAO's and hibernate is one which is widely used. But for nascent developers, creating its mapping to database tables is still difficult. Recently I come across a way to generate Java  POJO and hibernate mapping XML  from database tables  using a Jboss Hibernate Tools. Its as easy as a click  and magic, mapping XML is generated and corresponding POJO 's are generated. Listed down the steps :- 1. Install the hibernate-tools jar using Eclipse's Install New software, selecting URL from http://www.jboss.org/tools/download.html depending upon the eclipse version. 2. Click on [File -> New -> Other -> Hibernate -> Hibernate Configuration File] and create a cfg file. The following properties should be specified : jdbc url , username, password, DB schema, driver class and dialect. 3. Click on [File -> New -> Other -> Hibernate -> Hibernate Console Configuration ] and create a ne

My first Post

Welcome you at the JAVA BOOTS . Its a place where u find,discuss,share common problems,coding strategies, design principles etc in java and related technologies. I keep sharing my learnings here. Hope it will be helpful to all.