Thursday, 14 April 2016

Differences between CyclicBarrier and CountDownLatch

Here are main differences between CyclicBarrier and CountDownLatch concurrency utils in Java :

1) CyclicBarrier is resulable, CountDownLatch is not.

2) Both CyclicBarrier and CountDownLatch wait for fixed number of threads.

3) CountDownLatch is advanceable but CyclicBarrier is not.

Wednesday, 13 April 2016

Autowire Logger Object In Spring

You could do it with a @Qualifier annotation. Of course that means that you have added Loggerobjects to your application context already.
Importing this config into your application context would allow you to do that:
public class LoggerConfig {
    public Logger myClassLogger() {
        return LoggerFactory.getLogger(MyClass.class);

    public Logger myOtherClassLogger() {
        return LoggerFactory.getLogger(MyOtherClass.class);
And then in your classes that uses the Logger:
public class MyClass {
    private Logger logger;


public class MyOtherClass {
    private Logger logger;


Integer wrapper objects share the same instances only within the value 127

Since Java 5, wrapper class caching was introduced. The following is an examination of the cache created by an inner class, IntegerCache, located in the Integer cache. For example, the following code will create a cache:
Integer myNumber = 10
Integer myNumber = Integer.valueOf(10);
256 Integer objects are created in the range of -128 to 127 which are all stored in an Integer array. This caching functionality can be seen by looking at the inner class, IntegerCache, which is found in Integer:
So when creating an object using Integer.valueOf or directly assigning a value to an Integer within the range of -128 to 127 the same object will be returned. Therefore, consider the following example:
Integer i = 100;
Integer p = 100;
if (i == p)  
    System.out.println("i and p are the same.");
if (i != p)
    System.out.println("i and p are different.");   
    System.out.println("i and p contain the same value.");
The output is:
i and p are the same.
i and p contain the same value.
It is important to note that object i and p only equate to true because they are the same object, the comparison is not based on the value, it is based on object equality. If Integer i and p are outside the range of -128 or 127 the cache is not used, therefore new objects are created. When doing a comparison for value always use the “.equals” method. It is also important to note that instantiating an Integer does not create this caching.
Remember that “==” is always used for object equality, it has not been overloaded for comparing unboxed values

How to Retrieve Automatically Generated Keys in JDBC?

You can retrieve automatically generated keys (also called auto-generated keys or auto increment) from a table using JDBC 3.0 methods getGeneratedKeys(). The getGeneratedKeys()provide a standard way to make auto-generated or identity column values available to an application that is updating a database table without a requiring a query and a second round-trip to the server. SQL Server allows only a single auto increment column per table.
The ResultSet that is returned by getGeneratedKeys method will have only one column, with the returned column name of GENERATED_KEYS.
If generated keys are requested on a table that has no auto increment column, the JDBC driver will return a null result set.
When you insert rows by executeUpdate or execute an INSERT statement or an INSERT within SELECT statement, you need to indicate that you will want to retrieve automatically generated key values. You do that by setting a flag in a Connection.prepareStatement, Statement.executeUpdate, or Statement.execute method call. The statement that is executed must be an INSERT statement or an INSERT within SELECT statement. Otherwise, the JDBC driver ignores the parameter that sets the flag.

Monday, 11 April 2016

Java Basic Quiz -1

Java Basic Quiz -1

Please select atleast one option
  1. Which one is thread safe ?

  2. ArrayList

  3. How many public class created in one java file ?

  4. 1

  5. Which of these method of class String is used to compare two String objects for their equality? ?

  6. Equals()

  7. Standard output variable ‘out’ is defined in which class?

  8. Process

  9. Which of the following is correct way of implementing an interface salary by class manager?

  10. class manager implements salary {}
    class manager imports salary {}
    class manager extends salary {}

If you have any doubt mail me :