Differences Between NDM and FTP | Difference Between | Differences Between NDM vs FTP

A good summarised article on NDM vs FTP.

Differences Between NDM and FTP | Difference Between | Differences Between NDM vs FTP.


String Literal versus String Object

Strings are a special type of object in Java. Unlike C++, Strings in Java are character arrays and not necessarily end with a newline character.

So, what is a String literal ?

String a=”abcd”;

When this happens, a pool in the heap is created and “abcd” is stored in it. a now refers to that location in string pool.

String b=”abcd”;

When we write this line, b will refer to the same location in the string pool as a is referring.

Hence the following code would return true, as == checks for content equality and equals checks for object equality.

if (a==b && a.equals(b))

return true;


return false;

Now what happens when we use the new keyword.

String c=new String(“abcd”);

This creates a new object and a separate place not referring to the string pool.

Hence the following code returns false as a and c and not the equal objects.

if (a==c && a.equals(c))

return true;


return false;

Happy coding..  🙂

Difference between HashMap and WeakHashMap?

Nice post on WeakHashMap and HashMap..

Vivek's Tech Blog

WeakHashMap is an implementation of Map interface where the memory of the value object can be reclaimed by Grabage Collector if the corresponding key is no longer referred by any section of program. This is different from HashMap where the value object remain in HashMap even if key is no longer referred. We need to explicitly call remove() method on HashMap object to remove the value so that it can be ready to be reclaimed(Provided no other section of program refers to that value object). Calling remove() is an extra overhead.

Here is a simple program which demonstrate the difference:-

import java.util.HashMap;

import java.util.Map;

import java.util.WeakHashMap;

