Monday, September 30, 2013

How clone method works in Java

clone is a tricky method from java.lang.Object class, which is used to create copy of an Object in Java. Intention of clone() method is simple, to provide a cloning mechanism, but some how it's implementation became tricky and has been widely criticized from long time. Anyway, we will not go to classic debate of clone in Java, at-least for now; instead, we will try to learn how clone method works in Java. To be fair, understating cloning mechanism in Java is not easy and even experienced Java programmer fail to explain how cloning of mutable object works, or difference between deep copy and shallow copy in Java. In this three part article, we will first see working of clone method in Java, and in second part we will learn how to override clone method in Java, and finally we will discuss deep copy vs shallow copy mechanism. The reason I chose to make this a three part article, is to keep focus on one thing at a time. Since clone() itself is confusing enough, it's best to understand concept one by one. In this post, we will learn what is clone method, what it does and How clone method works in Java. By the way, clone() is one of the few fundamental methods defined by objects, others being equals, hashcode(), toString() along with wait and notify methods.

Tuesday, September 24, 2013

JSTL forTokens Tag Example - Split String in JSP

JSTL forTokens tag is another tag in core JSTL library to support Iteration or looping. It effectively complements, more useful <c:forEach> tag, by allowing you to iterate over comma separated or any delimited String. You can use this tag to split string in JSP and can operate on them individually. forTokens tag has similar attribute like forEach JSTL tag except one more attribute called delims, which specifies delimiter. For example to iterate over colon separated String "abc:cde:fgh:ijk", delims=":". By the way, forTokens tag also accept multiple delimiter, which means, you can split a big string into token based upon multiple delimiter e.g. colon(:) and pipe (|), This will be more clear, when we will see examples of JSTL forTokens tag in JSP. Rest of attribute e.g. items, var, varStatus, begin, end and step are same, as they are in case of <c:forEach> tag. For quick review, items specify String which needs to be split-ed in token and var hold current String.

Tuesday, September 3, 2013

How Maven find dependency JARs while building Java Project

One of the most attractive feature of Apache Maven framework it to manage dependency JARs, but do you know, How Maven finds dependency JAR while building Java project? From where does it download all JAR files? How does Maven search for a particular dependency? In fact, these are some of the frequently asked questions by Maven beginners. Many Java programmer started using Maven as build tool, because of it's dependency management advantages. Since almost all Java project uses lot's of open source libraries, which means adding multiple JAR into classpath. In fact, this dependency on JAR file is one of the most time consuming part of setting up a Java project in Eclipse. Maven comes in picture, and allows you to just declare dependency in pom.xml file, rest will be taken care by Maven. In this article, we will take a closer look on How Maven manages dependencies and How does it finds respective JAR files during build  process.