Professional Documents
Culture Documents
All
All
@ExceptionHandler(EntityNotFoundException.class) -- methodlevel
@ControllerAdvice -- class level
HandlerExceptionResolver (i) -> resolveException()
Q) what is swagger ?
1. It is the most popular API documentation format for RESTful Web Services.
2. It builds around the OpenAPI Specification that helps developers to
design, build, document, and consume RESTful APIs.
3. It provides both JSON and UI support.
4. JSON can be used as a machine-readable format, and Swagger-UI is for
visual display, which is easy for humans to understand by just browsing the API.
documentation.
swagger-ui.html
localhost:8080/V2/api-docs
localhost:8080/swagger-ui.html
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2called)
.select().apis(RequestHandlerSelectors.basePackage("guru.springfram
ework.controllers"))
.paths(regex("/product.*"))
.build();
}
}
Q)OOPS features ?
data hiding -> Our internal data should not go out directly.By using private
modifier we can impletememt data hiding.
abstraction -> Hiding internal Implmentaion and highliting set of services is
abstraction.
Abstract classes/Interfces we can implement abstraction.
encapsulation --> (datahiding + abstraction) binding of data and
corresponding methods into single unit.provide security
inheritance -> reusablity
polymarphism -> same method name with different forms -->
overloading ,method hiding (compile time)
Benefits of Encapsulation
1.The fields of a class can be made read-only or write-only.
2.A class can have total control over what is stored in its fields.
1.Make your class final, so that no other classes can extend it.
2.Make all your fields final, so that they’re initialized only once inside
the constructor and never modified afterward.
3.Don’t expose setter methods.
4.When exposing methods which modify the state of the class, you must always
return a new instance of the class.
5.Make sure to always return a clone copy of the field and never return the
real object instance.
Instance Methods
Static Methods
----------------
---------------
1.require object to call method
1.call directly with class name
2.no keyword is required
2.use static keyword for method
3. from Static methods can’t access instance
3.FROM Instance method can access static variables and static methods
directly.
methods and instance variables directly
String :If the content is fixed and won't change frequently then we should go
for String.
StringBuffer:If the content will change frequently but Thread safety is
required then we should go for StringBuffer.
StringBuilder: If the content will change frequently and Thread safety is not
required then we should go for StringBuilder.
Q) Exeption vs Overrding :
While overriding if the child class method throws any checked exception
compulsory the parent class method should throw the same checked exception or
parent otherwise we will get compile time error.
IOEException/Exception
IOEException
Q) Exeption vs Constructor :
IOEException
IOEException/Exception
With in the static block if we are trying to read any variable then that read
is considered as "direct read" If we are calling a method , and with in the method
if we are trying to read a method , that read is called Indirect read
If a variable is in RIWO state then we can't perform read operation directly
otherwise we will get compile time error saying " illegal forward reference
Q)Java 14 features ?
Switch Expressions (Standard)
Records (Preview)
Text Blocks (second preview)
Patteren Matchig for instance of (preview)
Q)Java SE 16 Features ?
Records
Sealed Classes (second Preview)
Patteren matching for instanceof
Q)Daemon Threads?
The Threads which are executing in the background are called daemon Threads.
The main objective of daemon Threads is to provide support for non-daemon Threads
like main Thread.
Q)Executor FrameWork?
*)Creating a New Thread for Every Job May Create Performance and Memory Problems.
*)Passing a Callable to the submit method is possible to get a Future object and
use it to retrieve the result of the asynchronous computation
How to Create an Executor:
// Creates a single thread ExecutorService
ExecutorService singleExecutorService = Executors.newSingleThreadExecutor();
Q)Comparable vs Comparator ?
comparable
Comparator
----------
----------
1. Present in java.lang Package 1.Present in
java.util Package.
2.It is Meant for Default Natural Sorting 2.It is
Meant for Customized Sorting Order.
3. Defines Only One Method compareTo(). 3.Defines 2
Methods compare() and equals().
4. All Wrapper Classes and String Class 4.The
Only implemented Classes of Comparator are Collator.
and RuleBaseCollator.
Q) Log Levels: ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.
Q)Composition vs Aggregation ?
Composition:
Without existing container object if there is no chance of existing
contained objects then
the relationship between container object and contained object is
called composition
which is a strong association.
Aggregation :
Without existing container object if there is a chance of existing
contained objects such
type of relationship is called aggregation. In aggregation objects have
weak association.
Q) HashMap updates in java8 ?
1.Before Java 8, the performance of the HashMap was poor due to the hash
collision.
2.The retrieval time of elements from HashMap increases from O(1) to O(n).
3.Java 8 hash elements use balanced tree instead of linked lists after a
certain threshold is reached.
4.HashMap starts with storing Entry objects in a LinkedList but after the
number of items in a hash becomes larger than a certain threshold.
5.The hash will change from using a linked list to a balanced tree.
Java supports atomic classes that support lock free, thread safe programming
on single variables. These classes are defined in java.util.concurrent.atomic
package. Some of the key classes include AtomicBoolean, AtomicInteger, AtomicLong,
AtomicIntegerArray, AtomicLongArray and AtomicReference.
Some of methods in AtomicInteger : addAndGet(),compareAndSet(),
decrementAndGet(), doubleValue(), get(), getAndDecrement(),
Time slice is introduced in operating system for scheduling processes. With the
help of Time slice factor short time running process will get a chance to execute.
A task executes for a predefined slice of time and then re-enter the pool of ready
tasks
Thread scheduler in java is the part of the JVM that decides which thread
should run.
Only one thread at a time can run in a single process.
The thread scheduler mainly uses preemptive or time slicing scheduling to
schedule the threads.
1.Volatile is the modifier applicable only for variables but not for classes
and methods.
2.If the value of variable keeps on changing such type of variables we have
to declare with volatile modifier.
3.If a variable declared as volatile then for every thread a separate local
copy will be created by the jvm, all intermediate modifications
performed by the thread will takes place in the local copy instead of master
copy.
4. Once the value got finalized before terminating the thread that final
value will be udated in master copy.
5. The main advantage of volatile modifier is we can resolve data
inconsistency problems, but creating and maintaining a separate copy for every
thread
increases complexity of the Programming and effects performance of the
system.
6.Hence if there is no specific requirement never recommended to use volatile
modifier and it's almost outdated.
6.Volatile means the value keep on changing where as final means the value
never changes hence final volatile combination is illegal for variables.
Yield()
<------------------------
start() TS allocate CPU
run() completes
new/born -----------> Ready/Runnable -----------------------------
>Running-------------------------------->Dead
|
|
|
| sleep()
| |
| 1.sleep time expire
Sleep state
| | 2.sleep thread got
inturrepted |
|
----------------------------------------------|
|
|
| |
|
| wait()
| |
| 1.if wait thread got notification
Waitng state
| 2.Wait time expires
|
-----------------------------------------------
--------|
3.if waiting thread got
interrupted.
| |
| |
|
| Suspend()
| |
|
Suspended state
| resume()
|
-----------------------------------------------
--------|