Tuesday, May 26, 2015
When we think about String Concatenation in Java, what comes in our mind is + operator, one of the easiest way to join two String, or a String and a numeric in Java. Since Java doesn't support operator overloading, it's pretty special for String to have behavior. But in truth, it is the worst way of concatenating String in Java. When you concatenate two String using + operator e.g. "" + 101, one of the popular way to convert int to String, compiler internally translate that to StringBuilder append call, which result in allocation of temporary objects. You can see the real difference in performance of our example program, in which we have concatenated 100,000 String using + operator. Anyway, this article is not just about + operator but also about other ways to concatenate multiple Strings. There are four ways to do this, apart from + operator, we can use StringBuffer, StringBuilder and concat() method from java.lang.String class for same purpose. StringBuilder and StringBuffer classes are there for just this reason, and you can see that in our performance comparison. StringBuilder is winner and fastest ways to concatenate Strings. StringBuffer is close second, because of synchronized method and rest of them are just 1000 times slower than them. Here we will see example of all four ways of concatenating Strings in Java.
Sunday, May 24, 2015
Abstraction and Polymorphism are very closely related and understanding difference between them is not as easy as it looks. Their operating model is also similar and based upon relationship between parent and child class. In fact, Polymoprhism needs great support of Abstraction to power itself, without Abstraction you cannot leverage power of Polymorphism. Let's understand this by what Abstraction and Polymorphism provides to an object oriented program. Abstraction is a concept to simplify structure of your code. Abstraction allows you to view things in more general terms rather than looking them as they are at the moment, which gives your code flexibility to deal with the changes coming in future. For example, if you were to design a program to control vehicles e.g. starting, stopping, horn, accelerator, breaks etc, how do you do that? would you design your program just to work with car or bike or would you think about different kinds of vehicles? This is where Abstraction comes into picture, it allows you think in terms of Vehicle rather than thinking in terms of Car. It provide that generalization much needed for a software to be reusable and customizable.
Saturday, May 23, 2015
There are three ways to convert a String to float primitive in Java parseFloat(), valueOf() method of Float class and new Float() constructor. Suppose you have String which represent a floating point number e.g. "3.14" which is value of PIE, you can convert it to float by using any of those three method. Since String is one of the most prominent data type in Java, you will often find yourself converting String to Int, Double and other data types and vice-versa. Java designer knows about that and they have made arrangement to carry out these basic task in a predictable and consistent manner. Once you know the trick to convert String to float, you should be able to convert String to Integer, Double and Short. BTW, converting String to byte array is little bit tricky because String is text data and bytes are binary, so character encoding comes into picture. If you want to learn more about how to do conversion between String and byte array, see this step by step tutorial for String to byte array.