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.
Saturday, April 5, 2014
If you are Java programmer then you know what is purpose of JAR file, but for those who are unaware, JAR file is deliverables of Java application. Just like C and C++ application produce EXE files, Java produce JAR files. In other words, A JAR (Java Archive) file is a ZIP format file that bundles Java classes into a single unit, it may contain all the resource needed by Java application as well. There are mainly two types of JAR file in Java: Library JAR (normal JAR) files: JARs which are reusable libraries e.g. Apache commons JAR file, guava.jar itself or even JDBC drivers e.g. ojdbc6_g.jar. There is another type as well, Executable JAR files: JARs which can be executed as a standalone Java applications. Main difference between normal and executable JAR file is that later contains mainfest file, which specify a main-class entry. When you run that JAR file, Java start your application by reading that main-class entry, because you need main method to execute Java programs. I had earlier shared steps to create JAR file from command prompt, which if you have not read, go read it. You will learn about basics of JAR command, which comes with JDK, mainfest file and different attributes of manifest file. In this tutorial we will learn how to make/create/export both library and executable JAR file in Eclipse IDE. Why it's important to know creating executable JAR in Eclipse, because its one of the most used tool by Java programmer. Once you know the steps, you export your Java program as JAR file in a second or two. After creating executable JAR file, you can follow these steps to run Java program from JAR file in command line.