Sunday, July 5, 2015

Difference between dependency injection and factory pattern?

Difference between dependency injection and factory pattern?
--------------------------------------------------------------
What is difference between factory pattern and dependency injection or difference between sprint IOC and factory pattern? are some of the frequently asked quesiton in Java irvs. Though both factory pattern and dependency injection might look similar, there is a fundamental difference between them. Factory pattern takes responsibility of creating object, while dependency injection inverse the process how an object gets his dependency, it transfer that responsibility to an external party e.g. Spring IOC transfer object creation and management functionality to the IOC container. In other words, If your use Factory pattern then your object has to retrieve dependency by himself, but in case of dependency injection, dependency will be injected to the object by container or whoever manages that object.

Advantages of Dependency Injection over Factory Pattern

1. Better decoupling of classes .

2. Better Unit testing . Now it is easier to inject mock implementation of the services into the object being tested.


You might also like.


In short, though both Factory pattern and DI helps object creation, DI is much better than Factory. With DI you get better decoupling of object and its dependency. Dependency injection also makes unit testing easier, which is very important to keep quality control in check. BTW, nothing comes free, in order to use dependency injection you need a container e.g. Spring IOC, while you can use Factory pattern by your own. You also need to provide configuraiton to initialize object and inject dependency, like one we use with Spring to initialize beans e.g. aplicatoin-Context.xml

Sunday, June 7, 2015

Tricky Multithreading and Concurrency Questions from Java Interviews

1) Question on volatile variable

2) Can we make an Array volatile in Java?

3) Can you write code for double checked locking in Singleton?

4) What happens if the object your are synchronizing is null?

5) Why Iterator of ConcurrentHashMap doesn't throw ConcurrentModificationException?

6) What is Busy Spin waiting Strategy? What is benefit?

7) If start() calls run() they why not we call run() method directly?

8) Why you should call wait() and notify() method in loop and not on if block?

Few more questions
- If you are asked to write code for synchronizing between two threads or 100 threads? which code will be harder to write and why?

- Tell me three problems you usually face on concurrent environment.

- What happend if you add task into Fixed thread pool and worker queue is full?

- What happend if an exception is throw into a Thread?

Sunday, May 31, 2015

JMSWMQ2020: Failed to connect to queue manager

JMSWMQ2020: Failed to connect to queue manager
-----------------------------------------------
Cause : reason code 2397 comes when SSL is enabled between MQ client and server but SSL handshake is not successful due to expired or mismatched certificates e.g. different signer.

Solution : add proper certificates in both MQ client and server. If you are using Java to connect to MQ then you also need to add correct certificates on keystore and truststore.

Sunday, February 8, 2015

How to round floating point number in Java

How to round floating point number in Java
--------------------------------------------
Rounding real in full:

Just use the method Math.floor () to round the real to the nearest integer:


Double a = Math. floor ( 1 . 99 ); //  1.0
Unlike the method Math.ceil () allows a real round to the nearest integer:


Double a = Math. ceil ( 1 . 01 ); //  2.0
If one wishes to respect the standard rounding rule, using the nearest higher or lower depending on the value around the decimal part, simply add 0.5 to the passed value Math.floor () :


Double a = Math. floor ( 1 . 99 + 0 . 5 ); //  2.0


Double a = Math. floor ( 1 . 49 + 0 . 5 ); //  1.0


If the value of the fraction is less than [b] 0.5 [/ b], the value will be rounded to the nearest whole number.
If the value of the fraction is greater than or equal to [b] 0.5 [/ b], the value will be rounded to the next whole number.
Warning: could also use a cast to type int or long to "lose" the fractional part, but it can cause problems with large values ​​because the cast can lead to a loss of information by truncation. Round to a real 'n' decimals: You can also make a rounded 'n' decimals using the following code:


 Select
Double a =  1 . 6,666,666 ;
//  Round  the  value  was  10 ^ -2
a * =  100 . 0 ;
a = . Math floor (a + 0 . 5 );
a / =  100 . 0 ; //  1.67
For simplicity, this method can be used directly

public  static  double  floor ( double a, int n) {
double p = Math. pow ( 10 . 0 , n)
return . Math floor ((a * p) + 0 . 5 ) / p;
 }
Which is used as follows:

Double a =  floor ( 1 . 6,666,666 , 2 ); //  1.67
But be careful, however, one can obtain an incorrect result when approaching the maximum limit doubles (1e308 anyway). For more accurate results, it may be towards the class BigDecimal and methods setScale () . Rounding to display: In contrast, if the result is to convert to a string, it is best to turn to the class java.text.DecimalFormat

How to use BigDecimal to add or subtract float and double in Java

How to use BigDecimal to add or subtract float and double in Java
-------------------------------------------------------------------
How to make precision calculations correct with floats or doubles?

If you try this:

 Select

. System.out println ( 1 . 3 - 1 . 2 );
You get: 0.10000000000000009 instead of 0.1, as shown on your calculator. In fact, it is impossible to accurately represent 0.1 or any negative power of 10 using a float or a double. The best solution for this problem is to use the java.math.BigDecimal class. For example, it is used like this:

 Select
BigDecimal bd1 =  new  BigDecimal ( " 1.3 " );
BigDecimal bd2 =  new  BigDecimal ( " 1.2 " );
. System.out println (bd1. substract (bd2));
Please note, you want to represent numbers must be passed as Strings in the constructor

J2EE Interview Question and answers for Java programmer

J2EE Interview Question and answers for Java programmer
----------------------------------------------------------
What is J2EE or JEE ?
What does Servlet JSP do in J2EE architecture ?
Difference between Application Server and Web Server?
What does EJB perovides EJB ?
What are popular application server which is used to deploy J2EE application?
What is advantage of using J2EE ?
What is the difference between Session Bean and Entity Bean?
What is distributed transaction and 2 phase commint?
What is difference in Java and J2EE ?
Does spring part of J2EE specificatoin ?

Jakarta Struts Interview Questions and Answers in Java

Jakarta Struts Interview Questions and Answers in Java
-------------------------------------------------------
What is struts ?
What is difference between Struts 1.0 and Struts 2.0?
What is Action class in Struts ?
What is difference between DispatchAction and LookupDispathAction in Struts ?
What is ActionServlet ?
Which design pattern is used in Struts framework?
What is difference between ForwardAction and IncludeAction in Struts?
What is difference between ActionForm and DynaActionForm in Struts ?
How can we prevent duplicate form submission using Struts?
What is difference between reset() and validate() in Struts framework?
What is struts-config.xml in Struts framework?

Resources to learn Java programming

Resources to learn Java programming
------------------------------------
Now, if you decide to learn Java programming, here are couple of resources, which you can take a look. I personally suggest picking a book, because they are almost always well written and catered for beginners. I personally recommend Head First Java to beginners, that's great book to start, if you don't know much about Java. You can also take a look at official Java tutorials offered by Sun Microsystems. These tutorials are comprehensive and covered almost all important details of Java programming language. Further you can use Google along with your learning. If you need to understand a particular concept, term or any issue with Java, Google can point you to specific resource. There are lots of blogs, tutorial sites, and free video tutorials on internet to learn Java programming.

Recommended Java Books for beginners
Head First Java
Thinking in Java

Recommend tutorials
Java SE tutorials
Javadoc for JDK 7