Friday, November 23, 2012

What is difference between BeanFactory and ApplicationContext in Spring framework

BeanFactory vs ApplicationContext
Difference between BeanFactory and ApplicationContext in Spring framework is a another frequently asked Spring interview question mostly asked to Java programmers with 2 to 4 years experience in Java and Spring. Both BeanFactory and ApplicationContext provides way to get bean from Spring IOC container by calling getBean("bean name"), but there are some difference in there working and features provided by them. One difference between bean factory and application context is that former only instantiate bean when you call getBean() method while ApplicationContext instantiate Singleton bean when container is started,  It doesn't wait for getBean to be called. This interview questions is third in my list of frequently asked spring questions e.g. Setter vs Constructor Injection and  What is default scope of Spring bean. If you are preparing for Java interview and expecting some Spring framework question, It’s worth preparing those questions. If you are new in Spring framework and exploring Spring API and classes than you would like check my post on some Spring utility functions e.g. calculating time difference with StopWatch and  escaping XML special characters using Spring HtmlUtils. Coming back to BeanFactory vs ApplicationContext, let’s see some more difference between them in next section.

Thursday, November 22, 2012

How to join three tables in SQL query – MySQL Example

Three table JOIN Example SQL
Joining three tables in single SQL query can be very tricky if you are not good with concept of SQL Join. SQL Joins have always been tricky not only for new programmers but for many others,  who are in programming and SQL for more than 2 to 3 years. There are enough to confuse someone on SQL JOIN ranging from various types of SQL JOIN like INNER and OUTER join, LEFT and RIGHT outer join, CROSS join etc. Between all of these fundamentals, What is most important about Join is, combining multiple tables. If you need data from multiple tables in one SELECT query you need to use either subquery or JOIN . Most of times we only join two tables like Employee and Department but some time you may require to join more than two tables and a popular case is joining three tables in SQL. In case of joining three tables table 1 relates to table 2 and than table 2 relates to table 3. If you look at closely you find that table 2 is a joining table which contains primary key from both table 1 and table 2. As I said it can be extremely confusing to understand join of three or more tables. I have found that understanding table relationship as primary key and foreign key helps to alleviate confusion than the classical matching row paradigm. SQL Join is also a very popular topic in SQL interviews and there is always been some questions from Join like Difference between INNER and OUTER JOIN,  SQL query with JOIN e.g. Employee Department relationship and  Difference between LEFT and RIGHT OUTER JOIN etc. In short this is one of the most important topic in SQL both from experience and interview point of view.

Sunday, November 18, 2012

Difference between Setter Injection vs Constructor Injection in Spring framework

Spring Setter vs Constructor Injection
Spring supports two types of dependency Injection, using setter method e.g. setXXX() where XXX is dependency or via constructor argument. First way of dependency injection is known as setter injection while later is known as constructor injection. Both approaches of Injecting dependency on Spring bean has there pros and cons, which we will see in this Spring framework article. Difference between Setter Injection and Constructor Injection in Spring is also a popular Spring framework interview question.Some time interviewer also ask as When do you use Setter Injection over Constructor injection in Spring or simply benefits of using setter vs constructor injection in Spring framework. Points discussed in this article not only help you to understand Setter vs Constructor Injection but also Spring's dependency Injection process. By the way if you are new in Spring framework and learning it, you may want to take a look at my list of 5 good books to learn Spring framework. That will certainly help on  your learning process. Since Spring is now a must have skill for Java programmers, it worth putting time and effort to learn this powerful framework

Wednesday, November 7, 2012

Difference between TreeSet, LinkedHashSet and HashSet in Java with Example

TreeSet, LinkedHashSet and HashSet all are implementation of Set interface and by virtue of that, they follows contract of Set interface i.e. they do not allow duplicate elements. Despite being from same type hierarchy,  there are lot of difference between them; which is important to understand, so that you can choose most appropriate Set implementation based upon your requirement. By the way difference between TreeSet and HashSet or LinkedHashSet is also one of the popular Java Collection interview question, not as popular as Hashtable vs HashMap or ArrayList vs Vector but still appears in various Java interviews. In this article we will see difference between HashSet, TreeSet and LinkedHashSet on various points e.g. Ordering of elements, performance, allowing null etc and then we will see When to use TreeSet or LinkedHashSet or simply HashSet in Java.

Tuesday, November 6, 2012

Difference between final, finally and finalize method in Java

What is difference between final, finally and finalize method is asked to my friend in a Java interview with one of the US based Investment bank. Though it was just a telephonic round interview, he was asked couple of good questions e.g. how to avoid deadlock in Java, How get() method of HashMap works and one of the puzzle which is based on recursion. In short final keyword can be used along with variable, method and class and has different meaning for all of them. finally is another Java keyword which is used in Exception handling along with try, catch, throw and throws. finalize() is a special method in Java which is called by Garbage Collector before reclaiming GC eligible objects. In this Java interview questions article we will compare final vs finally vs finalize and highlight some important difference between final, finally and finalize method in Java.

Sunday, November 4, 2012

4 ways to search Java Array to find an element or object - Tutorial Example

Searching in Java Array sounds familiar? should be,  because its one of frequently used operations in Java programming. Array is an index based data structure which is used to store elements but unlike Collection classes like ArrayList or HashSet which has contains() method, array in Java doesn't have any method to check whether an element is inside array or not. Java programming language provides several ways to search any element in Java array. In this Java tutorial we will see 4 examples of searching Array in Java for an element or object.  Every example is different than other and some of them are faster and others are slow but take less memory. These technique also valid for different types of array e.g. primitive and object array. I always suggest to prefer List over Array until you need every bit of performance from your Java application, it not only convenient to work but also more extensible.

Saturday, November 3, 2012

Why use @Override annotation in Java - Coding Best Practice

@Override annotation was added in JDK 1.5 and it is used to instruct compiler that method annotated with @Override 
is an overridden method from super class or interface. Though it may look trivial @Override is particularly useful while
overriding methods which accept Object as parameter just like equals, compareTo or compare() method of Comparator 
interface. @Override is one of the three built in annotation provided by Java 1.5, other two are @SuppressWarnings and @Deprecated. Out of these three @Override is most used because of its general nature, while @SuppressWarnings is also used while using Generics, @Deprecated is mostly for API and library. If you have read my article common errors while overriding equals method than you have see that one of the mistake Java programmer makes it,  write equals method with non object argument type as shown in below example:

Friday, November 2, 2012

Prefer TimeUnit Sleep over Thread.Sleep - Java Coding Tips

What is TimeUnit in Java
TimeUnit
in Java is a class on java.util.concurrent package, introduced in Java 5 along with CountDownLatch, CyclicBarrier, Semaphore and several other concurrent utilities. TimeUnit provides a human readable version of Thread.sleep() method which can be used in place of former. From long time Thread's sleep() method is standard way to pause a Thread in Java and almost every Java programmer is familiar with that. In fact sleep method itself is a very popular and has appeared on many Java interviews. Difference between wait and sleep is one of the tough Java question to answer. If you have used Thread.sleep() before and I am sure you do, you might be familiar with fact like it's an static method, it doesn't release lock when pausing Thread and it throws InterruptedException. But what many of us doesn't consider a potential issue is readability. Thread.sleep() is an overloaded method and accept long millisecond and long nanosecond, which makes it hard for programmers to find out exactly how many seconds, minutes, hours or day current Thread is sleeping. Look at below example of Thread's sleep() method:

Thread.sleep(2400000);