Core Java

  • 1. Java features
  • Polymorphism: overloading within same class, with same method name however different signature (parameters)
    Inheritance: Sub/Super class and interfaces; with method overriding from sub class to have have same method signature however different behavior
    Encapsulation: Java/OOP is emphasizing on encapsulation, so that class/objects can be loosely coupled, (and high cohesion).

  • 2. == and equals difference
  • == is comparing the value; for primitive types (bool, byte, char, short, int, long, float, double) these compares value of the primitive types. for objects, this compare the value (starting memory address) of the object, which unless the two are pointing to same object will be false

    equals is method of the object, default implementation is comparing the address location (==)

  • 3. == and === in js
  • === is strict comparison, which is true if two sides are of same type and same value

    == will do auto conversion (type casting) then compare; if both operands are objects, it will compare internal references (same object in memory)

  • 4. final
  • final is non-access(public, protected, private, default) modifier
    casting on a call, result the class non extended
    casting on a method, the method cannot be overriden
    casting on a variable, the variable must be initialized and cannot changed (changing value, means for objects/collection, would be the pointer pointing to same memory address/object, however, internal state still could be changed) after that

  • 5. String str=”i” vs String str=new String(“i”)
  • first assignment would try to get the string from the pool, if it’s already there, no new object created otherwise, new string and put into the pool
    second assignment would create an additional String object of value “i”, in addition to the “i” in the pool

  • 6. java collection
  • there are mainly two types of collection, list and set are sub types(interfaces) of Collection(interface)
    second type is map

  • 7. collection vs collections
  • collection is the list and set super interface, while collections like other classes ending with s is utilities (ie. sort, synchronized, unmodifiable methods from Collections)

  • 8. Hashmap, HashTable, ConcurrentHashMap
  • HashMap is not thread safe, it’s implemented using array (array of nodes/buckets/bins, which is for each hash code) and linkedList (java 7, when there is hash collision, new node would be appended) or redBlackTree (java 8, if the linkedList longer than TREEIFY_THRESHOLD=8)

    HashTable is thread safe, synchronized (monitorenter and monitorexit) for both get and put methods

    ConcurrentHashMap is thread safe with better performance, only partial lock on writes/put, no lock on get/read (before java 8, it’s locking/synchronized per segment; in java 8, it’s using CAS/optimistic locking for adding to empty bin, otherwise synchrnozied only on the specific bin/bucket/node)

  • 9. List vs Set vs hashSet
  • List is ordered, for easy retrieval by index
    Set is not ordered, and doesn’t allow duplicates
    HashSet is one set implementation, based on HashMap. basically it’s “collection” of keys of a special hashMap(the value is private static final Object PRESENT = new Object())

  • 10. ArrayList vs LinkedList
  • ArrayList is implemented on top of array (however growable), which provides constant time performance for size/get/set/iterator (by index); otherwise linear time (contains would run through whole array O(n))

    LinkedList is doubly linked list, from head to tail interconnected by the pre/next link. for modifications, it’s very fast, since it’s just repoint the pre/next; while retrieving it’s relatively slow, basically need to run through whole link in worst case(O(n))

  • 11. array vs arrayList
  • array is a special Object (direct super class of array is Object, so for reflections, String[] would become Object); the size is pre-defined

    ArrayList is implemented based on array, however, would grow the size/capacity dynamically. it would check and ensureCapacity on each adding

  • 12. poll and remove method on Queue
  • both retrieve and remove the head of the queue; however, remove() would throw noSuchElementException if the queue is empty

  • 13. Collection thread safety
  • List: ArrayList is not threadSafe; Vector is threadSafe; CopyOnWriteArrayList is threadSafe (each mutation is done on the clone/copy), so there is no fail fast issue on COWArrayList (using ReentrantLock)
    Set: HashSet is not threadSafe; CopyOnWriteArraySet is threadSafe

    Map: HashMap is not threadSafe, ConcurrentHashMap is thread safe, HashTable is thread safe

    otherwise, Collections.synchronizedMap/List/Set/Collection would give a threadSafe variation (using synchronized)

  • 14. Iterator vs Enumeration
  • both are almost same, while iterator allow removable (fail fast) and with better names

  • 15. fail fast vs fail safe
  • if there is structural modification during retrieval, List/Vector/HashMap would throw ConcurrentModifiedException; which is to fail fast (modcount different than expectedModCount)

    for ConcurrentHashMap and COWArrayList, since the mutations are done on the clone (

    note: below two blocks behave differently

    List input = new ArrayList();
                    input.addAll(Arrays.asList(new String[]{"first","second"}));
            for(String s : input){
            List input = new ArrayList();
                    input.addAll(Arrays.asList(new String[]{"first","second"}));
            for(String s : input){

    Leave a Reply

    Fill in your details below or click an icon to log in: Logo

    You are commenting using your account. Log Out /  Change )

    Google photo

    You are commenting using your Google account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )

    Connecting to %s