Friday, June 29, 2012

Builder Design pattern in Java - Example Tutorial

Builder design pattern in Java is a creational pattern i.e. used to create objects, similar to factory method design pattern which is also creational design pattern. Before learning any design pattern I suggest find out the problem a particular design pattern solves. Its been well said necessity is mother on invention. learning design pattern without facing problem is not that effective, Instead if you have already faced issues than its much easier to understand design pattern and learn how its solve the issue. In this Java design pattern tutorial we will first see what problem Builder design pattern solves which will give some insight on when to use builder design pattern in Java, which is also a popular design pattern interview question and then we will see example of Builder design pattern and pros and cons of using Builder pattern in Java. 

Thursday, June 28, 2012

10 xargs command example in Linux - Unix tutorial

xargs command in unix or Linux is a powerful command used in conjunction with find and grep command in UNIX to divide a big list of arguments into small list received from standard input. find and grep command produce long list of file names and we often want to either remove them or do some operation on them but many unix operating system doesn't accept such a long list of argument. UNIX xargs command divide that list into sub-list with acceptable length and made it work. This Unix tutorial is in continuation of my earlier post on Unix like 10 examples of chmod command in Unix and How to update soft link in Linux. If you haven’t read those unit tutorial than check them out. By the way In this tutorial we will see different example of unix xargs command to learn how to use xargs command with find and grep and other unix command and make most of it. Though what you can do with xargs in unix can also be done by using options provided in find but believe xargs is much easy and powerful.

Sunday, June 24, 2012

What is -XX:+UseCompressedOops in 64 bit JVM

-XX:+UseCompressedOops JVM command line option is one of the most talked option of 64 bit JVM. Though 64 bit JVM allows you to specify larger Java heap sizes it comes with a performance penalty by using 64 bit OOPS. Ordinary object pointers also known as OOPS which is used to represent Java objects in Virtual Machine has an increased width of 64 bit than smaller 32 bit from earlier 32 bit JVM. because of increased size of OOPS, fewer OOPS can be stored in CPU cache registers which effectively reduced CPU cache efficiency. -XX:+UseCompressedOops enables use of compressed 32 bit OOPS in 64 bit JVM which effectively compensate performance penalty imposed by 64 bit JVM without scarifying heap size advantage offered by them. You should use -XX:+UseCompressedOops if maximum heap size specified by -Xmx is less than 32G. This is my 3rd article on JVM after 10 JVM option Java programmer should know and  how to find 32 bit JVM or 64 bit JVM, I suggest reading those if you want to learn more about JVM.

Saturday, June 23, 2012

HashSet in Java – 10 Examples Programs Tutorial

HashSet in Java is a collection which implements Set interface and backed by an HashMap. Since HashSet uses HashMap internally it provides constant time performance for operations like add, remove, contains and size give HashMap has distributed elements properly among the buckets. Java HashSet does not guarantee any insertion orders of the set but it allows null elements. HashSet can be used in place of ArrayList to store the object if you require no duplicate and don't care about insertion order. Iterator of HashSet is fail-fast and throws ConcurrentModificationException if HashSet instance is modified concurrently during iteration by using any method other than remove() method of iterator class.If you want to keep insertion order by using HashSet than consider using LinkedHashSet. It is also a very important part of any Java collection interview, In short correct understanding of HashSet is must for any Java developer.

Friday, June 22, 2012

JUnit4 Annotations : Test Examples and Tutorial

JUnit4 Annotations are single big change from JUnit 3 to JUnit 4 which is introduced in Java 5. With annotations creating and running a JUnit test becomes more easy and more readable, but you can only take full advantage of JUnit4 if you know the correct meaning of  annotations used on this version and how to use them while writing tests. In this
Junit tutorial we will not only understand meaning of those annotations but also we will see examples of JUnit4 annotations. By the way this is my first post in unit testing but if you are new here than you may like post 10 tips to write better code comments and 10 Object oriented design principles for Programmer as well.

Monday, June 18, 2012

How to close Java program or Swing Application with Example

In order to close Java program we need to consider which kind of Java application it is?, because termination of Java application varies between normal core java program to swing GUI application. In general all Java program terminates automatically once all user threads created by program finishes its execution, including main thread. JVM doesn't wait for daemon thread so as soon as last user thread finished, Java program will terminate. If you want to close or terminate your java application before this your only option is to use System.exit(int status) or Runtime.getRuntime().exit(). This cause JVM to abandon all threads and exit immediately. Shutdown hooks are get called to allow some last minute clearing before JVM actually terminates. System.exit() also accept an int status parameter where a non zero value denote abnormal execute and its the result returned by java command to caller. In this java tutorial we will see example of closing both Java program and Java Swing application. This is also a good swing interview questions which you can ask to any GUI developer and my second article in swing after writing invokeAndWait vs invokeLater

Friday, June 15, 2012

JDBC Database connection pool in Spring Framework – How to Setup Example

