Posts

Showing posts from 2011

Where to declare Constants in Java

Its been an topic of debate where should constants be declared. Let look at approaches :- Declared in an Interface. Declared in a Class. Enums Going through google, i found number of explanation for both of these. Each have their pros and cons. I'll try to summarise and share my thoughts on it. Lets start with interface . In Java, Interfaces are used as abstract type, that contain method signatures, constants and nested types as  according to Oracle . We can use interfaces to defined a contract or for communication  as explained in my previous article . If we define constants here, we can have the following consequences:- We cannot stop a class to not implement an interface.So a Global constant interface can be used for subtype which is not advisable. We cannot give define constants depend on certain state condition. All the constants would stay in memory all the time. Although, we can define constants in interfaces, we have the above constraints. Lets look how we ov

More About Java Arrays

With the addition of Collections in Java, the use of primitives and primitive arrays decreases day by day. But at the API level or framework design level, primitives are highly used even today as they are fast and does’t include method chaining.  Arrays class in Java, provides the basic to advanced functionality to arrays. Java 6 adds a couple of more functions to this class. They are   copyOf ( T[ ] original, int newLength  ) copyOfRange( int[ ] original, int from, int to  )  CopyOf copies the array and allows to explicitly defined the size of new array, padding with zeroes (in case of int, float, double, short), null (in case of char) and false (in case of boolean). Its similar to System.arraycopy() but  doesnt allows to specify the size of output array. Simple but useful functionality.   Lets understand with the example:- public class Test { public static void main(String[] args) { int[] orig={1,2,3,4,5,6,6,7,77,8,8}; int[] copyOf = Arrays.cop

Pay Packets of BIG IT Giants

What Apple is paying for a Software developer..? How much is facebook, Twitter, linkedin paying to Analysts? Mentioned above are few Big Tech IT Companies where everybody likes to work. Well few of us got an opportunity. But at least everybody likes to know what they are paying....! Few days back, I come across a  website http://techcompanypay.com/  ,which shows the average salary of IT companies as per their designation. The company claims that it had collected the information publicily available on the internet, so no comment on the authenticity. But at least one can have the idea... Do check it out.

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.