##Nội dung câu hỏi và phần trả lời
- Check variables at compile time
- Weak typed: check variables at runtime (script languages such as: JavaScript, PHP…).
- Class resources
- Used for method, attributes, inner class.
- Available for all objects.
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism.
- One name many forms
- Override, overload methods.
- Increase flexibility.
- Increase reusability
- Extends class, implements interface.
- Is – a relationship.
- Hiding information and data.
- Use access modifier(public, protected, private)
- Make the system more modularized.
- Overriding
- Overloading
- Anonymous class.
- Java method has five elements: modifiers, return types, names, parameters, exceptions.
a. Overloading method:
- Same names
- Others are flexible
b. Overriding:
- Same names
- Same parameters (number and type, order)
- Access modifier is less restrict
- Return type: same type or covariant type. (equal or narrower)
- Exception: Checked exception (equal or narrower); flexible runtime exceptions.
- Binding: Association btw reference and actual object.
- Binding at runtime (Overriding method).
- Static binding: at compile time.
- Increase extendability.
- Increase abstraction of layered architecture.
- Use interface or abstract class.
- Composition: Has – a relationship. (Famous example: Object Adapter pattern)
- Inheritance: Is – a relationship. (Class adapter pattern).
- Inheritance:
o An overriding method can throw any uncheck exceptions
o An overriding method can throw narrower or fewer exceptions than the overridden method.
- Composition:
o Use try-catch block or throws exception when re-use method which throws exception
- Implementation
- Characteristics of method and attribute
- Purpose of using.
a. Abstract class:
- Single inheritance with “extends” key word
- Could have both abstract and concrete methods. Attributes are normal as normal classes.
- Use when we want to have common behaviors for subclasses.
b. Interface:
- Support for multiple inheritance.
- Have only abstract methods
- Provide the contract.
- Equals() method:
o Compare logically two objects.
- hashCode():
o An integer number associcated with the objects using for storing and retriving in demands.
- Both methods are useful when we want to store objects in hash collection or set duplicate elements.
- Equals() method:
o Public boolean equals(Object obj){} (must pass Object type)
o Check null -> check instanceof -> compare properties.
- Hashcode():
o Public int hashCode(){}
o Based on attributes we implement an agorithm to generate distinct numbers.
- equals(): Compare logically.
- “==” : Compare address.
- Comparable:
o Override compareTo(Object obj)
- Comparator:
o Act as the third party
o Override compare(Object obj1, Object obj2)
- Comparable: implement to compare an object itself with another.
- Use:
o Avoid duplicate elements on Set
o Sort collections or array by using Collections.sort(collection) and Arrays.sort(array)
- Yes
- With each criterion, we have implement Comparator interface
- GC:
o JVM mechanism for collecting unused objects and removing them.
o Purpose: optimize and save memory.
o Couldn’t enforce but could register:
Object.finalize()
Call gc() method of System and Runtime.
- ArrayList:
o No synchronization
o Increase 50% capacity.
- Vector:
o Synchronization
o Double capacity when full size.
- HashMap:
o No synchronization
o Allow one null key and many null values
- HashTable:
o Synchronization
o Don’t allow null key and null values.
- HashMap:
o Don’t guarantee the order of keys.
- TreeMap:
o Implements SortedMap interface
o Order of keys is sorted.
- Use ConcurrentHashMap
- List:
o Support random access by index
o Allow storing duplicate elements.
- Set:
o Don’t support random access
o No duplicate elements.
- Implements Comparable -> Use Collections.sort();
- Implements Comparator -> Use Collections.sort(list, comparator);
- Override equals() and hashCode().
- Wrong implementation of equals() can lead to memory leak problem.
- Solution 1: Iterate two sets then check in loops one by one
- Solution 2: Move elements to two lists then sort lists -> check common element with an efficient algorithm.
- Solution 1: Convert it to a set then set contains no duplicate objects.
- Solution 2: Sort the list then compare continuous objects faster.
- A Java interface for traversing through collection.
- hasNext(), next(), remove();
- Traverse through a collection.
- Make a copy of collection data.
- No effects to the collection.
- TreeSet:
o Implements SortedSet interface.
o Use a tree for storage.
o Elements are sorted.
- HashSet:
o Extends AbstractSet interface.
o Use hash table for storage.
- Array:
o Fixed size
o Data type: primitive, objects.
o Dimension: multi-dimension array.
- ArrayList:
o Dynamic size.
o Data type: only object.
o Dimension: No.
o Support Generics from Java 5.
- ArrayList.toArray() (From ArrayList to Array)
- Arrays.asList(array). (Vice-versa).
- MultiMap:
o Component of Guava framework.
o One key, multiple values.
o get(key) return a list of values.
- LinkedList:
o Provide linked list data structure.
o Use large memory (for references).
o Efficient for inserting or deleting.
o Not efficient for random access as a normal list.
- Immutability:
o String is immutable.
o StringBuffer and StringBuilder are mutable.
- Synchronization:
o StringBuilder is not synchronized.
o StringBuffer is synchronized
- When modifying a String, a new String object is created in memory, stored in the String pool and the instance refers to the new object.
- Split a string into an array.
- Use for loop to iterate the list from end to beginning.
- Pass by value:
o Pass only the bit-pattern (copy) of value.
o Method can’t change the variable value.
- Pass by reference:
o Receive a pointer of variable.
o Java only supports Pass by value
- Deep copy:
o Duplicate everything (Collection: structure + elements).
- Shallow copy:
o Copy as little as possible. (Collection: only structure + shared elements).
- Implements Cloneable interface
- Override clone().
- Solution 1: Implements Cloneable interface for all elements.
- Solution 2: Serialization. (Serialize and deserialize).
- Extends Exception class.
- Use try-catch block, finally, “throws”, “throw” keywords to handle exceptions.
- Code in finally block always execute, use for cleaning code.
- Checked exception
o Invalid condition out of program’s control
o Check at compile-time
- Unchecked exeption
o Check at run-time
o Defects (bugs) in programs
- Error:
o Irrecoverable condition occurred at run-time
o Can’t repair at run-time
o Eg: OutOfMemory
- Exception:
o Caused by bad input
o Can handle
o Eg: NullPointerException, IndexOutOfBoundException…
- Proccess to convert object to byte-stream for transferring through network or writing to disk.
- Implement Serializable interface.
- Use writeObject() and readObject() to serialize and deserialize
- Purpose: to increase performance in some specific situations.
- Use readExternal() and writeExternal() to read from stream and write object into stream.
- Thread:
o Path of execution run on CPU, light weighted process
o Related threads share same data memory
o Have their own individual stacks
- Process:
o Collection of threads shared the same virtual memory
o Every process has its own data memory location
- Thread safe:
o A method that can run safely in multithread environment without any resource confliction.
- Synchronization:
o Assure resources (variable, object, method…) are not accessed by multiple threads at the same time
- Object – helps one thread communicate with another to synchronize their operation
[ Câu hỏi ] 54. What is deadlock? How do you detect them? Do you handle them? And finally, how do you prevent them from occurring?
- Lock: multiple processes access same resource at the same time
- Deadlock: two thread waiting another in a cycle
- Check log file
- Use VisualVM to analyze
- Heap:
o Stores class instance + arrays
o Shared memory
- Non-heap:
o ‘method area’
- Stack memory:
o Allocate automatic variable in function
- Use good Java best practices
- Consider static resources, set empty collections...
- Minimize the variable scopes
- Use tools to check before release applications
[ Câu hỏi ] 58. What will you do if your program has 500 Internal Server Error or OutOfMemoryException?
- 500 error:
o Check log file
o Reprocedure and debug
- OutOfMemory:
o Check log file
o Use tool to check memory leak
- Audit Module 2 - JavaCore
- [IInstall] Hướng dẫn cài đặt Intelliji
- [Java core] Override và Overload
- [Java core] Access Modifier
- [Java core] Principles of OOP
- [Java core] Abstract
- [Java core] Interface
- [Java core] Inheritance
- [Java core] Ép kiểu ngầm định và tường minh
- [Java core] Bộ nhớ Heap và Stack
- [Java core] Comparable và Comparator
- [Java core] Array và ArrayList
- [Java core] Generic
- [Java core] Cài đặt List (part 1)
- [Java core] Cài đặt List (part 2)
- [Java core] Cài đặt LinkedList
- [Java core] OOD
- [Java core] Clean code
- [Java core] Exception
- [Java core] Thread
- [Java core] IO file
- [Java core] Regular Expression
- [CaseStudy] Java core