Setting up JDBC Database Connection Pool in Spring framework is easy for any Java application, just matter of changing few configuration in spring configuration file.If you are writing core java application and not running on any web or application server like Tomcat or  Weblogic,  Managing Database connection pool using Apache Commons DBCP and Commons Pool along-with Spring framework is nice choice but if you have luxury of having web server and managed J2EE Container, consider using Connection pool managed by J2EE server those are better option in terms of maintenance, flexibility and also help to prevent java.lang.OutofMemroyError:PermGen Space in tomcat by avoiding loading of JDBC driver in web-app class-loader, Also keeping JDBC connection pool information in Server makes it easy to change or include settings for JDBC over SSL. In this article we will see how to setup Database connection pool in spring framework using Apache commons DBCP and commons pool.jar

This article is in continuation of my tutorials on spring framework and database like LDAP Authentication in J2EE with Spring Security and  manage session using Spring security  If you haven’t read those article than you may find them useful.

Thursday, June 14, 2012

Video Example of depth first search (DFS) and breadth first search (BFS) graph algorithm

Breadth first Search (BFS) and Depth first search (DFS) algorithm are two most important graph and tree algorithm used for traversal. you can traverse all nodes of tree or graph by using BFS or DFS. Even though most of us learn about Breadth first search and depth first search in college its not easy to understand and it takes time to grasp the concept and I believe that once you understand how BFS or DFS works its easy to implement logic in Java or C++ but trying to implement or copy code without first understanding the algorithm is not going to work. I have read about BFS and DFS in text books, Wikipedia and several other places but never find an explanation as shown in this video. My friend got this video on YouTube few years back while revising concept of BFS and DFS while preparing for programming interview and data structure and shared with me that this is very simple, clear and concise. So I thought to share this BFS and DFS search video with you guys. This video explains How depth first search algorithm works with detailed example and stack data structure along with How breadth first search algorithm works with queue in simple words and live example of Breadth first search. if you are looking for code example than check out our last post Breadth first search in java code example

Saturday, June 9, 2012

10 Interview Questions on Java Generics for Programmer and Developers

Generic interview questions in Java interviews are getting more and more common with Java 5 around there for considerable time and many application either moving to Java 5 and almost all new Java development happening on Tiger(code name of Java 5).  Importance of Generics and Java 5 features like Enum,  Autoboxing, varargs and Collection utilities like CountDownLatch, CyclicBarrier and BlockingQueue are getting more and more popular on Java interviews. Generic interview question can get real tricky if you are not familiar with bounded and unbounded generic wildcards, How generics works internally, type erasure and familiarity with writing parametrized generics classes and methods in Java. Best way to prepare for Generics interview is to try simple program best on various features of generics. Anyway In this Java interview article we will see some popular Java Generics interview questions and there answer. By the way there are lot of material available in Javarevisited to better preparing for Java and J2EE interviews, you can prepare multi-threading and Collections using 15 thread interview question and Top 10 Java collection interview question along with several other questions answers articles on Spring, Struts, JSP and Servlet. If you are GUI developers and working in Java Swing technology than you can also check interview questions on Java Swing  mostly asked in Investment banks

java.lang.NullPointerException - Common cause of NullPointerException in Java Example

java.lang.NullPointerException or NullPointerException in Java is probably the first Exception you will face in Java. It is true nightmare for beginners in Java but pretty easy to solve once you get familiar with Exception handling in Java. What makes NullPointerException little tricky is its name which has pointer in itself and Java does not support pointers like multiple inheritance in Java . In this article we will see What is NullPointerException in Java, How to solve Exception in thread "main" java.lang.NullPointerException, finding possible cause of Java NullPointerException and how to troubleshoot NPE in Java. Based on my experience once you know little bit about NullPointerException its pretty easy to solve. By the way, as said, "prevention is better than cure", you can avoid != null check and NullPointerException by following some best practices.

Thursday, June 7, 2012

10 examples of Date command in Unix

Date command in unix or Linux is one of important command to learn and master because we always need date information. no matter you want to know current date in unix or your bash script needs current date in unix for archiving purpose you need to use date command. In its simplest format date command shows the current date and time in unix while with sophisticated option we can extract many useful information from unix date command. In this Unix command tutorial  we will see some useful tips on using date command in unix and learn more around date in unix and Linux. One important thing to note is that different implementation of unix date command may not be exactly same e.g. AIX version of date command doesn't support option "-d" or "date". Anyway let's see some example of unix date command:

By the way this is fifth article in my series of covering examples of important and frequently used commands e.g. find examples, grep examples, chmod example and sort command example.

Sunday, June 3, 2012

20 Design pattern and Software design interview questions for Programmers

Design patterns and software design questions are essential part of any programming interview, no matter whether you are going for Java interview or C#  interview. In face programming and design skill complement each other quite well, people who are good programmer are often a good designer as well as they know how to break a problem in to piece of code or software design but these skill just doesn’t come. You need to keep designing, programming both small scale and large scale systems and keep learning from mistakes.Learning about Object oriented design principles is a good starting point. Anyway this article is about some design questions which has been repeatedly asked in various interviews. I have divided them on two category for beginners and intermediate for sake of clarity and difficulty level.