Friday, September 12, 2014

Why Constructor is Important in Java

In simple word, Constructor is a method like block of code which is called by Java runtime during object creation using new() operator. Constructor are special in sense that they have the same name as the Class they are part of. They are also special in a sense that they are called by JVM automatically when you create object. Have you ever thought about Why do you need constructor? What benefits it provide? One reason is to initialize your object with default or initial state, since default values for primitives may not be what you are looking for. One more reason you create constructor is to inform the world about dependencies, a class needs to do it's job. Anyone by looking at your constructors should be able to figure out, what he needs in order to use this class.  For example, following class OrderProcessor needs a Queue and Database to function properly.

Wednesday, September 10, 2014

Right way to check if String is empty in Java

What do you most of us do while using String in Java? checking whether String is null or empty right? I am sure you know couple of way to test whether String is empty or not, but do you know the right way to do it? When we talk about Strings in Java, we can imagine them as arrays of characters, and they are, but in Java they are also object. An empty Java String, is considered as the not null String that contains zero characters, meaning its length is 0. However, a Java String that might only contain the white-space character is not considered as empty, it is considered to contain one character and its length is equal to 1. One of the most popular way of checking whether String is empty or not is String class' isEmpty() method, this looks perfect right, it's readable and returns boolean if String is empty otherwise returns false, but problem is you can not call this method without checking whether String is null or not. In another word, this is not null safe and it will throw NullPointerException if String is null. Another popular and faster way to check if String is empty or not is by checking it's length, e.g. if String.length() = 0 then String is empty, but this is also not null safe. Third common way of checking emptiness of String in Java is comparing it with empty String literal e.g. "".equals(str),this method is not as fast as previous two but it is null safe, you don't need to check for null, in case of null it will return false. So in my opinion, this is the right way to check if String is empty or not.  If you definition of empty String also includes null then you can also use Apache Commons Lang's StringUtils class. It has methods like isEmpty() which return true for both null and empty String literal. Again this is also null safe and will not throw NullPointerException.

Monday, September 8, 2014

Top 10 Java 8 Tutorials - Best of Lot, Must Read

It's almost six month since Java 8 has released and there are so many Java 8 tutorials are written by Oracle, bloggers and other peoples, but which should you read? Which tutorials are worth of your time? Actually this question is asked to me by one of my reader called John, he was asking me about some good Java 8 tutorials, which he can read to start with. This encouraged me to take a look at some of Java 8 tutorials available on internet and form a list of best. I have just used three criterion, content, language and length. Because tutorials complements books, they should not be like book. They should be small enough to complete in few hours and complete enough to give useful information. I also like tutorials, which discusses practical problems and examples. Another thing I kept in mind that this list of Java 8 tutorial must explain core features like lambda expressions, streams, default or extension methods, annotation changes in Java 8, Nashron JavaScript engine, new Date and Time API and couple of more interesting features. I think following Java 8 tutorials have all these qualities and they are currently the best Java 8 tutorial available online.

Java67 Headline Animator