1.
2. Introduce about Java Programming Language
- Java is object oriented programming language and it’s one of the most popular languages
in the world.
- It’s a high security language and often used to build the large software system like
ecommerce or banking.
- It’s famous with write one and run anywhere feature or in other word; it’s crossplatform
programming language.
3. Compare for me following three concepts JDK, JRE and JVM
- Talk about the different between three concepts:
JVM stands for Java Virtual Machine. It consider as a heart of whole application. It
transfer byte code into machine code and run the application. That’ the reason why
java can run in multiple plassform.
JRE is an evironment to execute Java applications. It consists of JVM and nesscessary
libraries like Java.Lang, Java.Utils. It can run applications because it has JVM but it
don’t have any complier so it just only execute complied code.
JDK stands for Java Development Kit. It consists of JRE and some development tool
like javac complier and debugging tool. JDK focuses on development purpose while
jre focuses on executing application.
4. Why Java is platform-independent?
- Because it have JVM. Firstly, java source will be compile into bytecode by Javac
compiler and after that JVM is responsible for interpreting bytecode into machine code
and this process don’t depend on platform/
5. Compare to stack memory and heap memories.
Stack
Heap
Java Heap Memory is the memory used Stack Memory is memory to store local variables in
at runtime to store Objects. Whenever functions and function calls at runtime in a java
anywhere in your program when you Thread. Local variables include: primitive types,
create an Object it will be stored in the reference types to objects in the heap (reference),
Heap (execute the new operator) declarations in functions, or arguments passed to the
function.
Objects stored in the heap are globally Stack memory is used only by one thread of
accessible execution.
Heap's management mechanism is more The operating mechanism is LIFO (Last-In-First-
complex. Heap is divided into 2 types Out)
Young-Generation, Old-Generation.
(Garbage Collection)
Heap memory lives from the start till the Stack memory is short-lived.
end of application execution.
6. Pass by value and Pass by references ?
- There are two kinds of passing machenism in Java :
+ Pass by value : It indicates that we pass primitive variables into a method as a
argument. And in that method, a copy parameter will be created and it copy the value of
arguments and it has it own memory space. All the change we did with copy parameter
will not affect to actual parameters outside the method.
+ Pass by reference: We pass a reference variable as a arguments into a method. And in
that method, a copy of that variable is created and it refer to the same location of the
object in heap memory. Any changes we made with the object inside that method will be
reflect to outside scope because actually, we have changed real object.
Pass a wrapper class into a method is pass by value or pass by reference ?
- It’s pass by value because wrapper class in Java is immutable. When you try to change
the value of a wrapper class, actually JVM create a new wrapper class with the value we
just changed
7. Can we override main method ?
- No, we can overload main method but we can’t override main method because the main
method is static method and static method can’t be overriden
8. Why main method is static method ?
- Because the main method is entry point of whole application. JVM have to execute
main() method to start application so it needs to be loaded into main memory so that
JVM can be find it and execute beacause the static method will be loaded into main
memory by class loader.
9. Can override the static method?
- No, we can’t override static methods because some following reason:
+ The static method is belong a class , not belong to any specific instance of class. It will
be share between all the instance of that class
+ Static method is bond at the compiler time and it’s loaded into main memory by class
loader before application execute while the overriding method is based on dynamic
binding at runtime. So we can’t override static method.
10. Advantage and disadvantage of OOP?
- There are many advantage of OOP like :
+ OOP can model complex system and make it more simplier
+ Reusibilty
+ It have high security level through encapsulation pillar
+ It’s easy to extend and maintain
- Disadvantages :
+ It more complicated to learn and unstand OOP
11. Compare Java and C
- Java is OOP languages so it have the features and advantages of OOP but it has compiler
time slower than C because Java need to interprete from bytecode to machine code while
C can compile directly source code to machine code.
12. The pillar of OOP.
- The first and I think the most important of OOP is abstraction. Abstraction is used to
model the relevant attributes and interactions between entites as classes and define an
abstract representation of a system. We can achive abstract pillar in OOP through abstract
class or interface.
- Encapsulation is when you wrap up your data under single unit. To achive encapsulation,
you have to:
+ Declare class field with private access modifier
+ Provide public getter and setter method to access and update the value of private
variable
- Inheritant: it allows subclass can reuse all the attributes and method from parent class
except private members. There are three kinds of inheritance in OOP. There are single
inheritance, multi level inheritance and hierarchical inheritance
- Polymorphism : It’s expressed by you can execute an action in various way. There are
two types of polymorphsim : compile-time polymorphism achive by overloading method
and runtime polymorphism achive by overloading method.
+ For example about runtime polymorphism : we start with three class animal class, dog
class and cat class. Animal class has abstract method is speak() and dog class and cat
class extends from animal class and it has its own speak() method. We create a reference
variable of animal class. And we can create an instance of Cat class or Dog class. The
speak() method is call will be belong to this instance. Or in other way, the calling method
is decided by JVM, not compiler, so it’s polymorphism at runtime
13. Compare to overloading and overriding method
Method Overloading Method Overriding
No.
1) Method overloading is used to increase the Method overriding is used to provide
readability of the program. the specific implementation of the
method that is already provided by
its super class.
2) Method overloading is performed within class. Method overriding occurs in two
classes that have IS-A (inheritance)
relationship.
3) In case of method overloading, parameter must be In case of method overriding,
different. parameter must be same.
4) Method overloading is the example of compile time Method overriding is the example of
polymorphism. run time polymorphism.
5) In java, method overloading can't be performed by Return type must be same or
changing return type of the method only. Return covariant in method overriding.
type can be same or different in method
overloading. But you must have to change the
parameter.
14. Does java support multiple inheritance?
- Java does not support multiple inheritance through class but we can achive that through
interface. In Java, a class can implement multiple inteface and an interface can extends
multiple interface. But when we use multiple inheritence. However, we need to carefully
with multiple inheritances because it can cause diamond problems. For example, we have
class A implement 2 interface is interface B and interface C. Both two interface have
method ed has the same name. So, there is an ambigous to know whether method from
interface B or interface C will be called.
15. Compare abstract class and interface
Interface
Abstract Class
Expression of abstraction < 100% 100% Abstraction
An abstract class can have abstract and non-abstract Java version < 8, Interface can only have
methods abstract method.
Java 8 version can add default and static
methods .
Java 9 version can add private methods
Abstract class does not support multiple inheritance Interface supports multiple inheritance
Abstract class can have final, non-final, static and Interface has only static final variables
non-static variables
The abstract keyword is used to declare an abstract The interface keyword is used to declare
class Interface
Use Abstract class when we can only complete a Use Interface when you want to build a
few standard functions (method/function) of the standard framework of functions
system, some of the remaining functions extend (method/function) that all
classes have to complete. These finished features modules/projects need. Modules must
are still usable as usual, these are general features. implement all defined functionality.
16. What is the main purpose of interface?
- Interface help us get abtraction and especially, it can be used to loose coupling between
modules . It’s related to a principle from SOLID principles( Dependecy Inversion) . The
high level module shouldn’t depend on low level module but both should be depend on
share abstraction and it’s interface. And with Spring Boot framework, there are two
mechanisms created to do that. This’s denpendency injection and Inversion of controller
17. Can we create object from abstract class and interface?
- We can not create object from abstract class or interface directly but we can create an
instance of class implement that interface or extend that abstract class. And because, we
have polymorphism pillar of OOP, so we can create reference variable has data type of
that interface or abstract class and create an instance for that variable is an instance of
class implement interface or extends from abstract method.
18. Enum in Java ?
- Enum like a special class in java and it’s represent for a group of constant.
19. Acess modifier in Java?
- There are four access modifier in Java
+ Private : We can only access private modifier within a class, not outside the class
+ Default : We can access default modifier within the same package
+ Protected : We can access protected modifier within the same package and different
package but it’s subclass
+ Public : We can access public modifier any where
20. When we should use private constructor?
- It allows to restrict instantiation of class. That means, we can prevent create an instance
of a class any place other than class itsself.
- There are some scenarios that we often use private contructor
+ Singletton Pattern
+ Delegate Constructor : allow us to pass parameter through several different constructor but
the constructor is private and it only can be use in class itsself
+ Create uninstantiable class or util class : it’s a class contain a collection of static method
and we can’t create an instance with this class.
+ Builder Pattern : It allow us to create an instance of complex object step by step rather than
having public contructor to create instance
Advantage of builder pattern :
+ Easy to read and understand
+ Prevent missing information with class has constructor with many parameters
+ Check constraint of field or validate information of the class
+ Create immutable class : We don’t have any setter method in the class, we can only
create instances of a class through static class builder inside that class.
21. Where are static member store ?
- Static member store in method area. Method area store all the class level data like the
name and the data type of field, methods and byte code have been compiled . It aslo store
the data of static member
Method area is a part of heap memories which is share among all the threads
22. Can place static variables inside a non-static method?
- Yes, of course. We can place static variables inside static method or non-static method but
inside a static method, we only can use static member, we can not use non-static method.
23. What is relationship between between static storage and mutiple thread?
- Static member store in the method area or in other words, static member stored in main
thread. So other thread want to get the static data will go to the main thread and method
area to get the data. So because, static member is share among all the threads so it’s not
thread-safe. We can use sychoronization mechanisms to protect the static member.
24. When should we use static block?
- A class can have multiple static blocks
- We can use the static block to initialize the value for static member in a class. Static block
always execute one time at the class loading time and before the main method is execute.
- We also can use static block for building singleton pattern or read the configuration file.
25. When static data is load ?
- Static data is loaded at the time of class loading and it will be loaded by Class Loader
26. Is it possible to call the static member through object?
- Yes, of course. We can call static member through object but it’s not recommened and
instead of that, we can call static member through class name because about definition,
static data’s belong to class, not bleong to any instance of that class
27. How to check an object is an instance of class ?
- We can use instaneof keyword to check that whether an object is an instance of class
28. Can we overload static method ?
- Yes, of course . we can overload static method but we can not override static method.
29. What is nested class, static class and inner class?
- Java allow declare a class inside another class. Class inside another class is called nested
class and class contain another class is called outer class. Nested class declare with static
keyword will be static class other it will be inner class.
- There different point between static class and inner class:
+ Static class can initialize without instance of outer class but inner class only exist if it was
wrapper by a outer class
+ Inner class can access both static and non static member from the outer class but static
class only can access the static member from outer class.
When we should use static class:
+ Nested class help program more readable, reusible and easy to maintain
+ We can model a part of a system or outer class into a nested class or we can create
some data structure inside outer class
30. Talk about private keyword in Java ?
- Final keyword in Java is used to restrict user action
- With final keywords, we have final variable, final method and final class
Final variable: In Java, a final variable is declared with final keyword and then you
won’t be able to change that variable. Or in a simple word, it’s a constant
Final method: A final method is declared with final keyword so that subclass will not
be able to overridde that method
Final class: when a class is declared with final keyword, it can not be inherited
31. How many way to initialize for final variable?
- There are three way to initialize value for final variable
+ We can initialize value for final variable at the declared time. And if you don’t initialize
for final variable at the declared time, it will be blank final variable. And we can initialize
for blank final variable in constructor or static block. And if you have many constructor
you have to initialize for final variable at all the constructor otherwise, it’ll be throw an
exception
32. How to create an immutable class in Java?
- An object is immutable if it cannot be change after initializing
- Because, immutable object can not be changed so every time we try to update the
information in that class, actually , we have create another object. But immutable objects
also have some advantages :
+ An immutable object is good for caching purpose, because you don’t worry about the value
change
+ Immutable class support thread safe in mutiple thread
- There are some conditions if we want to an immutable class:
+ Keep class is private so it can not be inherited
+ Make all the field of class is private so it can not be access directly
+ Don’t provide setter method for variable
+ Initialize all the field in a constructor performing deep copy
+ when you manipulate with the object, you return a copy object using construct rather
than return the actual object reference
- More convinent, you can use builder pattern to create immutable class.
33. What’s the purpose of equal method?
- In Java, equal method is used to compare two object based on some specific condition
like the value of field.
34. How about equal operator?
- With equal operator, you compare two object through it’s reference variables.
35. Relationship between equal method and hashCode method?
- The relationship between equal method and hashCode method like a contract.
+ If two objects are equal , they have to have the same hashCode
+ If two objects have the same hashCode, it maybe equal or not equal.
36. What is the purpose of overriden method and when we can use override method?
- In OOP, method overriding can understand as a subclass can provide specific
implementation of a method and that method already provide the implementation by
parent class.
- The condions of method overriding
+ Two method have to have the same name, the same return data type and data type of
parameter and it’s can not be final method or static method
37. When we log an instance of a class, it will be return a hasdcode. The meaning of that
hashCode and if we want to write the content of Object. How can you do that?
- The string return when we log an instance of a class is the name of class and hashcode of
that instance
- If you want to wirte the content of that instance, you have to override the toString
method?
38. What is exception and exception handling?
- An exception is an event that disrupt the nomarl follow of program.
- Exception handling is a mechnism that by which the normar follow of program will be
maintained if exeption happens
39. What is the difference between error and exception?
- Error in a program is irrecoverable. That means, when an error occurs, the progarm will
terminated immediately. There are several common error like OutOfMemmories error
and StackOverFollow error.
- Exception are on the other hand. That mean we can recorve them by exception handling
40. What are different types of exception?
- There are two types of exception:
+ Checked Exception: All the exception other than Runtime exceptions and error are knowns
as checked exception. It will be checked by compiler and thrown at the compiler time. There
are some common checked exception like SQL exception, FileNotFoundException and
IOException
+ Unchecked exception or Runtime exception: Unchecked exception won’t be checked by
compiler and it will be thrown at the runtime. There are some common runtime exception
like NullPointerException, ArrayIndexOutOfBoundException or ArithmethicException
41. How exception handling is done in Java?
- Try catch block is used for exception handling. If you think a block code can throw an
exception, you can surround them by try block, and catch block will catch them and
handle exception. Therefore, the normal follow of program will be maintained
42. How to handle mutiple exception together?
- We can write mutiple catch block and each catch block for one exception or you can
write single catch block with multiple exception and separate exception by pipe symbol.
- When we write multiple catch block, we have to follow bellow rules:
+ Handle the most specific exception at the first and move down for the most generic
exception
43. Try with resource ?
- Try with resource is the short form of try block and finally block. It will be close all the
resource automatically when try block is done.
44. What is different between throw and throws keywords
- Throw is a keyword which is used to explicity throw an exception in the program inside a
method or inside a block code whereas throws keyword is used to with methd signature
to declared an exception may be occur in that method. And when we use throws keyword,
we notify for the caller method have to handler that exception or transfer that exception
down to previous caller
- Throw keyword follow by an instance of exception class while throws keyword folow by
Exception class.
- You can only throw one exception at a time but you can declared mutiple exception with
throws keywords.
- Using throw keyword, only unchecked exception can be propagated whereas using
throws keywords, both checked and unchecked exception can be propagated.
What is the advantage of throws keywords :
+ It help the code more clearly and help the developers can handler exception without
missing the exception
+ It support checked exception propagation and throw don’t support checked
exception propagation
45. Tell about exception handling method overriding?
- Exception handling method overriding have to follow by some rules:
+ If the parent class method does not declared any exception, the child class override that
method can’t not declared checked exception. It only can declared uncheked exception.
+ If the parent class method declared an exception, the child class override from that can do
some following action like:
Can declared no exception
Can declared same exception
Can declared narrower exception than parent class method
46. How to make your own custom exception?
- In Java, you can easily create your own custom exception class which is extends from
Exception class and if you want to create your own custom runtime exception class, you
can extends from RuntimeException class.
47. What happens when you throw exception from finally block?
- When an exception is thrown from finally block , it will take precedence over exception
thrown from try catch block
48. How to compare two String object?
- If you want to compare two String object based on some specific attributes , you have to
override the equal() method.
- If you compare two object by the equal operator, it will compare by reference variable.
49. Why String is immutable ?
- String is immutable because of somes below reasons
+ String Pool : String pool is possible if only String is immutable.
If String is mutable , changing the String with one references will lead to change with all
references
+ Security : Spring parameter often used in network connection, database URL , username
and password. Because String is immutable, this value cannot be changed, otherwise hacker
can’t change the important information like username and password, they will take the
program permission.
+ Muitithread: Because String is immutable, so it’s safe for multiple thread.
+ Caching : String is often used in caching purpose and beacause String is immutable, we
don’t worry ablout that value can be changed.
50. What is String pool?
- String pool is special storage area in heap memories. It is used to improved the
performance of system. It like a cache memories contain all the String literals . When we
create an String Object with String literal. At first, it will be check in the string pool
whether that String literal have exist in String pool or not. If it have exist, it will be return
that String literal otherwise it will be create a new one and return it.
51. What is String Builder and String Buffer ?
- Both String Builder and String Buffer are mutable. That mean use can change the value
of String object.
- The difference between String Buffer and String Builder is String Builder using
asynchronous mechanisms, so it have the faster speed but not safe for multiple thread. On
the other hand, String Buffer using synchronus mechanism so it have slower speed
compare to Spring Bulder but it is thread safe
52. String and String builder, which is faster?
- String Builder is more faster beacause it change the value of its own object while String
is immutable and when we manipulate with String object , actually, we create another
String object
53. What is the difference between
54. Tell about ArrayList?
- ArrayList in Java is used as a dynamic array to store elements.
- Can contain duplicate elements
- Maintain the orders of elements added
- Asynchornous mechanisms
- Speed of accessing elements is very fast because it’s stored data by index
- The operator of adding or removing elements is very slow because a lots of shiffing is
required
55. Tell about properties of LinkedList?
- It use Double LinkedList Data Structure to store elements
- Can contain duplicate elements
- Maintain the order of elements added
- Asynchronous mechanisms
- The speed of accessing elements is more slower than ArrayList because it have to
traverse through all the elements of linkedlist.
56. What is the difference between ArrayList and Array?
- Array has fixed size, so the size of array can not be change after initializing . On the
other hand, array list has dynamic size , so it will be automatically increase when we add
an element and exceed the size of that ArrayList.
- Array can hold both primitive data type and reference data type but ArrayList only can
hold reference data type and if you want to store primitive data type in ArrayList, you
have to use wrapper class
- ArrayList provide many utility method to help manipulating with collection more easier
57. Compare ArrayList and Linked List
LinkedList
ArrayList
ArrayList uses dynamic array to store LinkedList uses a linked list (Doubly Linked List) to
elements. store elements.
ArrayList is an index-based data The elements in the LinkedList are called nodes, each
structure, where each element is node needs to store three pieces of information: the
associated with an index. reference of the previous element, the value of the
element, and a reference to the next element.
Adding and removing elements with Adding and removing elements with LinkedList is
ArrayList is slow because it uses arrays faster than ArrayList. Because it doesn't need to
internally. Because after adding or rearrange the elements after adding or removing. It
removing elements need reordering. simply updates the reference to the element before and
after it.
Retrieving elements in ArrayList is Retrieving elements in LinkedList is much slower
faster than LinkedList. Because the than in ArrayList. Because, it has to iterate through
elements in the ArrayList are stored the elements from first to last.
based on the index (index).
ArrayList can only act as a list because LinkedList can act as an ArrayList, stack (queue),
it only implements the List interface. queue, Singly Linked List and Doubly Linked List
because it implements List and Deque interfaces.
ArrayList is better at storing and LinkedList is better at data manipulation
retrieving data (get). (add/remove).
58. Tell about vector?
- Can contain duplicated elements
- It maintain the ordered of elements added
- The most different between vector and arrayList is vector is synchornous and thread-safe
while ArrayList is asynchornous and is not thread safe in multiple thread
59. Talk about Queue Interface?
- There are two class implementations Queue interface : LinkedList and Priority Queue
Priority Queue using Heap data structure to store elementsand it sort all the elements
through Comparable or Comparator interface
60. Java support pass by value or pass by reference?
- Java only support pass by value mechanism. C++ programming language support both
pass by value, pass by reference, and pass by pointer.
- In Java, when use pass a reference variable into a method, actually you pass value of that
variable to the method, and inside the method, a copy of that variable is created and it’s
point to the same object in heap memories.
What is the difference between pass by variable and pass by pointer in C++
- With pass by reference, another variable is created inside the method and it point to the
same memory like original variable but we can’t point that variable into another
memories during method operation.
- With pass by pointer, it like pass a reference variable in Java, it pass the memory addess
of variable into a method, at in that method, actually,we have changed the value of that
memory.
61. What is the difference between List and Set?
- There are two main point different between Set and List
+ List can contain duplicate elements but Set can not contain duplicate elements
+ List maintain the orders of elements added but Set don’t maintain the orders of elements
added except LinkedHashSet.
62. Why HashSet can’t contain duplicated elements and can’t main the order of elements?
- HashSet can't contain duplicated elements because inside HashSet, it use HashMap to
store the elements and actually, the elements of HashSet is the key of HashMap in key,
value pair, and the value of that key is a dumpy object. And because the key is unique,
therefore HashSet only contains unique elements.
- Hash Set can not maintain the orders of elements because it use the hash algorithm to
implement and in the hash Map, the key will be caculate by hash function and it return
the hashCode, and it store element in a bucket base on the hash code. So, it can not
maintain the order of elements added.
63. Compare Hash Set, LinkedHashSet and TreeSet
HashSet LinkedHashSet TreeSet
How to work? uses HashMap uses LinkedHashMap uses TreeMap internally to
internally to store internally to store store elements.
elements. elements.
Order Of HashSet does not LinkedHashSet TreeSet maintains the order
Elements maintain any order maintains the insertion of elements according to the
in which elements order of the elements. provided comparator
were added. Elements are stored in (Comparator). If no
the correct order in comparator is provided, the
which they were elements will be placed in
inserted. their natural ascending
order.
Performance HashSet gives LinkedHashSet falls TreeSet gives lower
better performance between HashSet and performance than HashSet
than TreeSet. Its performance and LinkedHashSet because
LinkedHashSet is almost similar to it has to sort the elements
and TreeSet. HashSet. But slightly after each insertion and
slower as it also removal.
maintains an internal
LinkedList to maintain
the insertion sequence of
the elements.
Insertion, O(1) performance LinkedHashSet also TreeSet gives O(log(n))
Removal and for inserting, offers O(1) performance performance for insert,
Retrieval removing, and for inserting, removing, remove, and retrieve
element retrieving and retrieving elements. elements.
operations elements.
Compare HashSet uses LinkedHashSet also uses TreeSet uses compare() or
elements equals() and equals() and hashCode() compareTo() methods to
hashCode() methods to compare compare elements and thus
methods to elements. remove possible duplicates.
compare elements
and thus remove
possible
duplicates.
Null element HashSet allows up LinkedHashSet also TreeSet does not allow null
to one null allows up to one null elements. If you try to insert
element. element. null into the TreeSet
element, it throws
NullPointerException.
64. You add an element already exist in SET. How JVM do that? Equal() method and
HashCode() method, which one call first.
- When you add an element into SET, first, it will hashCode() the value and hashCode()
value is the bucket identifier. If there is no bucket exist with the same hashCode, it will
create new one and store elements in that, otherwise it will traverse through that bucket to
check whether there is no one element with the same key exist in that bucket by equal
method or not, if no one exist, it will create a new one otherwise, it will ignore that
element
65. What is the difference between HashMap, TreeMap, LinkedHashMap
66. Compare to HashMap and ConcurrentHashMap
- HashMap use asynchronous mechanism and it’s not thread safe
- ConcurrentHashMap use synchonous mechansm and it’s safe for multiple thread
- HashMap allow store one null key and many null value, but the ConcurrentHashMap
don’t support store null key or null value
67. Compare Hashtable and HashMap
- It support synchronization mechanism , and it’s safe for multiple thread unlike the
HashMap
- HashTable don’t support stored any null key and null value while the HashMap supports
store one null key and multiple null value
About performance, ConcurrentHashMap will better than HashTable beacause
ConcurrentHashMap using multiple lock for each bucket while Hashtable only use
one lock for all buckets inside it
68. What is thread pool?
- Thread pool is a software design pattern. And it use a collection of worker threads to
execute asynchronous task. It help save the resourece and improvement of program.
- When a thread complete a task, it’ll return to thread pool and thread pool will assign it for
another task. We have to do that because opening and closing a thread take a lot of
resuorce and it make program slower. We have to take advantage of existing thread
instead of create a new one
69. Compare Comparable and Comparator Inteface?
- For object type, if you want to sort the elements in a collection, you need to provide a
comparator. There are tow way to provide comparator for collection : Comparable and
Comparator interface.
Here is some different point between Comparable and Comparator interface:
Comparator
Comparable
Comparable provides only one way of Comparable provides a variety of
comparison. That is, we can only compare by id comparisons. That is, we can sort based on
or name, or age, ... many factors such as id, name, age, ...
Comparable modifies the original class, that is, Comparator does not change the original
the comparable object's class must modify and class. We can create a new class that
implement the Comparable Interface to implements the Comparator Interface to
implement. implement.
Comparable provides compareTo() method to Comparator provides compare() method to
compare two elements. compare two elements.
Any list can be sorted using the Any list can be sorted using the
Collections.sort(List) method . Collections.sort(List, Comparator) method
70. Which Java version, you often use? Why do you use it?
- I often use Java 8. Java 8 is recommended by many people beacause it has many
important improvement of Java programming language and it also have very large user
community.
71. Tell me about some improvement of Java 8 compare to previous version?
- There are many improvement of Java 8 compare to previous version such as:
+ Default method, static method
+ Fuctional Interface
+ Lamda Expression
+ Method Reference
+ Optional
+ Stream API
72. What is default method and static method?
Default method
- The problem before Java 8 version : Class implement interface have to provide
implementaion for all the method defined in that interface even though it don’t need to
use all of them. You can imagine, a very large interface with hundred of methods, this
problem can become very big challenge
- Default method is created to overcome this challenge. Default method is a method have
default implementaion define in the interface. And class implement implement that
interface don’t have to provide their own implementaiton if it is’n necessary
Static method
- Static method is the same with dafault method and it can’t be overriden, from the class
implement the interface.
- It is used to create utility method inside a interface instead of create another utility class.
73. What is functional programming?
- Functional programming is a programming style base on perform function and it don’t
change the value of variable. With functional programming, a function can become the
input for another dunction. It increases the usablitiy and it paralel programming because
the value of variable can not be change
74. Compare OOP and funtional programming?
- With the functional programming, funtion is a the main unit of operation while with OOP,
it’s object
- Fucntion programming support both data and behavior abstraction while OOP only
support data abstraction
- Funcitonal Programming have the better performance than OOP on processing big data
- Functional programming does not change the value of outside variable but with OOP, it
change the value of variable
- With functional programming , we can pass a functions as a argument into another
function but with OOP, it don’t support that.
75. What is functional interface?
- It’s created in Java 8 to support functional programming
- An functional Interface is an interface with only one abstract method but it can have
multiple static method or default method.
- We can use lamda expression to create an instance of interface.
- There are some predefined functional iterface in Java 8:
Predicate<T>:
+ Represent predicate of a one argument
+ It have one abstract method name Test have one parameter and return a boolean
value.
+ It’s used in filter or removeIf method in Stream API
Supplier<T>
+ It represent for supplier of result
+ It have one abstract method named get() . That method don’t have any parameter
but it returns a generic value.
+ It’s used in generate metho in Stream API or apply for factory design pattern
Fucntion<T,R>
+ It represents of a function that accepts one argument and produces a result
+ It have one abstract method name apply. That method have one parameter and it’s
return a value.
+ It’s used in map() method in Stream API
Consumer<T>
+ It represent for an operation that acept single input and return no value
+ It have one abstract method named accept have one parameter but return no value
+ It’s often used in ForEach method in Stream API.
76. What is lamda expression and purpose of lamda expression
- Lamda expression is a function with no name, no class, no scope and no return type
declaration. Lamda expression can be defined as an anonymous function, it can be
passed like argument for onother function.
- Why we use lamda expression?
+ It provide an implementation of functional interface and support functional
programming.
+ Help developer write less code and the code can be more readable
+ It’s used in Stream API
- There is a noticable point in lamda expression is that lamda expression cannot change the
value of outside variable
77. What is method reference ?
- Method reference is a spectial type of lamda expression , it’s short form of lamda
expression and it’s used to create lamda expression by referencing to existing methods.
- There are some type of method reference :
+ Reference to static method
+ Reference to instance method of particular object
+ Reference to instance method of an arbitrary object of a particular type.
+ Refernce to constructor of a Class
78. Tell about ForEach loop in Java 8?
- ForEach Loop provide for programmer with a new, concise and interesting way to iterator
over collecction
- It will use Consumer interface with a lamda expression to implement abstract method to
do some action with each element for iterator loop.
- With the Map, forEach loop will use BiConsumer to iterate over collection
79. Tell me about Stream API?
- In java 8, Stream API is release to help programmer caculate with collection more
quickly, concise and have better performance with parallel.
- Stream is not data structure, stream is an immutable object. All the aggregation operation
that we perform with collection, actually, we don’t change the data source. It only return
a new stream.
- All the operation in Stream API are lazy. That means it only perform when necessary or
in other words, it only perform when it reach terminate operation like collect() ,
forEach(),..
- We can not use index to access the element in stream.
80. What is Stream Pipeline?
- To perform a sequence of operation over the elements of data source, the Stream API
need to three part : source, intermidiate operation and terminal operation.
81. What is lazy invocation in Stream API?
- Lazy invocation in Stream API can understand as all the immediate operation will not be
perfrom until the stream don’t reach to one terminal operation yet. It help improve
performance of program
82. Compare map() and platMap() method in Stream API?
- Map is used to transform an element of a Stream API into new element by apply a
function into it.
- The return of map function is a new Stream have the number of element is the same with
original element
- FlatMap is used to transform an element of a Stream into new Stream and merge all the
sub stream into a Single Stream
- The retun of flat map is a new stream have the number of element greater than original
Stream
83. Tell about Optional in Java 8?
- Optional in Java 8 is a container class which is used to contain a single value and that
value may be null or not null
- NullPointerException is one of the most common exception that programmer take so
optional will remind the developer that that variable may be null or not, please carefully.
84. Compare finalize object and finalize system?
- Finalize() method of object is used to do some action before that object is collected by
garbage collector
- Finallization() method of system is used to required JVM rull all the finallize() method
that haven’t been called in the class have finallize() method
But finallize method don’t recommend and instead of that, we can use try with
resoure or implments AutoClosable interface to dispose resource safely
85. Tell me about ACID pillar in OOP?
- Automicity: It state that all the operation in a transaciton have to execute successfully and
all the the change will be commit and synchronize with database otherwise, if there is
only one operation execute failure, all the change will be ignore and it will roll back at
the state before starting transaction
- Consistency: All the operation in a transaction have to guarantee all the contraint or
unique with the database
- Isolataion : With isolation pillar, we can have multiple transaction execute concurently
but all of them is independent with each other. That means a transaction can not see the
change of another transaction util that transaction is commit
- Durability: It indicates that when a transaction is commit, all the change will be apply for
database even though the system is failure like we loose the power. When the server is
restart, it will recover and apply all the change for database
86. Tell me about isolation level in transaction?
- There are four levels of isolation in a transaction:
+ Read uncommited : It states that a transaction cannot read data from another transaction
even though that transaction is not commited
+ Read commited : It states that a transaction can read data from another transaction only if
that transaction is committed but in this level , a transaction can still read or write data from
a recored that is updated because of another transaction. It’s a phantom problem.
+ Repeatable read : It almost the same with read commited but in this level, a transaction can
read or write data into a record in the case that record is updating by another transaction.
Beacause, when a transaction update a record, it will be hold a write lock for that record and
another transaction want to read or write data into that record have to wait until that write
lock is release.
+ Serializable: With this level , when a transaction read or write a record, it will be lock that
record. It more safely but it can lead to performance problems.
87. Tell me about modules in Spring Boot?
- I know some modules in Spring Boot like Starter, Boot AutoConfigure, Actuaror, Test, …
+ Starter is a very important module in Spring Boot. It helps simply development process by
providing a set of default dependencies, configuration for each type of application. For
example, in the pom.xml file . I have dependencies springboot-starter-data-jpa, it will
container all the dependencies related to JPA and Hibernate, we don’t have to import it
separately.
+ Boot AutoConfigure is also a very important module in Spring Boot architecture. It support
create a bean automatically. With Spring Boot, we can create a bean through annotation like
@Component,@Bean. It help speed up developing process of application.
+ Actuator: Actuator is a module help manage and monitor the state of Spring Boot
application. With Actuator module, we can get some information about applications like
healths , end point mapping or about the beans.
+ Test Module provide many annotations support testing purpose in Java Spring Boot
application