Posts

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...

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...

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(); } ...

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 informa...

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");                     }        ...