Wednesday, April 23, 2014
Apart from Class and Interface, Enumeration type or Enum is another popular type in Java programming language. Enum allows you to represent fixed number of things in a type-safe manner e.g. days of week, days of month, planets in solar system, buttons on remote control, keys on keyboard and suits on playing cards. Before Enum was introduced, prior to Java 1.5, integer and string constants are used to represent fixed number of things, known as enum int pattern and enum string pattern. Though they serve the purpose, they had some major drawbacks, one of them was type-safety i.e. you cannot restrict them to represent fixed number of values e.g. an integer constant, which is representing days of week can have value as 100, which is incorrect, given we have only 7 days in a week. These limitation are addressed by providing enum type, which guarantees that only correct and fixed number of values can be assigned to them. Good thing about Enum in Java is that they are feature rich, bad thing is that they are still under-used and many developers doesn't even know about Enumeration type in detail. Though there are lot of blog post about different features of Enum in Java, and there usage, there is hardly any page, which provides a good look on key points about Enum. It doesn't mean to be comprehensive but at-least provides those lesser known details. I had previously shared some features in form of question answer on my article 15 Java Enum Interview Questions and in this post we will learn more about Enum in Java, it's more like a summary and notes than a tutorial, which is meant to quickly realize power of Enumeration type in Java.
Monday, April 14, 2014
One of the confusing part in learning Java for a beginner to understand how to find length of array and ArrayList in Java? Main reason for confusion is inconsistent way of calculating length between two. Calling size() method on arrays and length, or even length() on ArrayList is common programming error made by beginners. Main reason of confusion is special handling of array in Java. Java native arrays has built-in length attribute but no size() method while the Java library containers, known as Collection classes e.g. ArrayList<>, Vector<>, etc, all have a size() method. There is one more thing which adds to this confusion, that is capacity, at any point capacity of any collection class is the maximum number of elements collection can hold. size of collection must be less than or equal to its capacity. Though in reality, collection resize themselves even before it reaches its capacity, controlled by laod factor. I have mentioned this before on my post difference between ArrayList and Array in Java, if you not read it already, you may find some useful detail there as well. So, use length attribute to get number of elements in a array, also known as length, and for same thing in Collection classes e.g. ArrayList, Vector, use size() method. To give you more context, consider following lines of code, can you spot the error, which is bothering our beginner friend:
Wednesday, April 9, 2014
It's been couple of weeks Java 8 has released and lot of Java developers are trying their hands on major enhancement e.g. lambda expression, method reference, new data and time classes, and more importantly Stream API for bulk data operations. In my opinion, best way to learn any new feature or functionality is by writing short examples, and this is what prompt me to write this article. In this Java 8 tutorial, I have shared simple examples of java.util.Stream package. Streams are one of the most important addition on JDK, it allows you to leverage other changes e.g. lambda expression, method reference, functional interface and internal iteration introduced via forEach() method. Some of the most common things we do with Streams are filtering a collection, applying map and reduce function on all elements of collection and taking advantage of lazy evaluation, built-in parallelism via parallelStream(). This is by no means a complete set of examples you need to master Java 8 Stream API, but it will introduce with key functions and encourage you to explore by yourself by reading Java documentation and trying them. If you like to also learn lambda expression in details, you can check my post 10 ways to use lambda expression in Java 8.