Monday, September 30, 2013

How clone method works in Java?

The clone() is a tricky method from java.lang.Object class, which is used to create a copy of an Object in Java. The intention of the clone() method is simple, to provide a cloning mechanism, but somehow it's implementation became tricky and has been widely criticized from a 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 a difference between deep and shallow copy in Java.

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.