public class WeakMap {

public static void main(String[] args) {

Map weak = new WeakHashMap();

Map map = new HashMap();


String weakkey = new String("weakkey");

weak.put(weakkey,new Object());

String key = new String("key");

map.put(key, new Object());

weakkey = null;

key = null;




View original post 141 more words


We can differentiate between the three using a very nice diagrammatic approach collected from the internet. Here is a brief description on the three.


JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed.

The JVM performs following main tasks:

  • Loads code
  • Verifies code
  • Executes code
  • Provides runtime environment

JRE :  JRE consistes of JVM and other libraries like rt.jar etc.

JDK: Along with JVM and JRE , JDK consists of the actual development tools such as javac and java.

The diagrammatic approach shown in http://www.javatpoint.com/difference-between-jdk-jre-and-jvm   link is very helpful.

Setting up a JDBC connection to Oracle 11g from java class

For setting up a JDBC connection , we need to have the following data primarily:

DB_URL = “jdbc:oracle:thin:@localhost:1521:xe”;

This is for setting up connection to oracle 11g. For any other databases the DB_URL and the DRIVER may vary. Also  the URL pattern depends on the port and the database that we are trying to connect to and the details may be obtained in tnsnames.ora file in  <drive:\>oracleexe\app\oracle\product\11.2.0\server\network\ADMIN path

XE =
(ADDRESS = (PROTOCOL = TCP)(HOST = Devashish-PC)(PORT = 1521))

Now comes the database username and password. If you already have a user created then you can skip this step else do the following:

1. In start menu in the oracle11g folder click on get started.
2. When prompted to enter the username type ‘system’ and password type ‘tiger’.
3. Localhost appears.
4. Click on application express.
5. Create the database user.

Suppose my user is created with credentials :

USER = “dev”;
PASS = “june”;

Now when the following steps has been performed. Go to eclipse and create a new project. Download ojdbc14 , configure buildpath and add as external jar.

Then the the code for connecting is :

final String JDBC_DRIVER="oracle.jdbc.driver.OracleDriver";
final String DB_URL = "jdbc:oracle:thin:@localhost:1521:xe";
// Database credentials
final String USER = "dev";
final String PASS = "june";
Connection con=null;
Statement stmt=null;
try {
ResultSet rs=stmt.executeQuery("select * from tab");
} catch (Exception e) {

It should return the no of tables fetched e.g. in my case it showed 10.

Happy coding  🙂

Threads Pools and Executor Service

In order to maintain the performance in a distributed system, we might require to limit the number of threads in a thread pool. We have two approaches:

Executor Service & Thread Pools .

I feel executor service is the easier approach whereas Thread Pools is required when we need customization.

Executor Service:

Executor Service is the child interface of Executor. Basically, we can create the following types if executor service as:

FixedThreadPool  :  To create a fixed number of threads, if more tasks are submitted then the idle threads in the thread pools the tasks will wait.


CachedThreadPool : Threads will execute the task and are killed.

SingleThreadPool : Only one thread in the entire thread pool.

We just require to create an object of the Runnable/Thread type and submit it in the threadpool.

A sample code snippet is as follows:

ExecutorService executor= Executors.newScheduledThreadPool(5);

for(int i=0;i<10;i++)


Runnable worker=new WorkerThread(i);




if (executor.isTerminated()) {




class WorkerThread implements Runnable


private int i;

public WorkerThread(int i) {




public void run() {

System.out.println(Thread.currentThread().getName()+” Start= “+i);

System.out.println(Thread.currentThread().getName()+” End.”);




Thread Pool Executor is perhaps a better approach where we have a lot of options.

ExecutorService threadPoolExecutor =        new ThreadPoolExecutor(corePoolSize,            maxPoolSize,keepAliveTime,TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),                   threadFactory,rejectionHandler);

Let us see what the above parameters mean:

Core and maximum pool sizes :

When a new task is submitted in method , and fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle.

If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full.

Keep-alive times :

If the pool currently has more than corePbeoolSize threads, excess threads will be terminated if they have been idle for more than the keepAliveTime.


Any blocking queue can be used to transfer and hold the submitted task such as a Synchronous Queue, a LinkedBlockingQueue or an ArrayBlockingQueue.

Thread Factory: A default thread factory can be used as

ThreadFactory threadFactory = Executors.defaultThreadFactory();

RejectedExecutionHandler : Implementation to handle the jobs that can’t fit in the worker queue.

For that we need to create a class which implements the RejectedExecutionHandler.

class RejectedExecutionHandlerImpl implements RejectedExecutionHandler



public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {

System.out.println(r.toString()+” is rejected”);



So we create the executor pool as:

ThreadPoolExecutor executorPool=new ThreadPoolExecutor(2, 4, 10,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(2),threadFactory,rejectionHandler);

And then we add a new task in the thread pool as

executorPool.execute(new WorkerThread(3));

After all the tasks has been executed by the thread pool we should terminate the thread pool as executorPool.shutdown();

Binary Search Trees : Insertion and Reversal

Recently I was asked by a fellow colleague if I knew how to reverse a binary search tree in Java. I knew the process and had an idea about binary search trees but I was really unable to write a code for that[it was a time limit question]. So after looking at a few articles and understanding the reversal algorithm, I was able to do it with recursion.

So lets recall once what is it we are talking about:

What is a Binary Search Tree (BST)?

Binary Search Tree (BST) is a binary tree data structure with a special feature where in the value store at each node is greater than or equal to the value stored at its left sub child and lesser than the value stored at its right sub child.

What is the solution ?

So, first of all I create a Node type of class

class Node
Node left;
Node right;
int value;
Node(int value)

Now I create a new class in which I define an insert , print and reversal method as :


public void insert(Node node,int value)
if (value < node.value) { if (node.left !=null) { insert(node.left,value); } else { System.out.println(" Inserted " + value + " to left of node " + node.value); node.left=new Node(value); } } else if (value > node.value) {
if (node.right!=null) {
else {
System.out.println(" Inserted " + value + "to right of node " + node.value);
node.right=new Node(value);
public void printInOrder(Node node) {
if (node != null) {
System.out.println(" Traversed " + node.value);
public void reverse(Node node)
Node tmp;
if (node!=null) {


And finally comes the main method as:

Node rootnode= new Node(25);
insert(rootnode, 11);
insert(rootnode, 15);
insert(rootnode, 16);
insert(rootnode, 23);
insert(rootnode, 79);
System.out.println("Traversing tree in order");
System.out.println("Traversing tree in reverse order");

So, finally I could obtain the reversal of binary tree with the help of our valuable resources available in internet. 🙂