Wednesday, April 21, 2010

String concat Vs String + operator

String concat method used to Concatenates the given string

String h="Hello ";
h=h.concat("World");
System.out.println(h);
o/p Hello World

Using concat method we can Concatenates only strings.

String '+' operator used for Concatenates with another objects or primitive types.

String test="They are total "+7+"Fruits";
In the above string we concat string with integer primitive type.

String sample="The total percentage is "+ 70.5;
In the above string we concat string with float primitive type.

Wednesday, April 14, 2010

ArrayList vs LinkedList

.
ArrayList and LinkedList both are ordered collections.

When we observe the class definitions of these two, ArrayList implements RandomAccess interface where as LinkedList implements Queue interface.

public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable

public class LinkedList extends AbstractSequentialList implements List, Queue, Cloneable, java.io.Serializable

In ArrayList accessing elements is veryfast bcoz its a random access, LinkedList it's bit slow.

In ArrayList adding elements is very costily operation, basically it perform lot of adjustments in the memory when we perform add/delete operation, In linkedList adding or removing elements are very easy as it maintains links between elements. Linkedlist uses nodes concept, each node has a two links ie right and left link. So adding and removing involves adding /deleting links between elements.

Finally when application involves more insertions or deletions go for LinkedList OR application fetches the data at once and uses many times go for ArrayList.