Answered Questions

  • Arrays/Linked List

    When and on what conditions will you decide whether to use linked lists or arrays, also Which process is fastest and which is slowest in arrays as well as linked list i.e, sorting, searching, add, delete etc.

    Star Read Best Answer

    Editorial / Best Answer

    Aqan12  

    • Member Since Apr-2010 | Jun 13th, 2010


    Memory:
    When you create an array you allocate all the memory while initializing the array, so if you had an array of 1000 Objects, you'd allocate space to hold 1000 object references as soon as you initialize the array.

    LinkedList on the other hand extends dynamically as the objects are added to the linked list.

    Accessing Elements:
    Arrays are useful if you are going to access elements using the index. if you need to access the 100th element you can directly access it by a[100].

    To access 100th element in a LinkedList you'll have to traverse 99 elements to reach the 100th element.

    LinkedList can handle addition/deletion of elements better then arrays and that's one of the most powerful feature of LL over arrays.

    Adding/Inserting Elements:
    Array is fixed size; to add a number of elements greater then the size of the array you'd have to copy the array into a new bigger array.

    To insert an element in the middle of an array all the elements after the inserted element would have to moved up one index.

    LinkedList can grow dynamically.

    Deleting Elements:
    To delete an element from Array you'd have to move down all the elements after the deleted element.

    LinkedList elements can be deleted dynamically.

    To summarize, LinkedList can handle manipulation of data structure much better and efficiently manage memory but arrays have better accessibility when accessing elements by index.
    Search and Sort would be similar performance on both.

  • Memory Allocation

    What is the difference between below 2 statements? String s1=new String("abc");String s2="abc";How it will allocate in memory?

    gaurav

    • Jun 15th, 2012

    in String s1 = "Hello World"; // "Hello world" is a constant and it will be allocated in the string pool. all the variables refering to "Hello World" constant will refer to same memory location in th...

  • Object Creation

    In how many ways we can create an object? Explain with example.

    Star Read Best Answer

    Editorial / Best Answer

    vishrutha  

    • Member Since Feb-2009 | Feb 13th, 2009


    1. Using new keyword
    This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.

    MyObject object = new MyObject();

    2. Using Class.forName()
    If we know the name of the class & if it has a public default constructor we can create an object in this way.

    MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

    3. Using clone()
    The clone() can be used to create a copy of an existing object.

    MyObject anotherObject = new MyObject();
    MyObject object = anotherObject.clone();

    4. Using object deserialization
    Object deserialization is nothing but creating an object from its serialized form.

    ObjectInputStream inStream = new ObjectInputStream(anInputStream );
    MyObject object = (MyObject) inStream.readObject();

    5. Using class loader
    one more is through creation of object using classloader like

    this.getClass().getClassLoader().loadClass(”com.amar.myobject”).newInstance();

    Now you know how to create an object. But its advised to create objects only when it is necessary to do so.

    Max

    • Sep 7th, 2011

    All sorts of syntax sugar can also be named: 6. New String as a result of concatenation: String varName = getClass().getName(); //to avoid compile-time optimization String a = "Prefix" + varName; ...

  • If a page only has one gridview, no other controls. Should Ajax be used? Justify

  • what is difference between runnable and thread ?why we use static class

    AMIT GURJAR

    • Sep 21st, 2012

    Sir please tell me that when a thread never return desired result then why we use it?