Tuesday, June 15, 2010

Difference between Delete, Truncate and Drop in SQL

Delete is a DML (Data Manipulation Language) command where as Truncate and DROP are DDL (Data Definition Language) commands.

Three are used to remove rows from the table, but there are few differences

If we use Delete on any table, all rows in that table are deleted, but it doesn't free the space containing the table.

Ex: DELETE FROM TABLE_NAME WHERE CONDITION

If 'where' condition is not specified in the above example, all the rows in the corresponding table are deleted.

Truncate is also used to delete the rows from the table, but it frees the space containing the table

Ex: TRUNCATE TABLE TABLE_NAME

Drop is used to delete the entire table including rows, privileges, relationships with other tables.

Ex: DROP TABLE TABLE_NAME

Delete operation can be rolled back (B'coz Delete is a DML command), where as Truncate and drop operations can't be rolled back (B'coz these two are DDL commands).

Tuesday, June 8, 2010

Why Multiple Inheritance is not supported in java using classes

Why Multiple inheritance is not possible in java using classes means that it can be achieved using something else. We can use interfaces to make multiple inheritance possible in java.

Let us first discuss why its not possible with classes using a simple example

Let us assume we've two programmers A and B

A has written a class named ClassA which is inheriting ClassSuper and overriding one of its methods called Hello()

B has written a class named ClassB which is inheriting ClassSuper and overriding one of its methods called Hello()

Now, if multiple inheritance is allowed in java, another programmer called C wanted to extend both the clasess ClassA and ClassB which are written by A and B programmers respectively.

He is not overriding the method Hello() in his class. What if he calls method Hello()?

How does Java Virtual Machine(JVM) know which Hello() method (from ClassA or ClassB) to call?

This is the reason why multiple inheritance is not possible in java. But, still it can be achieved using interfaces. One must implement all the methods presented in interface if he is implementing interface. So, there is no possibility of confusion that which method to call, because java doesn't allow same method signatures in one class or interface.

Friday, June 4, 2010

Different types of variables in java

There are three types of variables in java
1. Instance Variables
2. Static Variables
3. Local Variables

1. Instance Variables: Instance variables are also called global variables. Each instance of the class has its own copy of instance variables (This is the fact that they are called instance variables). These variables are not declared with in any of the methods in a class but they are declared with in the class. These variables are also called non-static fields.

2. Static Variables: Static variables are defined with in the class with static key word (This is the fact that they are called static variables). Static modifier tell the compiler that there is only one copy of this variable exists regardless of how many times the class has been instantiated.

3. Local Variables:
Local variables are defined with in the method. They are only accessible with in that method.

Here is the example for variables

public class Variables
{
public static int staticVariable = 0;
int instanceVariable = 0;
public static void main(String args[])
{
String localVariable = "Hello";
System.out.println(localVariable);
}
}

From the example:

Instance Variable is int instanceVariable = 0;
Static Variable is public static int staticVariable = 0;
Local Variable is String localVariable = "Hello";