link mingle home | logged in as: fab | logout| help

Add Page Drag Me
Home Programming
Java
Core Java

Java Myths you should know before attending any Technical Interview Part 1

1. You can write corresponding Java function for any C or C++ function
Java programming only supports the pass-by-value parameter-passing mechanism and this put some restriction on the way you can code in Java when compared to C or C++. Variables in Java programming can be one of two types: reference types or primitive types. Both types are handled the same way when passed as arguments to a method. Both are passed by value; neither is passed by reference. So there is no way you can write a generic swap function in Java, which can swap 2 primitive values. For Example the following swap function never works in Java
public static void swap(int li, int lj) //Never works
    { 
     int temp = li; 
     li = lj; 
     lj = temp; 
     } 
2. Garbage Collection invoked by VM and System.gc behave the same way.
Most modern garbage collectors do not always free all objects that are available for collection. Analysis has shown that most objects are short lived and therefore, the collector, in an effort to be more efficient, examines long-lived objects less frequently than short-lived objects. The thinking is that because most objects are short lived, an object that has been referenced for a longer time will probably be referenced later.

Invocation of System.gc method usually results in the garbage collector doing a full collection. Normally, this is a more exhaustive and complete process than when the collector is invoked by the VM itself. The rationale for this is when the VM invokes the collector; it should finish as soon as possible. If a programmer explicitly calls System.gc, the inference is that there is more time available to do the most amount of work. In either case, do not assume that all available objects are collected. There is a better chance that all available objects are collected when the garbage collector is invoked due to an explicit call to System.gc, but there is no guarantee.
3. ArrayLists are more efficient form of storage than Arrays when you have continuously expanding and shrinking data.
ArrayLists are actually made of arrays and are only as efficient as Arrays. Only advantage of using ArrayList over Array is that,ArrayList offers a dynamic List interface which is easy to use. The following code does most of the work for ArrayList when it needs to expand its capacity:
public void ensureCapacity(int minCapacity) {
	modCount++;
	int oldCapacity = elementData.length;
	if (minCapacity > oldCapacity) {
	    Object oldData[] = elementData;
	    int newCapacity = (oldCapacity * 3)/2 + 1;
    	    if (newCapacity < minCapacity)
		newCapacity = minCapacity;
	    elementData = new Object[newCapacity];
	    System.arraycopy(oldData, 0, elementData, 0, size);
	}
    }
This is what ArrayList does when adding a new element
1. Check if ArrayList is full ,if not full add the new element and return.
2. Create a new Object Array of size 3/2 times the old one
3. Copy the contents from old Array to New Array
4. Add the new element to the newly created Array.
Choose ArrayList over Arrays only for ease of coding and not for efficiency.
 
Thumbnails by Thumbshots.net