Category Archives: Difference

Java: Difference between HashMap and Hashtable


HashMap and Hashtable both implements Map interface and used to store data in key and value pairs. Both use hashing techniques to get unique keys.

Apart from some similarities, there are many differences between HashMap and Hashtable classes as follows:

java.util.HashMap java.util.HashTable
HashMap class introduced in JDK 1.2. Hashtable is a legacy class.
HashMap inherits AbstractMap class. Hashtable inherits Dictionary class.
HashMap is traversed by Iterator. Hashtable is traversed by the Enumerator and Iterator.
Hashmap, Iterator is fail-fast. Hashtable, Enumerator is not fail-fast.
HashMap is not synchronized and not-thread safe. Hashtable is synchronized and thread safe.
HashMap can be synchronized by calling this code
Map m = Collections.synchronizedMap(hashMap);
Hashtable is internally synchronized and can’t be unsynchronized.
HashMap class allows only one null key and multiple null values. Hashtable doesn’t allow any null key or value.
HashMap is fast. Hashtable is slow.

For more detail:

 

Advertisements

Valid Variations of main() in Java


Main method is launcher method acts as an entry point for the JVM to start execution of a program. JVM always looks the main() method signature to launch the program. Below are different variation of main() method that are valid in Java.

  1. Default Prototype: This most  preferred way to write main() method in Java
    class TestClass
    {
        public static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    

    Meaning of the main Syntax:

    • main(): This is launcher method configured in the JVM to initiate execution of a program.
    • String[]: These are parameters passed as command line arguments.
    • public: This keyword is access modifier to define scope of a method. For JVM can execute the method from anywhere.
    • static: This keyword is Non access modifier use to show part of a class.Here use with Main method so that called by JVM without any object.
    • void: The main method doesn’t return anything. void keyword use to as part of method signature if there is no return type.
  2. Order of Modifiers: We can swap position of modifiers (static and public) in main method.
    class TestClass
    {
        static public void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  3. Variants of String Array Arguments: We can place square brackets at different positions or use varargs (…) for arguments in main method.
             Arguments Array Declaration (Way 1):

    class TestClass
    {
        public static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    

    Arguments Array Declaration (Way 2):

    class TestClass
    {
    	public static void main(String args[])
    	{
    		System.out.println("Main Method");
    	}
    }
    

    Arguments with Variants:

    class TestClass
    {
    	public static void main(String...args)
    	{
    		System.out.println("Main Method");
    	}
    }
    
  4. Final Modifier to static main method: We can make main() as final.
    class Testclass
    {
        public final static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  5. Final Modifier String argument: We can make String args[] as final.
    class TestClass
    {
        public static void main(final String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  6. synchronized keyword to static main method:
    class TestClass
    {
        public synchronized static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  7. strictfp keyword to static main method: strictfp used to restrict floating point calculations.
    class TestClass
    {
        public strictfp static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  8. Combined all  above keyword to static main method:
    class TestClass
    {
        final static synchronized strictfp static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    

Inheritance of main() method

In inheritance JVM Executes the main() without any errors.

class Parent
{
    public static void main(String[] args)
    {
        System.out.println("Main Method Parent");
    }
} 

class Child extends Parent
{ 

}

or

class Parent
{
    public static void main(String[] args)
    {
        System.out.println("Main Method Parent");
    }
}
class Child extends Parent
{
    public static void main(String[] args)
    {
        System.out.println("Main Method Child");
    }
}

In both the cases, Parent.class and Child.class files generated by Java compiler javac. When we execute any of the two .class, JVM will execute without any error as Child class hide parent class method when override.

Method Overloading with main()

Java allow the main method overloading but the program doesn’t execute the overloaded main method when we run your program. Program execution start for one method only which is following one of any above allowed main() method signatures.

import java.io.*;
public class Test { 

	// Normal main()
	public static void main(String[] args) {
		System.out.println("Hello Facing Issues on IT? (from main)");
		Test.main("Facing Issues on IT");
	} 

	// Overloaded main methods
	public static void main(String arg1) {
		System.out.println("Hello, " + arg1);
		Test.main("Dear ","Facing Issues on IT");
	}
	public static void main(String arg1, String arg2) {
		System.out.println("Hello , " + arg1 + ", " + arg2);
	}
}

Output


Hello Facing Issues on IT? (from main)
Hello  Facing Issues on IT
Hello Dear Facing Issues on IT

JDBC : Difference between executeQuery() Vs executeUpdate() Vs execute() method


executeQuery(), executeUpdate() and execute() are the methods of java.sql.Statement interface of JDBC API which are used to execute the SQL statements.

executeQuery() Vs executeUpdate() Vs execute()

executeQuery() executeUpdate() execute()
This method is use to execute the SQL statements which retrieve some data from database. This statement is used to execute SQL statements which update or modify database. This method can be use for any kind of SQL statements.
This method returns a ResultSet object which contains the result returned by query. This method returns an int value which represent number of rows affected by the query. This will be 0 for statement which are returning nothing. This method return a Boolean value. TRUE indicates that query returned a ResultSet object and FALSE indicate returned an int value or returned nothing.
This method is use to execute select query. This method is use to execute non select query. This method is use to execute select and non select queries.
Ex: SELECT Ex:
DML->INSERT , UPDATE and DELETEDDL-> CREATE, ALTER
Any Type of SQL statements.

More on JDBC

Follow below links to know more on JDBC and solving JDBC issues :

Difference between Stored Procedure and functions


The differences between Stored procedures and Functions are given below:

See also :

Stored Procedure Function
Is used to perform business logic. Is used to perform calculation.
Must not have return type. Must have the return type.
May return zero or more values. May return only one values.
We can call function from procedure. Procedure can not be call from function.
Procedure supports input and output parameters Function supports only input parameters.
Exception handling using try/catch block can be used in stored procedures. Exception handling using try/catch can’t be used in user defined functions.

 

More on JDBC

Follow below links to know more posts on JDBC and solving JDBC issues :

JDBC: Difference between Statement, PreparedStatement and CallableSatement


JDBC API introduced statement, PreparedStatement and CallableStatemnet to execute different types of queries:

  1. Statement : Used to execute Normal SQL Queries.
  2. PreparedStatement: Used to execute dynamic or parameterized queries.
  3. CallableStatement: Used to execute StoredProcedure.

Statement Vs PreparedStatement Vs CallableStatement

Statement Prepared Statement Callable Statement
It is used to execute normal SQL queries. It is used to execute dynamic or parameterized SQL queries. It is used to execute Stored procedure or function.
It is proffered when particular query to be executed only once. It is proffered when particular query to be executed multiple times. It is proffered when stored procedure or functions to be executed.
You can no pass parameter to query by using this interface. You can pass parameter to query at run time by using this interface. You can pass three types of parameters by using this interface IN, OUT and IN OUT
This interface mainly used for DDL statements like CREATE, ALTER , DROP etc. This is used to be any kind of SQL queries which are used multiple times It is used with Stored Procedure and functions.
The performance of this interface is very low. The performance of this interface is better than query while using with multiple queries. Performance of this interface is very high because stored procedure execute on database end.
For More: Statement For More: Prepared Statement For More: Callable Interface

More on JDBC

Follow below links to know more on JDBC and solving JDBC issues :