Monday, 22 September 2014

What is Garbage Collection

Usually programmer taking very much care while creating the Objects and his neglecting destruction of Useless objects. Due to this neglectance at certain point of time  for the creation of new object sufficient memory may not be available and entire program will be collapse due to memory problems.

But in Java, "as a programmer is responsible only for  creation of objects and he is not responsible for destruction  of useless objects".

Java provided one assistant which is running in the background for destruction of useless Objects. Due to this assistant the chance of failure java program with memory problem i very rare. This assistant is nothing "Garbage Collection".

Hence the main objective of Garbage Collection is to "Destroy useless bojects".

When Garbage Collection is Required?
Programmer is not responsible to destroy useless objects, But It is always a good programming practice to make an object eligible for Garbage collection, if it is no longer required.

An object is said to be eligible for garbage collection , If it is doesn't contain any reference.


Sunday, 21 September 2014

How to Make an Object is eligible for Garbage Collection Explicitly

An object is said to be eligible for Garbage Collection, If it is doesn't contain any reference.
If an object is contain any reference then it is not eligible for Garbage Collection.

If an object is no longer required then assign null to all its reference , then automatically that object eligible for Garbage Collection.

ex:-                Student s1= new Student();
              here Student object is not eligible for Garbage Collection. Because s1 reference is there.
if we assign the null value for the object then that object is eligible for G.C.
           
          ex:-        s1=null;

By Requesting the JVM to Run Garbage Collector:

When ever we are making an object eligible for garbage Collector  it may not be destroyed by G.C immediately when ever jvm runs G.C then only that will be destroyed.

we can request jvm to run garbage collector, programmatically......

1) by System class:
System contain a static method G.C for this
                   System.gc();
2) By Runtime class
by using runtime object  a java application can communicate with jvm.
Runtime class is a singleton class hence we con't create Runtime object by using constructor.
we can create the Runtime object by using factory method getRuntime
                  Runtime r= Runtime.getRuntime();
       

Thursday, 18 September 2014

How many types of Scopes are there in JSP

Scopes in JSP:
4 Types are there:
1.Page
2. Request
3. Session
4. Application

Monday, 15 September 2014

It is possible overloading the main method in java

Yes. It is possible.

public class MainOverloaing {
public static void main(String []args)
{
System.out.println("Hi");
}
public static void main(int... args)
{
System.out.println("hello");
}
}

Output:  Hi

Sunday, 14 September 2014

Palindrome program: Given String is Palindrome or not in java

import java.util.Scanner;

public class TestPolin {

  public static void main(String args[])
  {
     String reverseString="";
     Scanner scanner = new Scanner(System.in);

     System.out.println("Enter a string to check if it is a palindrome:");
     String inputString = scanner.nextLine();

     int length = inputString.length();

     for ( int i = length - 1 ; i >= 0 ; i-- )
        reverseString = reverseString + inputString.charAt(i);

     if (inputString.equals(reverseString))
        System.out.println("Input string is a palindrome.");
     else
        System.out.println("Input string is not a palindrome.");

  }
}



Thursday, 11 September 2014

Java 1.5 new features

  1. Generics for Collections
  2. Enhanced for Loop (for-each loop)
  3. Autoboxing/Unboxing
  4. Typesafe Enums
  5. Varargs/Vargs (Variable-length Argument Lists)
  6. Static Import
  7. Metadata (Annotations)
  8. Formatting
  9. Scanner

Wednesday, 10 September 2014

Autowiring in Spring

Bean wiring means configuring the beans along with its dependencies into on xml.
In bean wiring the programmer is responsible to configure all bean properties into xml file.
In case of auto wiring spring IoC container will automatically wire the dependencies which are in the form of objects. It means auto wiring is supported only for dependency in the form of objects but not primitives and collections.

4 Types of auto wirings are there
1. byName
2. byType
3. Constructor
4. Auto detect.


1)
byName
The byName mode injects the object dependency according to name of the bean. In such case, property name and bean name must be same. It internally calls setter method.
2)
byType
In byType spring container verifies whether a bean class name configured in xml and the property type to be injected are matching or not. If matched then the container injects the class object by calling setter method of the class.
3)
constructor
The constructor mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters.
4)
Auto detect
It is deprecated since Spring 3.

Saturday, 6 September 2014

Difference between Wait() and Sleep() method in java



sleep() is a method which is used to hold the process for few seconds or the time you wanted but in case of wait()method thread goes in waiting state and it won’t come back automatically until we call the notify() or notifyAll().
The major difference is that wait() releases the lock or monitor while sleep() doesn’t releases any lock or monitor while waiting. Wait is used for inter-thread communication while sleep is used to introduce pause on execution, generally.
synchronized(LOCK) {   
    Thread.sleep(1000); // LOCK is held
}

synchronized(LOCK) {   
    LOCK.wait(); // LOCK is not held  }

Thursday, 4 September 2014

Web Service Introduction - SOAP


SQL Command for finding Nth highest salary and name in the given Table

Select empname,salary from Employee a where N = (select count(distinct(salary)) from Employee b where a.salary<=b.salary);

* The above query is used to select the Nth highest salary with the name

SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee );

*The above query is used to select the Second highest salary