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.
Tuesday, April 7, 2015
In this Java tutorial we will learn how to parse String to Date using Joda-Time library, for example, we will convert date String "04-12-2014" to java.util.Date object which represent this date. Before Java 8 introduced its new Date and Time API, Joda was only reliable, safe and easy way to deal with date and time intricacies in Java. Java's own Date and Time was not that great, starting from JDK 1.1 when they made java.util.Date a mutable object and when they introduced Calendar in Java 1.2. It is one of the most criticized feature of Java on communities along with checked exception and object cloning. Even though Java 8 has corrected its mistake with an excellent, shiny new API, which address all past issue, Joda date and time library still has a role to play in Java systems. First and foremost reason is because most of the large banks and clients are still running on Java 1.6 and will likely take another 5 to 6 year to adopt Java 8, Joda is the only friend you can trust to deal with date and time nightmares. One of the most common task in Java is to parse String to Dates and even though Java provides a utility class called SimpleDateFormat, its not safe to use in multi-threaded environment until you know how to use thread confinement to make SimpleDateFormat thread-safe. Many beginners either create new instance of SimpleDateFormat each time they have to convert String to Date or commit classical mistake of storing it in an instance or static variable, only to face mysterious issues later. Since most of the Joda Time classes are Immutable, you can use them easily and safely in concurrent application.