Monday, February 25, 2013

If an object becomes eligible for Garbage Collection and its finalize() method has been called and inside this method the object becomes accessible by a live thread of execution and is not garbage collected. Later at some point the same object becomes eligible for Garbage collection, will the finalize() method be called again?


Nice question. Isn't it? :)

I urge you to think about this for a moment before hurrying to the answer.

.
.
.
finalize() will run only once. Repeat with me, finalize() will run only once! So, the answer to above question is No.


Wednesday, February 20, 2013

Understanding SQL joins once and for all


Understanding SQL joins is fundamental to any java programmer who needs to fetch data from database. SQL joins can be very confusing. Here is a very simple Venn diagram that help you understand all the important SQL joins pretty quickly.. 



SQL Joins Venn Diagram
















INNER JOIN: 
Select only those rows that have values in common in the columns specified in the ON clause. Records available in both the tables. 

LEFT, RIGHT, or FULL OUTER JOIN:  
Select all rows from the table on the left (or right, or both) regardless of whether the other table has values in common and (usually) enter NULL where data is missing.  (Note:  FULL OUTER JOIN not implemented in Access.)

CROSS JOIN:
Select all possible combinations of  rows and columns from both tables (Cartesian product). If tableA has n records and tableB has m records then a cross join will produce n x m results.. joining every records from tableA with every single record from tableB. So if tableA has 30 records and tableB has 35 records the cross join will produce 30x35 = 1050 records in the result set. (This is Not A Good join - the query may run for a very long time  and produce a huge, not very useful result set.) 
           
MINUS:
You get this by specifying a where clause saying not to include records having a match in tableB.  

Note:
There is nothing like left inner join or right inner join. They both mean inner join, they all are one and the same. 

Monday, February 11, 2013

Can we override the main method() ?


Technically the answer is NO. 

> Main method is a static method.
> Static methods belong to the class and not to the objects.
> If you extend a Parent class having main method the child class doesn't have the parent class's main method so there is no question about overriding it.
> You can write your own main method in the child class but that would be again a static method of the child class and would have nothing to do with the main method of the parent class.

Following code will compile and will execute fine...

public class Parent{
    public static void main(String args[]){
        System.out.println("In Parents main method..");
}
class child extends Parent{
  public static void main(String args[]){
        System.out.println("In childs main method..");
}
You can even call the main method of other class from one class directly as its a static method!!

Any questions??