// API callback
related_results_labels_thumbs({"version":"1.0","encoding":"UTF-8","feed":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/","xmlns$blogger":"http://schemas.google.com/blogger/2008","xmlns$georss":"http://www.georss.org/georss","xmlns$gd":"http://schemas.google.com/g/2005","xmlns$thr":"http://purl.org/syndication/thread/1.0","id":{"$t":"tag:blogger.com,1999:blog-1902220484592789830"},"updated":{"$t":"2024-11-06T08:33:41.552+05:30"},"category":[{"term":"java"},{"term":"java interview"},{"term":"Microservice"},{"term":"design pattern"},{"term":"spring"},{"term":"building microservices"},{"term":"java8"},{"term":"java interview questions"},{"term":"java microservices"},{"term":"cleancodetips"},{"term":"youtube"},{"term":"drools"},{"term":"drools decision table"},{"term":"drools rule engine"},{"term":"drools rule engine tutorial"},{"term":"drools syntax"},{"term":"drools tutorial"},{"term":"drools workbench"},{"term":"rule engine java"},{"term":"java8 feature"},{"term":"java8feature"},{"term":"Spring data"},{"term":"code smell"},{"term":"java design pattern"},{"term":"spring boot"},{"term":"thread"},{"term":"Automated Microservices"},{"term":"DDD"},{"term":"design pattern in java"},{"term":"jpa"},{"term":"spring cloud tutorial"},{"term":"Devops"},{"term":"Domain driven design"},{"term":"GOF"},{"term":"Spring data jpa"},{"term":"design pattern java"},{"term":"design patterns in java"},{"term":"j2ee"},{"term":"java interview questions for experienced"},{"term":"java pattern"},{"term":"microservice tutorial"},{"term":"microservices tutorial"},{"term":"mvc"},{"term":"spring mvc"},{"term":"Big data"},{"term":"Inheritance forest"},{"term":"Microservices example"},{"term":"Repository"},{"term":"Restful API"},{"term":"Spring Repository"},{"term":"Spring tutorial"},{"term":"SpringAsync"},{"term":"Springdata overview"},{"term":"anti pattern"},{"term":"databse"},{"term":"design patterns in java with realtime examples"},{"term":"design principles in java"},{"term":"devops practices"},{"term":"interface"},{"term":"interview"},{"term":"interview question on spring scope"},{"term":"java 5"},{"term":"java 6"},{"term":"java 8 stream"},{"term":"java jdk 8"},{"term":"java refactor"},{"term":"jdk 1.8"},{"term":"jenkins.Microservice with jenkins"},{"term":"jxls"},{"term":"lambda"},{"term":"microservice framework java"},{"term":"microservices architecture"},{"term":"microservices discovery"},{"term":"microservices with spring boot example"},{"term":"microservices-devops together"},{"term":"optional"},{"term":"overloading"},{"term":"refactoring code"},{"term":"singleton"},{"term":"spring cloud services"},{"term":"spring context"},{"term":"@Query"},{"term":"@Transactional"},{"term":"AOP"},{"term":"Adapter pattern"},{"term":"Angular4"},{"term":"BMT"},{"term":"CI"},{"term":"CMT"},{"term":"CQRS"},{"term":"Callback"},{"term":"Config server"},{"term":"DI"},{"term":"Decorator"},{"term":"Decorator pattern"},{"term":"Dependency injection"},{"term":"Deployment"},{"term":"Design decision"},{"term":"Docker"},{"term":"Docker Architechture"},{"term":"Docker Registry"},{"term":"Docker image"},{"term":"DocumentDB"},{"term":"Ejb"},{"term":"Encapsulation.Domain driven design"},{"term":"Event Source"},{"term":"Event driven architecture"},{"term":"Fileupload"},{"term":"Fileupload using Mock"},{"term":"Fileupload using junit"},{"term":"Flyweight"},{"term":"Flyweight pattern"},{"term":"GOF Flyweight"},{"term":"GOF singleton"},{"term":"Hashmap"},{"term":"Hexagonal"},{"term":"Hexagonal architechture"},{"term":"Hollywood Principle"},{"term":"IOC"},{"term":"Inheritance"},{"term":"Interview question on Design pattern"},{"term":"JCS"},{"term":"JDBC"},{"term":"JDBC API"},{"term":"JDBC Connection"},{"term":"JDBC Drivers"},{"term":"Java principles"},{"term":"Junit"},{"term":"MongoDB"},{"term":"PropertyPlaceholderConfigurer"},{"term":"Scalable"},{"term":"Shotgun Surgery"},{"term":"Sprig Mock"},{"term":"Spring How to tutorials"},{"term":"Spring Mock Mvc"},{"term":"Spring application context"},{"term":"Spring cloud config server"},{"term":"Spring with example"},{"term":"StactOverfloError"},{"term":"Tell Don't Ask"},{"term":"Transaction"},{"term":"Transaction \u0026 Checked Exception"},{"term":"Transaction \u0026 Runtime Exception"},{"term":"Type erasure"},{"term":"abstractclass"},{"term":"access modifiers in java with example program"},{"term":"antipattern"},{"term":"archetype in maven"},{"term":"archetype maven  own archetype"},{"term":"bad code"},{"term":"behavioral pattern"},{"term":"benifits of getter setter"},{"term":"breach encapsulation"},{"term":"callback method"},{"term":"can we overload main method"},{"term":"cant resolve property."},{"term":"casting example"},{"term":"chef"},{"term":"closure"},{"term":"code smell examples"},{"term":"config server tutorial"},{"term":"continuous delivery"},{"term":"custom archetype"},{"term":"declarative transaction"},{"term":"decorator pattern java"},{"term":"define devops"},{"term":"design pattern in java with example"},{"term":"devops concepts"},{"term":"devops continuous integration"},{"term":"devops definition"},{"term":"devops for beginners.agile"},{"term":"downcasting"},{"term":"download spring framework jars for eclipse spring installation"},{"term":"enum"},{"term":"etends"},{"term":"eureka server"},{"term":"event sourcing"},{"term":"event-driven"},{"term":"event-driven architechture"},{"term":"explicit casting"},{"term":"extensible"},{"term":"extention"},{"term":"factory method pattern.difference factory method and simple factory"},{"term":"factory pattern"},{"term":"fail-fast"},{"term":"feign client tutorial"},{"term":"fileupload mock"},{"term":"fluent api"},{"term":"functional"},{"term":"functional program"},{"term":"functional programming"},{"term":"functional programming for beginners"},{"term":"functional programming in java"},{"term":"functional programming vs oop"},{"term":"generics in java interview questions"},{"term":"generics interview questions Type Inference"},{"term":"getter"},{"term":"getter\/setter"},{"term":"getterandsetter"},{"term":"gof pattern"},{"term":"how to achieve multiple inheritance in java"},{"term":"inheritence"},{"term":"interface and Enum"},{"term":"interview question on spring data"},{"term":"java 1.8"},{"term":"java 1.8 api"},{"term":"java access modifiers"},{"term":"java based configuration"},{"term":"java casting"},{"term":"java constructor"},{"term":"java core pattern"},{"term":"java design patterns tutorial"},{"term":"java encapsulation"},{"term":"java final constructor"},{"term":"java functional"},{"term":"java functional programming"},{"term":"java microservices."},{"term":"java platform independent"},{"term":"java regex"},{"term":"java regex special characters"},{"term":"java regex tester"},{"term":"java regular expression tester"},{"term":"javaonfly"},{"term":"jdk1.8"},{"term":"jenkins devops"},{"term":"jre 1.8"},{"term":"jvm platform independent"},{"term":"lambda expression"},{"term":"learn devops"},{"term":"load multiple property file"},{"term":"main method"},{"term":"map"},{"term":"maven"},{"term":"maven archetype quickstart"},{"term":"maven placeholder"},{"term":"microservices architecture tutorial"},{"term":"microservices communication"},{"term":"microservices framework"},{"term":"microservices framework java"},{"term":"microservices in java"},{"term":"microservices java"},{"term":"mongo unwind"},{"term":"multiple inheritance in java"},{"term":"multiple inheritance in java example"},{"term":"multiple inheritance in java using interface"},{"term":"netflix microservices"},{"term":"onion architectural"},{"term":"opcodes"},{"term":"open closed principle example"},{"term":"orthogonal architecture"},{"term":"overload main"},{"term":"overloading in java"},{"term":"overloading meaning"},{"term":"parallel inheritance"},{"term":"placeholder"},{"term":"platform independent in java"},{"term":"platform independent java"},{"term":"port"},{"term":"port and hub"},{"term":"pring cloud netflix"},{"term":"program to an interface"},{"term":"properties"},{"term":"properties resolving"},{"term":"property file in multiple spring context"},{"term":"protected java"},{"term":"protected keyword in java"},{"term":"puppet"},{"term":"query annotation"},{"term":"queryDsl"},{"term":"robomongo"},{"term":"runtime"},{"term":"runtime behavior"},{"term":"salience in drools"},{"term":"scaling"},{"term":"scopes"},{"term":"server"},{"term":"server pool"},{"term":"service registry"},{"term":"servlet"},{"term":"servlet 3"},{"term":"setter"},{"term":"shotgun"},{"term":"simple factory"},{"term":"skeletal implementation"},{"term":"skeletal interface"},{"term":"spring boot cloud"},{"term":"spring boot microservices"},{"term":"spring cloud config server. spring cloud services"},{"term":"spring cloud netflix"},{"term":"spring cloud service"},{"term":"spring daya jpa"},{"term":"spring eclipse tutorial"},{"term":"spring framework download"},{"term":"spring framework eclipse"},{"term":"spring framework in java"},{"term":"spring mvc example"},{"term":"spring mvc java based"},{"term":"spring scope"},{"term":"spring singleton"},{"term":"spring transaction"},{"term":"spring-cloud-starter-feign"},{"term":"spring4"},{"term":"stackOverFlow"},{"term":"strategy"},{"term":"strategy design pattern in java"},{"term":"strategy pattern"},{"term":"strategy pattern in java"},{"term":"stream api"},{"term":"structural pattern"},{"term":"toString()"},{"term":"type safe query"},{"term":"upcasting"},{"term":"what is devops"},{"term":"what is functional programming"},{"term":"what is microservices"},{"term":"workflow engine"}],"title":{"type":"text","$t":"Make \u0026amp; Know Java"},"subtitle":{"type":"html","$t":"Want to master in Java!! make yourself Observer and make this blog observable (A Javaonfly Production)\n"},"link":[{"rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml","href":"https:\/\/javaonfly.blogspot.com\/feeds\/posts\/default"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/-\/java+interview?alt=json-in-script\u0026max-results=8"},{"rel":"alternate","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/search\/label\/java%20interview"},{"rel":"hub","href":"http://pubsubhubbub.appspot.com/"},{"rel":"next","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/-\/java+interview\/-\/java+interview?alt=json-in-script\u0026start-index=9\u0026max-results=8"}],"author":[{"name":{"$t":"Shamik Mitra"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/02620539092954103493"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"32","height":"32","src":"\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjO2_A4uqRhlGdVcT56_sL024d9he6fReKq6CmpVwX7qqUKgBabEnhONHd9y8oFkNxgq80hXHMuC0mPAGRbIVw97aE63F0pwKB7fEKo397jRnCXRsc_1S1Px7VJSxmIsts\/s113\/534152_10200300633532106_2101616363_n.jpg"}}],"generator":{"version":"7.00","uri":"http://www.blogger.com","$t":"Blogger"},"openSearch$totalResults":{"$t":"44"},"openSearch$startIndex":{"$t":"1"},"openSearch$itemsPerPage":{"$t":"8"},"entry":[{"id":{"$t":"tag:blogger.com,1999:blog-1902220484592789830.post-1518277284781495984"},"published":{"$t":"2020-12-13T11:06:00.010+05:30"},"updated":{"$t":"2020-12-13T11:18:27.880+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"cleancodetips"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java interview"}],"title":{"type":"text","$t":"God Class- The developers booby trap!!!"},"content":{"type":"html","$t":"\u003Cp\u003E\u003C\/p\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEg4ZNGNljzNaJKvOhV2W2Qwx9K1SKqXu-J8_vVPqvRmdumPYXsfJKTkcv-6vGpe_2m3YH1U0IUFtpqpzc4iscOBZVV-YidVcepKvCmdC3W_NmqpKK2oAYuDSoJWKi7DS3PwuO8zmQbOJP0\/s762\/GodObjects.PNG\" imageanchor=\"1\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"501\" data-original-width=\"762\" src=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEg4ZNGNljzNaJKvOhV2W2Qwx9K1SKqXu-J8_vVPqvRmdumPYXsfJKTkcv-6vGpe_2m3YH1U0IUFtpqpzc4iscOBZVV-YidVcepKvCmdC3W_NmqpKK2oAYuDSoJWKi7DS3PwuO8zmQbOJP0\/s320\/GodObjects.PNG\" width=\"320\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u0026nbsp;\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; white-space: pre-wrap;\"\u003EWe all know what is a \u003Cb\u003Edystopian world\u003C\/b\u003E -- A world of Chaos or Injustice, for developer Antipattern, is the dystopian world but surprisingly willingly or unwillingly developers love to live in a dystopian world.\u003C\/span\u003E\u003Cp\u003E\u003C\/p\u003E\u003Cspan id=\"docs-internal-guid-bc250ab3-7fff-294c-44e6-b158d502dac0\"\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003EJokes apart, but this is the ground reality for most of the Legacy or old projects, common experience for developers are they change in one place, it breaks in another place. This has happened due to three major factors.\u003C\/span\u003E\u003C\/p\u003E\u003Cbr \/\u003E\u003Col style=\"margin-bottom: 0; margin-top: 0;\"\u003E\u003Cli dir=\"ltr\" style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; list-style-type: decimal; vertical-align: baseline; white-space: pre;\"\u003E\u003Cp dir=\"ltr\" role=\"presentation\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003ETight coupling and complex codebase.\u003C\/span\u003E\u003C\/p\u003E\u003C\/li\u003E\u003Cli dir=\"ltr\" style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; list-style-type: decimal; vertical-align: baseline; white-space: pre;\"\u003E\u003Cp dir=\"ltr\" role=\"presentation\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003EMultiple God Objects are driving Business Logic.\u003C\/span\u003E\u003C\/p\u003E\u003C\/li\u003E\u003Cli dir=\"ltr\" style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; list-style-type: decimal; vertical-align: baseline; white-space: pre;\"\u003E\u003Cp dir=\"ltr\" role=\"presentation\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003EInexperienced coder and tight timeline.\u003C\/span\u003E\u003C\/p\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003EI have discussed enough on Tight coupling, \u003C\/span\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;\"\u003ESRP, KISS, Law of Demeter\u003C\/span\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003E all these, these are the basic stuff must-have in the developer toolbox irrespective of experience.\u003C\/span\u003E\u003C\/p\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003EIronically we assume junior developers(Not all Juniors some are very good) may not have this knowledge, only Core Java knowledge is sufficient for them, and in the selection process we omit those questions or in their syllabus, we will not include them. Anyway, I am not going to discuss that this is a different topic and different countries have different selection processes or syllabus.\u003C\/span\u003E\u003C\/p\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003EMy take on coding is if from the beginning you do not know basic design principles, you can’t build a good code, which impacted other project members in long run, implementing a business logic according to acceptance criteria is just the tip of Iceberg, handling negative scenario and modularizing and packaging would be your main focus.\u0026nbsp;\u003C\/span\u003E\u003C\/p\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;\"\u003EIn Agile for a 5 pointers development story(not counting QA), i like (2 + 1+1+1) split,\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/p\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003E2 for designing and packaging higher component(Top-down approach)\u003C\/span\u003E\u003C\/p\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003E1 for Building logic,\u003C\/span\u003E\u003C\/p\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003E1 for handling error handling\u003C\/span\u003E\u003C\/p\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003E1 for Unit Testing.\u003C\/span\u003E\u003C\/p\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003EIf you are not maintaining design and packaging you will end up with multiple God objects, let discuss what do we mean by God Objects?\u003C\/span\u003E\u003C\/p\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;\"\u003EGod Object\/Monster Object ::\u0026nbsp;\u003C\/span\u003E\u003C\/p\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003EIn a simple term, God objects are something that is doing everything or knows about everything. That Uncle who pokes his nose in everywhere.\u0026nbsp;\u003C\/span\u003E\u003C\/p\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003EWant to trace your project’s God objects footprint? Check your last two months' commit history and plot a graph and find out which are classes modified most.\u003C\/span\u003E\u003C\/p\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003EYou will observe a few classes will change often, those are your projects God Objects- who controlling your projects. In a classical word a Feature Envy class.\u0026nbsp;\u003C\/span\u003E\u003C\/p\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;\"\u003EI always wonder what makes God Objects?\u003C\/span\u003E\u003C\/p\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003Eso, I try to see the commit history of the God object and found one use case that in the first place a developer creates objects which hold few important business responsibilities, the intention was to create a Mediator type of object which has reference to multiple other important objects to reduce the coupling and accomplish more business logic with a small line of codes. To know how Mediator reduces coupling you can check the Mediator pattern.\u003C\/span\u003E\u003C\/p\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003EThen over time developers are adding logic on top as this class acts as one shop for all. Blindly developers put logic there as it has already all-important objects references !!!!!.\u003C\/span\u003E\u003C\/p\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003Eit grows over time until you get a big escalation and your project managers mandate no one touch this class!!!!\u0026nbsp;\u003C\/span\u003E\u003C\/p\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003ESo, If we restrict ourselves in the first place we can reduce the probability of God Object, Once Mediator object grows it will turn into God objects-- A obvious booby trap, Once God Objects are created I think at that point in the Agile era refactoring a God object is very tough and should be avoided.\u0026nbsp;\u003C\/span\u003E\u003C\/p\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;\"\u003EOn the contrary, during each sprint, at the end of the development phase you need to pay attention to Modularizing and refactoring your code a must-do activity in Sprint, to accomplish it, take a blanket story for all development work, Sprint goal is not to only focus on MVP also it needs to focus on Technical quality, but the second one is missing most of the cases, As, Business or Scrum Master doesn't want to give importance on code quality, peer review. I am open to hearing various perspectives on the strategy,\u0026nbsp;\u003C\/span\u003E\u003C\/p\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;\"\u003EHow to maintain good code quality in Sprint?\u003C\/span\u003E\u003C\/p\u003E\u003Cbr \/\u003E\u003Cp dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\u003Cspan style=\"color: #0e101a; font-family: Arial; font-size: 11pt; font-style: italic; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;\"\u003E\u0026nbsp;God objects are bad for your code, it's a booby trap which brings unwanted side effects. Often your Mediator Objects turned in into God object, Pay attention to Mediator Objects after each sprint and refactor it when necessary, Restricting it in the first place will be the best protection not to refactor after it turned into God Objects.\u003C\/span\u003E\u003C\/p\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/javaonfly.blogspot.com\/feeds\/1518277284781495984\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2020\/12\/godclass-codesmell.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/1518277284781495984"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/1518277284781495984"},{"rel":"alternate","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2020\/12\/godclass-codesmell.html","title":"God Class- The developers booby trap!!!"}],"author":[{"name":{"$t":"Shamik Mitra"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/02620539092954103493"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"32","height":"32","src":"\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjO2_A4uqRhlGdVcT56_sL024d9he6fReKq6CmpVwX7qqUKgBabEnhONHd9y8oFkNxgq80hXHMuC0mPAGRbIVw97aE63F0pwKB7fEKo397jRnCXRsc_1S1Px7VJSxmIsts\/s113\/534152_10200300633532106_2101616363_n.jpg"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEg4ZNGNljzNaJKvOhV2W2Qwx9K1SKqXu-J8_vVPqvRmdumPYXsfJKTkcv-6vGpe_2m3YH1U0IUFtpqpzc4iscOBZVV-YidVcepKvCmdC3W_NmqpKK2oAYuDSoJWKi7DS3PwuO8zmQbOJP0\/s72-c\/GodObjects.PNG","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-1902220484592789830.post-4865068112119011518"},"published":{"$t":"2019-05-04T20:37:00.000+05:30"},"updated":{"$t":"2019-05-04T20:38:04.960+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"java interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"spring"},{"scheme":"http://www.blogger.com/atom/ns#","term":"spring boot"},{"scheme":"http://www.blogger.com/atom/ns#","term":"SpringAsync"}],"title":{"type":"text","$t":"Effective Advice on Spring @Async : Final Part ."},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003Ca href=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjMVGOyPZmwduOnSqt0nEFNQu7jszJWeAT2alPxarkBEzEmjLJcza0-j3wU9nVIwp5jcp5dNr88vN5RaEIMLdeJmNK1oNARSfkWVvKKxQh5WPD0YhaHgEZCCu-fLnoESWxP-zoPn3RCWnM\/s1600\/AsyncusgaeInSpringBoot.jpg\" imageanchor=\"1\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"232\" data-original-width=\"350\" height=\"212\" src=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjMVGOyPZmwduOnSqt0nEFNQu7jszJWeAT2alPxarkBEzEmjLJcza0-j3wU9nVIwp5jcp5dNr88vN5RaEIMLdeJmNK1oNARSfkWVvKKxQh5WPD0YhaHgEZCCu-fLnoESWxP-zoPn3RCWnM\/s320\/AsyncusgaeInSpringBoot.jpg\" width=\"320\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EIn my previous articles, I discussed the Spring Async concept\nand How to use it effectively. If you want to revisit,\u0026nbsp; below are the\nlinks, Also for new visitors I recommend,\u0026nbsp; please go through previous\nparts after reading this one.\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"2\" style=\"-webkit-text-stroke-width: 0px; background: white; box-sizing: border-box; font-variant-caps: normal; font-variant-ligatures: normal; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; widows: 2; word-spacing: 0px;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"2\" style=\"-webkit-text-stroke-width: 0px; background: white; box-sizing: border-box; font-variant-caps: normal; font-variant-ligatures: normal; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; widows: 2; word-spacing: 0px;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"2\" style=\"-webkit-text-stroke-width: 0px; background: white; box-sizing: border-box; font-variant-caps: normal; font-variant-ligatures: normal; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; widows: 2; word-spacing: 0px;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003E\u003Cspan style=\"box-sizing: border-box; color: #29a8ff; cursor: pointer; text-decoration: none;\"\u003E\u003Ca href=\"https:\/\/javaonfly.blogspot.com\/2019\/03\/effective-advice-on-spring-async-part-1.html\" style=\"box-sizing: border-box; cursor: pointer;\"\u003EPart 1\u003C\/a\u003E\u003C\/span\u003E::\u0026nbsp; How Spring\nAsync work internally, and how to use wisely so it assigns a task to a new\nthread.\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"3\" style=\"-webkit-text-stroke-width: 0px; background: white; box-sizing: border-box; font-variant-caps: normal; font-variant-ligatures: normal; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; widows: 2; word-spacing: 0px;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003E\u003Ca href=\"https:\/\/javaonfly.blogspot.com\/2019\/03\/SpringAsyncExceptionHandler.html\" style=\"box-sizing: border-box; cursor: pointer;\"\u003E\u003Cspan style=\"color: #29a8ff; text-decoration: none;\"\u003EPart2\u003C\/span\u003E\u003C\/a\u003E::\u0026nbsp; How to\nhandles Exception, If something goes wrong while executing a task.\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"4\" style=\"-webkit-text-stroke-width: 0px; background: white; box-sizing: border-box; font-variant-caps: normal; font-variant-ligatures: normal; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; widows: 2; word-spacing: 0px;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"5\" style=\"-webkit-text-stroke-width: 0px; background: white; box-sizing: border-box; font-variant-caps: normal; font-variant-ligatures: normal; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; widows: 2; word-spacing: 0px;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EIn this part, we will discuss How Spring Async work with the\nweb.\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"6\" style=\"-webkit-text-stroke-width: 0px; background: white; box-sizing: border-box; font-variant-caps: normal; font-variant-ligatures: normal; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; widows: 2; word-spacing: 0px;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EI am very excited to share an experience with you about Spring\nAsync and HttpRequest, as an interesting incident happened in one of my\nprojects, and I believe by sharing it I can save some valuable time of yours in\nfuture.\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"6\" style=\"-webkit-text-stroke-width: 0px; background: white; box-sizing: border-box; font-variant-caps: normal; font-variant-ligatures: normal; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; widows: 2; word-spacing: 0px;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003ELet me try to depict the scenario in Crisp word,\u0026nbsp;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"6\" style=\"-webkit-text-stroke-width: 0px; background: white; box-sizing: border-box; font-variant-caps: normal; font-variant-ligatures: normal; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; widows: 2; word-spacing: 0px;\"\u003E\n\u003Cstrong style=\"box-sizing: border-box;\"\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EObjective:\u003C\/span\u003E\u003C\/strong\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003E\u0026nbsp;The\u0026nbsp; objective was to pass some information from UI\u0026nbsp; to a backend controller,\nwhich will do some work and eventually calls an async mail service for triggers\na mail.\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"11\" style=\"-webkit-text-stroke-width: 0px; background: white; box-sizing: border-box; font-variant-caps: normal; font-variant-ligatures: normal; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; widows: 2; word-spacing: 0px;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EOne of my Juniors did the following code(Try to replicate the\ncode intention with below code snippet, not the actual code ), can you spot\nwhere the problem lies?\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"17\" style=\"-webkit-text-stroke-width: 0px; background: white; box-sizing: border-box; font-variant-caps: normal; font-variant-ligatures: normal; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; widows: 2; word-spacing: 0px;\"\u003E\n\u003Cstrong style=\"box-sizing: border-box;\"\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EThe Controller\u003C\/span\u003E\u003C\/strong\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003E:: Which\ncollect information from UI as a form\u0026nbsp; HTTP Servelet request, do some\noperation and pass it to async Mail service.\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003Epackage com.example.demo;\u003C\/span\u003E\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003Eimport javax.servlet.http.HttpServletRequest;\u003C\/span\u003E\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.beans.factory.annotation.Autowired;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.web.bind.annotation.PathVariable;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.web.bind.annotation.RequestMapping;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.web.bind.annotation.RequestMethod;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.web.bind.annotation.RestController;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E@RestController\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epublic class GreetController {\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E@Autowired\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eprivate AsyncMailTrigger greeter;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E@RequestMapping(value = \"\/greet\", method =\nRequestMethod.GET)\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epublic String greet(HttpServletRequest request) throws Exception\n{\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003EString name = request.getParameter(\"name\");\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Egreeter.asyncGreet(request);\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(Thread.currentThread() + \" Says Name is\n\" + name);\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(Thread.currentThread().getName() + \"\nHashcode\" + request.hashCode());\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Ereturn name;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E}\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E}\u003C\/span\u003E\u003Cspan style=\"color: #222635;\"\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003EThe Async Mail Service, I marked it as\n@Component you can easily change it to @Service. Here I have one method called\nasyncGreet which takes the HttpRequest, fetch the information from there and\nsay, trigger the mail(this part is omitted for simplicity). Notice I put a\nThread.sleep() here for a purpose, I will discuss the same later.\u003C\/span\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epackage com.example.demo;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport java.util.Map;\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"color: blue; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport javax.servlet.http.HttpServletRequest;\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"color: blue; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.scheduling.annotation.Async;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.stereotype.Component;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E@Component\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epublic class AsyncMailTrigger {\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E@Async\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epublic void asyncGreet(HttpServletRequest request) throws\nException {\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(\"Trigger mail in a New Thread ::\n\"\u0026nbsp; +\nThread.currentThread().getName());\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(Thread.currentThread().getName() + \"\ngreets before sleep\" + request.getParameter(\"name\"));\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003EThread.sleep(1000);\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(Thread.currentThread().getName() + \"\ngreets\" + request.getParameter(\"name\"));\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(Thread.currentThread().getName() + \"\nHashcode\" + request.hashCode());\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E}\u003C\/span\u003E\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E}\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003ENow the main class\u003C\/span\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epackage com.example.demo;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.boot.SpringApplication;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport\norg.springframework.boot.autoconfigure.SpringBootApplication;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.scheduling.annotation.EnableAsync;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E@SpringBootApplication\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E@EnableAsync\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epublic class SpringAsyncWebApplication {\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epublic static void main(String[] args) {\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ESpringApplication.run(SpringAsyncWebApplication.class, args);\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E}\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E}\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003EIf I run this program output will very similar\nto below\u003C\/span\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003EThread[http-nio-8080-exec-1,5,main] Says Name is Shamik\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Ehttp-nio-8080-exec-1 Hashcode 821691136\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ETrigger mail in a New Thread:: task-1\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Etask-1 greets before sleep Shamik\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Etask-1 greets null task\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"color: blue; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E-1 Hashcode 821691136\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"color: blue; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EPay attention to the output, the request has the information\nstill before sleep but after that, it magically disappears? Strange\nisn't?\u0026nbsp; But It is the same request object hashcode proves the same.\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"24\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EWhat happened ? what is the reason behind the disappearance of\nthe information from Request? That was happening to my junior, the mail\nrecipients, recipients name disappear from the request and mail is not\ntriggered.\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"29\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cstrong style=\"box-sizing: border-box;\"\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003ELet's put Sherlock hats to\ninvestigate the problem.\u003C\/span\u003E\u003C\/strong\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"30\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EIt is a very common problem with the request, to understand the\nproblem have a look, how a request lifecycle works.\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"28\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EThe request is created by the servlet container right before the\ncall to servlet service method, In Spring then request passes through\ndispatcher servlet, in the Dispatcher servlet identifies the controller by\nrequest mapping and calls the desired method in the controller, and when the\nrequest has been served, servlet container either delete the request object or\nreset the state of the request object. (This totally depends on the container\nimplementation, actually it maintains a pool of request). However, I am not\ngoing to the deep dive how container maintains the request object.\u0026nbsp;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"35\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EBut keep one thing in mind once the request has been served and\nresponse is committed, container reset its state or destroy the request\nobject.\u0026nbsp;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"36\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003ENow put Spring Async part into the consideration, What async\ndid,\u0026nbsp; it picks one thread from the thread pool and assigns the task to it,\nIn our case, we pass the request object to the async thread and in the\nasyncGreet method, we are trying to extract info directly from the request.\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"37\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EBut as this is async our main thread (Controller part)\u0026nbsp;\nwill be not waiting for this thread to complete, so it's print the statement\nand commits the response, and refresh the state of the request object.\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"38\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EIronically we pass the\u0026nbsp;\u003Cstrong style=\"box-sizing: border-box;\"\u003Erequest\u0026nbsp;\u003C\/strong\u003Eobject\ndirectly to the async thread, still, the point where the response is not committed\nin the main thread, request holds the data, I explicitly put a sleep a statement\u0026nbsp; so in main thread response can be committed and refresh the\nrequest state, so after sleep we experience there is no data in the request, it\nvanishes, a great experiment to prove the incident.\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"43\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"44\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cstrong style=\"box-sizing: border-box;\"\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EWhat we will learn from this\nexperiment?\u003C\/span\u003E\u003C\/strong\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"46\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cstrong style=\"box-sizing: border-box;\"\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003ENever pass Request object or any\nobject related to Request\/Response(headers), directly while using async you\nnever know when your response will be committed and refresh the state If you do\nyou will face an intermittent error.\u003C\/span\u003E\u003C\/strong\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"48\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"49\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cstrong style=\"box-sizing: border-box;\"\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EWhat can be done?\u003C\/span\u003E\u003C\/strong\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv pid=\"50\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003EIf you need to pass a bunch of information from request create a\nvalue object and set the information and pass the value object to Spring Async,\nin this manner, you can create a concrete solution.\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003C\/div\u003E\n\u003Cdiv pid=\"59\" style=\"background: white; box-sizing: border-box; margin: 3.75pt 0in 11.25pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003E\u003Cb\u003ERequestVO Object\u003C\/b\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003Epackage com.example.demo;\u003C\/span\u003E\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epublic class RequestVO {\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003EString name;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epublic String getName() {\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Ereturn name;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E}\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epublic void setName(String name) {\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Ethis.name = name;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E}\u003C\/span\u003E\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E}\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cb\u003EAsync Mail service\u003C\/b\u003E\u003C\/span\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003Epackage com.example.demo;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003Eimport java.util.Map;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003Eimport javax.servlet.http.HttpServletRequest;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003Eimport org.springframework.scheduling.annotation.Async;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003Eimport org.springframework.stereotype.Component;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E@Component\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epublic class AsyncMailTrigger {\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E@Async\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epublic void asyncGreet(RequestVO reqVO) throws Exception {\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(\"Trigger mail in a New Thread ::\n\"\u0026nbsp; +\nThread.currentThread().getName());\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(Thread.currentThread().getName() + \"\ngreets before sleep\" + reqVO.getName());\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003EThread.sleep(1000);\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(Thread.currentThread().getName() + \"\ngreets\" + reqVO.getName());\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E}\u003C\/span\u003E\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E}\u003C\/span\u003E\u003Cspan style=\"color: #222635;\"\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cb\u003EGreet Controller\u003C\/b\u003E\u003C\/span\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epackage com.example.demo;\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"color: blue; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport javax.servlet.http.HttpServletRequest;\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"color: blue; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.beans.factory.annotation.Autowired;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.web.bind.annotation.PathVariable;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.web.bind.annotation.RequestMapping;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.web.bind.annotation.RequestMethod;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.web.bind.annotation.RestController;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E@RestController\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epublic class GreetController {\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E@Autowired\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Eprivate AsyncMailTrigger greeter;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E@RequestMapping(value = \"\/greet\", method =\nRequestMethod.GET)\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Epublic String greet(HttpServletRequest request) throws Exception\n{\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003EString name = request.getParameter(\"name\");\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ERequestVO vo = new RequestVO();\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Evo.setName(name);\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E\/\/greeter.asyncGreet(request);\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Egreeter.asyncGreet(vo);\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(Thread.currentThread() + \" Says Name is\n\" + name);\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(Thread.currentThread().getName() + \"\nHashcode\" + request.hashCode());\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Ereturn name;\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E}\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003E}\u003C\/span\u003E\u003Cspan style=\"color: #222635;\"\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cb\u003EOutput\u003C\/b\u003E\u003C\/span\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003EThread[http-nio-8080-exec-1,5,main] Says Name is Shamik\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Ehttp-nio-8080-exec-1 Hashcode 1669579896\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003ETrigger mail in a New Thread:: task-1\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Etask-1 greets before sleep Shamik\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cspan style=\"font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E\u003Cspan style=\"color: blue;\"\u003Etask-1 greets Shamik\u003C\/span\u003E\u003Cspan style=\"color: #222635;\"\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cstrong\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003EConclusion\u003C\/span\u003E\u003C\/strong\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , serif; font-size: 14.5pt;\"\u003E: Hope you enjoyed the article If you have questions, please\nput your questions on the comment box.\u003C\/span\u003E\u003Cspan style=\"color: #222635; font-family: \u0026quot;cambria\u0026quot; , \u0026quot;serif\u0026quot;; font-size: 14.5pt;\"\u003E\u003Co:p\u003E\u003C\/o:p\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003C\/div\u003E\n\u003Cdiv class=\"MsoNormal\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background: white; margin-bottom: 11.25pt; margin-left: 0in; margin-right: 0in; margin-top: 3.75pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/javaonfly.blogspot.com\/feeds\/4865068112119011518\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2019\/05\/effective-advice-on-spring-async-final.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/4865068112119011518"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/4865068112119011518"},{"rel":"alternate","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2019\/05\/effective-advice-on-spring-async-final.html","title":"Effective Advice on Spring @Async : Final Part ."}],"author":[{"name":{"$t":"Shamik Mitra"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/02620539092954103493"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"32","height":"32","src":"\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjO2_A4uqRhlGdVcT56_sL024d9he6fReKq6CmpVwX7qqUKgBabEnhONHd9y8oFkNxgq80hXHMuC0mPAGRbIVw97aE63F0pwKB7fEKo397jRnCXRsc_1S1Px7VJSxmIsts\/s113\/534152_10200300633532106_2101616363_n.jpg"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjMVGOyPZmwduOnSqt0nEFNQu7jszJWeAT2alPxarkBEzEmjLJcza0-j3wU9nVIwp5jcp5dNr88vN5RaEIMLdeJmNK1oNARSfkWVvKKxQh5WPD0YhaHgEZCCu-fLnoESWxP-zoPn3RCWnM\/s72-c\/AsyncusgaeInSpringBoot.jpg","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-1902220484592789830.post-1381221667007758338"},"published":{"$t":"2019-03-24T13:59:00.000+05:30"},"updated":{"$t":"2019-05-04T20:25:12.513+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"java interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"spring"},{"scheme":"http://www.blogger.com/atom/ns#","term":"SpringAsync"}],"title":{"type":"text","$t":"Effective Advice on Spring @Async (ExceptionHandler): Part 2"},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003Ca href=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjGpT0EkSlkVIWPQ46feOC-pCgDUfbRR76eOq7UzReebYl1U6m99YlVvfsbShxEGAezv5JkdyOGRILpvdBggdhHtIKX7xp0AF_reqPoZW8hej93trFo8rKq82FyApjOANGrUl0goOt1cv0\/s1600\/SpringAsyncpart2.jpg\" imageanchor=\"1\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"625\" data-original-width=\"1000\" height=\"200\" src=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjGpT0EkSlkVIWPQ46feOC-pCgDUfbRR76eOq7UzReebYl1U6m99YlVvfsbShxEGAezv5JkdyOGRILpvdBggdhHtIKX7xp0AF_reqPoZW8hej93trFo8rKq82FyApjOANGrUl0goOt1cv0\/s320\/SpringAsyncpart2.jpg\" width=\"320\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\nIn this article, I am going to discuss, how to catch Exception while you are using @Async annotation with Spring boot. Before deep diving to Exception handler I hope everyone who is reading this article, read my first part, If not I encourage you to do so, here is the link.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nWhile you are forking thread from the main thread possibly you have two options.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n1. \u003Cb\u003EFire and forget\u003C\/b\u003E :: you just fork a thread , assign some work to that thread, and just forget, at this moment,\u0026nbsp; you do not bother with the outcome as you do not need this information in order to execute next business logic, generally it's return type is void, let's understand with an example, Say you are in a process of\u0026nbsp; sending salary to the employees,\u0026nbsp; as a part of that you send an email to each employee attaching respective salary slip, and you do it asynchronously. Obviously sending salary slip via email to the employee is not a part of your core business logic, it is a cross-cutting concern, it is a good to have a feature(Some cases it is must have, then you adopt retrying, or schedule mechanism) rather than must to have.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n2. \u003Cb\u003EFire with Callback\u003C\/b\u003E:: Here you fork a thread from the main thread and assign some work to that thread then attach a Callback. After that, the main thread proceeds with other tasks but the main thread keeps checking the callback for the result. The main thread needs that outcome from sub-thread as a form of callback to execute further.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSay you are preparing an Employee report,\u0026nbsp; Your software stores employee information in different backends based on the data category. , say General Service hold Employees general data (name, birthday, sex, address) and financial service holds(Salary, Tax, PF related data), so you fork two threads parallelly one for general service one for financial service, but you need both data to proceed further as in the report you need to combine both the data. So in the main thread, you want those results as a callback from subthreads, generally, we do it by CompletebleFuture(read Callable with Future).\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn the above scenarios, if all is well that is the ideal case, but in the real-time exception can happen, then how you will handle Exception in the above two scenarios?\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn the second scenario, It is very easy as result comes as a callback, whatever the result it is(Success or Failure), in case of failure,\u0026nbsp; Exception wrapped inside CompltebleFuture you can check the same and handle it in the main thread. Not a big deal,\u0026nbsp; basic java code. So I will skip that.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nBut Scenario one is tricky, you fork a thread with some business case,\u0026nbsp; how you can be sure that is a success, or if that is failure how you will debug it, how you will get a trace that something goes wrong?\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThe solution is very simple, You need to inject your own Exception handler so that if an exception occurs while you are executing an Async method it should pass the control to that handler and then handles what to do with that,\u0026nbsp; very Simple isn't it.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nTo achieve that we need to follow below steps.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n1. \u003Cb\u003EAsyncConfigurer\u003C\/b\u003E::\u0026nbsp; AsyncConfigurere is an Interface provided by Spring, It provides two methods one is if you want to override the TaskExecutor(Threadpool) another is an Exception handler where you can inject your exception handler so it can catch the uncaught exceptions. You can create your own class and implements that one directly. but I will not do that, as an alternative, I will use Spring\u0026nbsp; AsyncConfigurerSupport class which annotates by @Configuration and @EnableAsync, and provides a default implementation.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Epackage com.example.ask2shamik.springAsync;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport java.lang.reflect.Method;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport java.util.concurrent.Executor;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.context.annotation.Configuration;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.core.task.SimpleAsyncTaskExecutor;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.lang.Nullable;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.scheduling.annotation.AsyncConfigurerSupport;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.scheduling.annotation.EnableAsync;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E@Configuration\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E@EnableAsync\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Epublic class CustomConfiguration extends AsyncConfigurerSupport {\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E@Override\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Epublic Executor getAsyncExecutor() {\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Ereturn new SimpleAsyncTaskExecutor();\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E@Override\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E@Nullable\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Epublic AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003Ereturn (throwable, method, obj)-\u0026gt;{\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(\"Exception Caught in Thread - \" + Thread.currentThread().getName());\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(\"Exception message - \" + throwable.getMessage());\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(\"Method name - \" + method.getName());\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Efor (Object param : obj) {\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(\"Parameter value - \" + param);\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E};\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nPlease note that, In case of, \u003Cb\u003EgetAsyncExecutor \u003C\/b\u003Emethod, I am not going to create any new Executor as I do not want to use my own task executor so I use Spring default \u003Cb\u003ESimpleAsyncExecutor\u003C\/b\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nBut\u0026nbsp; I need my custom uncaught exception handler to handle any uncaught Exception so I create a lambda expression which actually extends\u0026nbsp; \u003Cb\u003EAsyncUncaughtExceptionHandler \u003C\/b\u003Eclass and overrides \u003Cb\u003Ehandleuncaughtexception \u003C\/b\u003Emethod.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nIn this way, I instruct Spring while loading, use my application specific AsyncConfugurer(\u003Cb\u003ECustomConfiguration\u003C\/b\u003E)\u0026nbsp; and use my lambda expression as an Exception handler.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNow I create an async method which will throw an Exception.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Epackage com.example.ask2shamik.springAsync.demo;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport java.util.Map;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.scheduling.annotation.Async;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.stereotype.Component;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E@Component\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Epublic class AsyncMailTrigger {\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E@Async\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Epublic void senMailwithException() throws Exception{\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Ethrow new Exception(\"SMTP Server not found :: orginated from Thread :: \" + Thread.currentThread().getName());\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNow, Create a caller method.\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Epackage com.example.ask2shamik.springAsync.demo;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport java.util.HashMap;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport java.util.Map;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.beans.factory.annotation.Autowired;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.scheduling.annotation.Async;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.stereotype.Component;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E@Component\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Epublic class AsyncCaller {\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E@Autowired\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003EAsyncMailTrigger asyncMailTriggerObject;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003Epublic void rightWayToCall() throws Exception {\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003ESystem.out.println(\"Calling From rightWayToCall Thread \" + Thread.currentThread().getName());\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003EasyncMailTriggerObject.senMailwithException();\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNow let's run this Application using Spring boot, to see How it catch the Exception thrown by the method sendMailwithException.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Epackage com.example.ask2shamik.springAsync;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.beans.factory.annotation.Autowired;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.boot.CommandLineRunner;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.boot.SpringApplication;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.boot.autoconfigure.SpringBootApplication;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.context.ApplicationContext;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.context.annotation.Bean;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport org.springframework.scheduling.annotation.EnableAsync;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003Eimport com.example.ask2shamik.springAsync.demo.AsyncCaller;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E@SpringBootApplication\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Epublic class DemoApplication {\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E@Autowired\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003EAsyncCaller caller;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003Epublic static void main(String[] args) {\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003ESpringApplication.run(DemoApplication.class, args);\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u0026nbsp;@Bean\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u0026nbsp;public CommandLineRunner commandLineRunner(ApplicationContext ctx) {\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; return args -\u0026gt; {\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; caller.rightWayToCall();\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;};\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u0026nbsp; \u0026nbsp; }\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"color: blue;\"\u003E}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nAnd the output is\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003ECalling From rightWayToCall Thread main\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003EException Caught in Thread - SimpleAsyncTaskExecutor-1\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003EException message - SMTP Server not found:: originated from Thread:: SimpleAsyncTaskExecutor-1\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003EMethod name - senMailwithException\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EConclusion\u003C\/b\u003E: Hope you like the tutorial, If you have a question please put it in the comment box and stay tuned for part 3.\u003C\/div\u003E\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/javaonfly.blogspot.com\/feeds\/1381221667007758338\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2019\/03\/SpringAsyncExceptionHandler.html#comment-form","title":"1 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/1381221667007758338"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/1381221667007758338"},{"rel":"alternate","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2019\/03\/SpringAsyncExceptionHandler.html","title":"Effective Advice on Spring @Async (ExceptionHandler): Part 2"}],"author":[{"name":{"$t":"Shamik Mitra"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/02620539092954103493"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"32","height":"32","src":"\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjO2_A4uqRhlGdVcT56_sL024d9he6fReKq6CmpVwX7qqUKgBabEnhONHd9y8oFkNxgq80hXHMuC0mPAGRbIVw97aE63F0pwKB7fEKo397jRnCXRsc_1S1Px7VJSxmIsts\/s113\/534152_10200300633532106_2101616363_n.jpg"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjGpT0EkSlkVIWPQ46feOC-pCgDUfbRR76eOq7UzReebYl1U6m99YlVvfsbShxEGAezv5JkdyOGRILpvdBggdhHtIKX7xp0AF_reqPoZW8hej93trFo8rKq82FyApjOANGrUl0goOt1cv0\/s72-c\/SpringAsyncpart2.jpg","height":"72","width":"72"},"thr$total":{"$t":"1"}},{"id":{"$t":"tag:blogger.com,1999:blog-1902220484592789830.post-2017531183903409920"},"published":{"$t":"2018-02-19T22:34:00.000+05:30"},"updated":{"$t":"2018-02-19T22:34:55.926+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"java"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java interview questions"}],"title":{"type":"text","$t":"Java:: What is invokeDynamic"},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\n\u003Ca href=\"http:\/\/javaonfly.blogspot.in\/2018\/01\/how-does-method-dispatch-happen-in-java.html\"\u003EIn my previous article\u003C\/a\u003E,\n I discussed --How JVM determines which methods to call at runtime?Also,\n we learned that In bytecode level, java compiler uses 4 special opcodes\n for method invoking\u003Cbr \/\u003E\n\u003Cstrong\u003Einvokestatic,invokeinterface,invokevirtual,invokespecial\u003C\/strong\u003E.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNow,\n the question arises if all types of methods invoking (interface, static\n method, instance method, constructor etc.) are covered by 4 opcodes, \nwhy there is another opcode \u003Cstrong\u003EinvokeDynamic\u003C\/strong\u003E has been introduced?\u003Cbr \/\u003E\n\u003Cstrong\u003EWhy invokeDynamic?\u003C\/strong\u003E\u003Cbr \/\u003E\nTo understand why invokedymic is required let dig down to a real problem,\u003Cstrong\u003E\u0026nbsp; for a moment think you are \u003C\/strong\u003Ebuilding\n a framework where based on the command passed from UI\u0026nbsp;you load a class \nruntime\u0026nbsp;and invoke a particular method of that class.\u003Cbr \/\u003E\nSay \nyou are building a Web framework like Struts\u0026nbsp;-- Now based on the URL you\n have to map a java class and invoke a specific\u0026nbsp;method so you \nwill\u0026nbsp;create an XML(like struts-config.xml) where developer explicitly \nput the strategy --\u0026nbsp;which URL map to which class and which method to be \ncalled.\u003Cbr \/\u003E\nLet's see the prototype of the XML.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"cm-replace _replace_01\" style=\"border: medium none; height: 188px; margin: 0px 0px 10px; width: 821px;\"\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u0026lt;controller url=\"\/employee\" class=\"com.java.example.EmployeeController\"\u0026gt;\u003Cbr \/\u003E\u0026lt;param key=\"add\" method=\"executeAdd\"\u0026gt;\u003Cbr \/\u003E\u0026lt;param key=\"update\" method=\"executeUpdate\"\u0026gt;\u003Cbr \/\u003E\u0026lt;param key=\"delete\" method=\"executeDelete\"\u0026gt;\u003Cbr \/\u003E\u0026lt;goto key=\"success\"\u0026nbsp; path=\"\/success.jsp\"\u0026gt;\u003Cbr \/\u003E\u0026lt;goto key=\"faliure\"\u0026nbsp; path=\"\/error.jsp\"\u0026gt;\u003Cbr \/\u003E\u0026lt;\/controller\u0026gt;\u003C\/span\u003E\u003C\/div\u003E\nSeeing,\n this configuration XML one thing is clear, based on the action taken by\n the user in UI, different method will be called for EmployeController \nclass, If user performs an add then internally an '\u003Cstrong\u003Eadd\u003C\/strong\u003E' parameter will be passed and framework checks that which method has to call for 'add' parameter and found it is executeAdd.\n Until\u0026nbsp; User does not take an action no one can tell which method will \nbe called, so all the decision has to be taken at runtime based on the \nparameters passed(add, edit, delete).\u003Cbr \/\u003E\nThink how you can \ndesign the same in java, what is the weapon available in java to \nimplement the same, Of course, by the Reflection mechanism-- By the \nreflection you are able to load the Employee controller class and based \non the parameter you can invoke the appropriate method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nPseudocode will be like that.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv class=\"cm-replace _replace_11\" style=\"border: medium none; height: 148px; margin: 0px 0px 10px; width: 821px;\"\u003E\n\u003Cspan style=\"color: blue;\"\u003EClass controller = Class.forname(\"com.example.EmployeeController\");\u003Cbr \/\u003EController empController = controller.newInstance();\u003Cbr \/\u003EClass inputParams[] = {javax.servlet.http.HttpRequest,javax.servlet.http.HttpResponse};\u003Cbr \/\u003EString methodSuffix = makeFirstLetterCapital(empController.getParam());\/\/add to Add\u003Cbr \/\u003EMethod tobeInvoked= Class.declaredMethod(\"execute\"+methodSuffix,inputParams);\u003Cbr \/\u003EtobeInvoked.invoke(empController,request,response);\u003C\/span\u003E\u003C\/div\u003E\nThis\n runs very well, It will take the decision on runtime except for one \nproblem. Reflection has huge performance penalties because it is always \nchecking security constraints like what is the method access specifiers,\n is the caller has permission to call the method etc, so for this \nreflection is bit slow.\u003Cbr \/\u003E\nThis is the one reason to introduce \u003Cstrong\u003EinvokeDynamic\u003C\/strong\u003E\n in the place of reflection, it facilitates dynamic programming -- where\n type checking and method resolution done at runtime. So by that you can\n add remove methods programmatically -- You can do bytecode engineering \nruntime I mean, you can insert a new method which is not in the class \ndefinition !!! or override a method runtime!!! invokeDynamic provides \nthis type of flexibility, unlike reflection.\u003Cbr \/\u003E\ninvokeDynamic is as fast as other opcodes, but to introducing invoke dynamic in Java is not an easy task.\u003Cbr \/\u003E\nJava\n is a statically type so type checking is done at compile time also \ncompiler checks a method is available in the class or not if the method \nis not available it throws compiler error so the questions is\u003Cbr \/\u003E\n\u003Cstrong\u003EHow\n can we trick compiler in such a way where we can introduce a new method\n at runtime to be specific type checking and method resolution done at \nrun-time?\u003C\/strong\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cstrong\u003EHow invokeDyamic works internally?\u003C\/strong\u003E\u003Cbr \/\u003E\nTo work the invokeDynamic opcode correctly two important components are \u003Cstrong\u003EMethodHandle\u003C\/strong\u003E and \u003Cstrong\u003ECallSite\u003C\/strong\u003E\u003Cbr \/\u003E\n\u003Cstrong\u003EMethodHandle: \u003C\/strong\u003EMethod\n handles wraps the metadata about a method-- It holds the method \nsignature so by invoking it you can invoke a method on an Object at \nruntime.\u003Cbr \/\u003E\n\u003Cstrong\u003ECallSite : \u003C\/strong\u003ECallsite\n can hold Method handle and if the Call site is mutable we can change \nthe Method handle time to time,\u0026nbsp;so based on the parameter we can change \nthe MethodHandle and as Method handle holds the method so runtime we can\n change the method call without altering the bytecode instruction that \nis invoked dynamic, so same method invocation can execute different \nmethods based on the parameters.\u003Cbr \/\u003E\nActually when the \ninvokeDynamic instruction is read by the interpreter following procedure\n happens underhood--\u0026nbsp;\u0026nbsp;invokedynamic instruction is associated with a \nspecial method called the \u003Cstrong\u003Ebootstrap method\u003C\/strong\u003E (BSM). When the invokedynamic instruction is read by the interpreter, the BSM is invoked. It returns an object called Callsite (hold a method handle) that indicates which method actually execute.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSo,\n Calliste works like the\u0026nbsp;subline\u0026nbsp;in\u0026nbsp;the\u0026nbsp;Train\u0026nbsp;track-- Trani\u0026nbsp;always \nruns\u0026nbsp;on\u0026nbsp;a\u0026nbsp;straight\u0026nbsp;track\u0026nbsp;but\u0026nbsp;when \nit\u0026nbsp;needs\u0026nbsp;to\u0026nbsp;change\u0026nbsp;it's\u0026nbsp;direction\u0026nbsp;Line\u0026nbsp;Engineers\u0026nbsp;are\u0026nbsp;pull\u0026nbsp;the\n liver of\u0026nbsp;subline\u0026nbsp;so\u0026nbsp;it\u0026nbsp;joins \nwith\u0026nbsp;another\u0026nbsp;line\u0026nbsp;according\u0026nbsp;to\u0026nbsp;the\u0026nbsp;needs\u0026nbsp;and\u0026nbsp;Train\u0026nbsp;just\u0026nbsp;pass\u0026nbsp;on\u0026nbsp;that\u0026nbsp;line\u0026nbsp;and\u0026nbsp;change\u0026nbsp;the\u0026nbsp;track,\n Calliste mimic the same methodology when based\u0026nbsp;on parameter\u0026nbsp;it\u0026nbsp;changes \nthe underlying\u0026nbsp;method\u0026nbsp;handle\u0026nbsp;and\u0026nbsp;same\u0026nbsp;method\u0026nbsp;invocation\u0026nbsp;call\u0026nbsp;diffrent\u0026nbsp;method\u0026nbsp;at\u0026nbsp;runtime.\u003Cbr \/\u003E\nLet\u0026nbsp;see\u0026nbsp;the\u0026nbsp;diagram,\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003Ca href=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjgz9ffqVXp4Sn4_7IU-4kVIbputbuAE93eipD2Y7ykLKWwqEee5uuxkzmaf_2eSIJPNbAlowGQFQNGQq4Yz6p7ol_nKg46A8QDdLpCFj2Hd1fyp0lcQ2bNCvJPJRlwXzmDP0Z0NR22flo\/s1600\/Callsite.jpg\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"351\" data-original-width=\"641\" height=\"350\" src=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjgz9ffqVXp4Sn4_7IU-4kVIbputbuAE93eipD2Y7ykLKWwqEee5uuxkzmaf_2eSIJPNbAlowGQFQNGQq4Yz6p7ol_nKg46A8QDdLpCFj2Hd1fyp0lcQ2bNCvJPJRlwXzmDP0Z0NR22flo\/s640\/Callsite.jpg\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cstrong\u003EConclusion\u003C\/strong\u003E:\u0026nbsp;invokeDynamic\u0026nbsp;is\u0026nbsp;a\u0026nbsp;valuable\u0026nbsp;inclusion\u0026nbsp;in\n \nterms\u0026nbsp;of\u0026nbsp;framework\u0026nbsp;level\u0026nbsp;developer,\u0026nbsp;By\u0026nbsp;invokedynamic\u0026nbsp;we\u0026nbsp;can\u0026nbsp;get\u0026nbsp;the\u0026nbsp;real\u0026nbsp;essence\u0026nbsp;of\u0026nbsp;Dynamic\u0026nbsp;programming\u0026nbsp;in\u0026nbsp;JVM,\u0026nbsp;Many\u0026nbsp;languages\u0026nbsp;which\u0026nbsp;run\u0026nbsp;on\u0026nbsp;JVM\u0026nbsp;uses\u0026nbsp;the\u0026nbsp;invokedynamic\n to achieve dynamic type\u0026nbsp;checking\u0026nbsp;also\u0026nbsp;Lamda\u0026nbsp;uses\u0026nbsp;invokedynamic,\u0026nbsp;In\u0026nbsp;my\u0026nbsp;next\u0026nbsp;tutorial\u0026nbsp;I\u0026nbsp;will\u0026nbsp;show\u0026nbsp;you\u0026nbsp;how\u0026nbsp;to\u0026nbsp;write\u0026nbsp;Callsite\u0026nbsp;and\u0026nbsp;MethodHandle\u0026nbsp;and\u0026nbsp;How\u0026nbsp;you\u0026nbsp;can\u0026nbsp;changeMethodHandle\u0026nbsp;under\u0026nbsp;Callsite\u0026nbsp;dynamically.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/javaonfly.blogspot.com\/feeds\/2017531183903409920\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2018\/02\/invokeDynamic.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/2017531183903409920"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/2017531183903409920"},{"rel":"alternate","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2018\/02\/invokeDynamic.html","title":"Java:: What is invokeDynamic"}],"author":[{"name":{"$t":"Shamik Mitra"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/02620539092954103493"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"32","height":"32","src":"\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjO2_A4uqRhlGdVcT56_sL024d9he6fReKq6CmpVwX7qqUKgBabEnhONHd9y8oFkNxgq80hXHMuC0mPAGRbIVw97aE63F0pwKB7fEKo397jRnCXRsc_1S1Px7VJSxmIsts\/s113\/534152_10200300633532106_2101616363_n.jpg"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjgz9ffqVXp4Sn4_7IU-4kVIbputbuAE93eipD2Y7ykLKWwqEee5uuxkzmaf_2eSIJPNbAlowGQFQNGQq4Yz6p7ol_nKg46A8QDdLpCFj2Hd1fyp0lcQ2bNCvJPJRlwXzmDP0Z0NR22flo\/s72-c\/Callsite.jpg","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-1902220484592789830.post-6105225966266033707"},"published":{"$t":"2018-01-23T11:24:00.000+05:30"},"updated":{"$t":"2018-01-23T11:24:37.868+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"java"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java interview questions"}],"title":{"type":"text","$t":"How does method dispatch happen in Java?"},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\n\u003Ca href=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEhvVz33iViIfmOpkpgQGtgmRFoy5f4004_uZNotiCJ_cf8V8hJldaxx2VX7zefKx4y7UmT4EgGav5c2KqSF9CBMqkQrXrYKxis913pvoUTrqUbPUWacdsI4RjLETgYjmwRl5VnM8TM5LG4\/s1600\/method+call.jpeg\" imageanchor=\"1\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"194\" data-original-width=\"259\" src=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEhvVz33iViIfmOpkpgQGtgmRFoy5f4004_uZNotiCJ_cf8V8hJldaxx2VX7zefKx4y7UmT4EgGav5c2KqSF9CBMqkQrXrYKxis913pvoUTrqUbPUWacdsI4RjLETgYjmwRl5VnM8TM5LG4\/s1600\/method+call.jpeg\" \/\u003E\u003C\/a\u003E\u0026nbsp;Have you ever wondered when you call a method like\u003Cb\u003E \u003C\/b\u003Ea\u003Cb\u003E list.add(\"Shamik\")\u003C\/b\u003E, How the actual method invoke in runtime?\u003Cbr \/\u003E\nIf you want to discover the How part then you are in the\u0026nbsp;right place else you can easily skip the article as it is not related to coding perspective. we know in Java we maintain two steps process\u003Cbr \/\u003E\nCompiler compiles and make the bytecodes\u003Cbr \/\u003E\nThe interpreter takes bytecode and changes the instruction to machine code.\u003Cbr \/\u003E\nBut think, When your code compiles to bytecode how the method call looks like and in the runtime how the dynamic linking happens, In a simple word how JVM find the actual method and call that method.\u003Cbr \/\u003E\nIn this tutorial, we will discuss the same.\u003Cbr \/\u003E\nIn java(till Java7) we have four types of method\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E1. Static methods.\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E2. private, package private or public methods\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E3. Interface methods declaration.\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E4. Some special methods like the constructor,\u0026nbsp;super etc.\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nNow, as the actual method call happens at runtime, somehow at compile time(bytecode) we have to instruct JVM where to find the method or location of the method. But for some cases it is not possible to tell earlier(compile time) which method will be invoked( like in case of overriding, Polymorphism) so compiler has to defer the lookup of the method until runtime, so there are different types of \u003Cb\u003Eopcodes \u003C\/b\u003Eare used by compiler to tell JVM what to do in runtime.\u003Cbr \/\u003E\nAt runtime, JVM maintains a runtime table called \u003Cb\u003Evtable \u003C\/b\u003Ewhere each entry represents the precise location of the method. The help of this vtable, JVM actually dispatches the call to an actual method.\u003Cbr \/\u003E\n\u003Cb\u003Eopcodes\u003C\/b\u003E\u003Cbr \/\u003E\nIn bytecode, java uses 4 \u003Cb\u003Eopcodes \u003C\/b\u003Estill java6 but in java7 there is a new opcode introduced called \u003Cb\u003Einvokedynamic\u003C\/b\u003E, I will write a separate article on \u003Cb\u003Einvokedynamic\u003C\/b\u003E\u0026nbsp;opcode but in this article, we will discuss the other opcodes.\u003Cbr \/\u003E\n\u003Cb\u003Einvokestatic\u003C\/b\u003E: invokestatic opcode is used at compile time to dispatch static methods.\u003Cbr \/\u003E\n\u003Cb\u003Einvokevirtual\u003C\/b\u003E: invokevirtual used to dispatch instance methods.\u003Cbr \/\u003E\n\u003Cb\u003Einvokespecial\u003C\/b\u003E:\u0026nbsp;invokespecial is used to dispatch special methods like constructor or super or for the private method.\u003Cbr \/\u003E\n\u003Cb\u003Einvokeinterface\u003C\/b\u003E:\u0026nbsp;invokeinterface is used to dispatch a method call via an interface.\u003Cbr \/\u003E\nNow, we will write a java example and try to see the bytecode representation of that example.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Epackage com.example.methodcall;\u003Cbr \/\u003E\u003Cbr \/\u003Eimport java.util.ArrayList;\u003Cbr \/\u003Eimport java.util.List;\u003Cbr \/\u003E\u003Cbr \/\u003Epublic class MethodCall {\u003Cbr \/\u003E\u003Cbr \/\u003Epublic void addCity() {\u003Cbr \/\u003EList\u0026lt;String\u0026gt; city = new ArrayList\u0026lt;String\u0026gt;();\u003Cbr \/\u003Ecity.add(\"Kolkata\");\u003Cbr \/\u003E\u003Cbr \/\u003E}\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Epublic void addState() {\u003Cbr \/\u003EArrayList\u0026lt;String\u0026gt; state = new ArrayList\u0026lt;String\u0026gt;();\u003Cbr \/\u003Estate.add(\"WestBengal\");\u003Cbr \/\u003E\u003Cbr \/\u003E}\u003Cbr \/\u003E\u003Cbr \/\u003Epublic static void main(String[] args) {\u003Cbr \/\u003EMethodCall target = new MethodCall();\u003Cbr \/\u003Etarget.addCity();\u003Cbr \/\u003Etarget.addState();\u003Cbr \/\u003E}\u003Cbr \/\u003E\u003Cbr \/\u003E}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cspan style=\"color: black;\"\u003ENow I want to see the bytecode representation of the above java program so I will run the following command\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003Ejavap -c MethodCall.class\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\nBytecode will look like following\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"color: blue;\"\u003E\u0026nbsp;public class com.example.methodcall.MethodCall {\u003Cbr \/\u003E\u0026nbsp; public com.example.methodcall.MethodCall();\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; Code:\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 0: aload_0\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 1: invokespecial #8\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \/\/ Method java\/lang\/Object.\"\u0026lt;init\u0026gt;\":()V\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 4: return\u003Cbr \/\u003E\u003Cbr \/\u003E\u0026nbsp; public void addCity();\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; Code:\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 0: new\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; #15\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \/\/ class java\/util\/ArrayList\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 3: dup\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 4: invokespecial #17\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \/\/ Method java\/util\/ArrayList.\"\u0026lt;init\u0026gt;\":()V\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 7: astore_1\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 8: aload_1\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 9: ldc\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; #18\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \/\/ String Kolkata\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 11: invokeinterface #20,\u0026nbsp; 2\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \/\/ InterfaceMethod java\/util\/List.add:(Ljava\/lang\/Object;)Z\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 16: pop\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 17: return\u003Cbr \/\u003E\u003Cbr \/\u003E\u0026nbsp; public void addState();\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; Code:\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 0: new\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; #15\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \/\/ class java\/util\/ArrayList\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 3: dup\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 4: invokespecial #17\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \/\/ Method java\/util\/ArrayList.\"\u0026lt;init\u0026gt;\":()V\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 7: astore_1\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 8: aload_1\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 9: ldc\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; #31\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \/\/ String WestBengal\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 11: invokevirtual #33\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \/\/ Method java\/util\/ArrayList.add:(Ljava\/lang\/Object;)Z\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 14: pop\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 15: return\u003Cbr \/\u003E\u003Cbr \/\u003E\u0026nbsp; public static void main(java.lang.String[]);\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; Code:\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 0: new\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; #1\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \/\/ class com\/example\/methodcall\/MethodCall\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 3: dup\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 4: invokespecial #39\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \/\/ Method \"\u0026lt;init\u0026gt;\":()V\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 7: astore_1\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 8: aload_1\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 9: invokevirtual #40\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \/\/ Method addCity:()V\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 12: aload_1\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 13: invokevirtual #42\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \/\/ Method addState:()V\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 16: return\u003Cbr \/\u003E}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EDeep dive into the bytecode Representation :\u003C\/b\u003E\u003Cbr \/\u003E\nIn the above bytecode representation, except invokestatic all opcodes has been used.\u003Cbr \/\u003E\nIf\n you noticed the bytecode minutely you can explore that for each method a\n section is entitled and each\u0026nbsp;java line converted to a command. Let go \nthrough each method section\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003Ecom.example.methodcall.MethodCall()\u003C\/b\u003E:\n This is the constructor of MethodCall class, here you can find an \ninvokespecial call because this opcode is used for calling a special \nmethod like constructor or super etc. if you pay attention to the \ncommented line beside the invokespecial call you will find the method \ndetails\u003Cbr \/\u003E\n\u003Ci\u003E\u003Cb\u003E\u003Ci\u003E\/\/ Method \njava\/lang\/Object.\"\u0026lt;init\u0026gt;\":() V: This says constructor can be found\n in java.lang.object which is detonated by a special symbol \u0026lt;init\u0026gt;\n and it takes nothing as an argument\u003C\/i\u003E\u003C\/b\u003E\u003C\/i\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u0026nbsp;public void addCity(\u003C\/b\u003E\u003Cb\u003E)\u003C\/b\u003E\u003Ci\u003E\u003Cb\u003E\u003Ci\u003E:\u0026nbsp;\u003C\/i\u003E\u003C\/b\u003E \u003C\/i\u003EIn this section bytecode use invokeinterface opcode for\u0026nbsp; the line\u003Cbr \/\u003E\nList\u0026lt;String\u0026gt; city = new ArrayList\u0026lt;String\u0026gt;();\u003Cbr \/\u003E\ncity.add(\"Kolkata\");\u003Cbr \/\u003E\nand it is commented\u003Cbr \/\u003E\nas\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\/\/\n InterfaceMethod java\/util\/List.add:(Ljava\/lang\/Object;)Z which means \nadd is an interface method\u0026nbsp; which is in java.util.List and it takes \nObject as an input argument.\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nHere \ninvokeInterface opcode is used because, as we did the polymorphic \nassignment so at the compile time there is no way to know where is the \nactual add method implementation, so compiler has to put such opcode \nwhich will instruct JVM to dispatch the call to\u0026nbsp;exact method from Vtable\n at runtime, so method resolution happens at runtime.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u0026nbsp;public void addState(): \u003C\/b\u003E\u003Cbr \/\u003E\nIn this section bytecode use invokevirtual opcode for\u0026nbsp; the line\u003Cbr \/\u003E\nArrayList\u0026lt;String\u0026gt; state = new ArrayList\u0026lt;String\u0026gt;();\u003Cbr \/\u003E\nstate.add(\"WestBengal\");\u003Cbr \/\u003E\nand it is commented as\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\/\/\n Method java\/util\/ArrayList.add:(Ljava\/lang\/Object;)Z which means add \ncan be found in java.util.ArrayList and it takes Object as an input \nargument.\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nThere, is very subtle difference in \ncoding -- we use ArrayList instead of List so it is not a polymorphic \nassignment so it creates a huge difference in bytecode now bytecode \nknows the exact class where to find the add method at compile-time but \nstill call will be dispatched in runtime as if some other class can \nextend ArrayList. But it uses Invokevirtual opcode which is used for \ncalling an instance method.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003Epublic static void main(java.lang.String[]): \u003C\/b\u003EThe\n last section is entitled to\u0026nbsp;the main method where we call two instance \nmethods addCity and addState so it uses invokevirtual opcode.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003EConclusion\u003C\/b\u003E\n : In this article we have a fair bit of an idea how method call is \nhappened using different opcodes, But in Java7 an important opcode has \nbeen added that is invokeDynamic, which opens the door to allow \ndynamic\u0026nbsp;type language in JVM, so other languages which run on top of JVM\n uses this invokeDynamic opcode to make them dynamic language certain \nextent also Lambda Expression in Java8 uses the invokedynamic opcode, In\n my next tutorial I will give a detailed overview on\u0026nbsp;-- invokeDynamic \nopcode.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/javaonfly.blogspot.com\/feeds\/6105225966266033707\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2018\/01\/how-does-method-dispatch-happen-in-java.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/6105225966266033707"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/6105225966266033707"},{"rel":"alternate","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2018\/01\/how-does-method-dispatch-happen-in-java.html","title":"How does method dispatch happen in Java?"}],"author":[{"name":{"$t":"Shamik Mitra"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/02620539092954103493"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"32","height":"32","src":"\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjO2_A4uqRhlGdVcT56_sL024d9he6fReKq6CmpVwX7qqUKgBabEnhONHd9y8oFkNxgq80hXHMuC0mPAGRbIVw97aE63F0pwKB7fEKo397jRnCXRsc_1S1Px7VJSxmIsts\/s113\/534152_10200300633532106_2101616363_n.jpg"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEhvVz33iViIfmOpkpgQGtgmRFoy5f4004_uZNotiCJ_cf8V8hJldaxx2VX7zefKx4y7UmT4EgGav5c2KqSF9CBMqkQrXrYKxis913pvoUTrqUbPUWacdsI4RjLETgYjmwRl5VnM8TM5LG4\/s72-c\/method+call.jpeg","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-1902220484592789830.post-4312651746569888136"},"published":{"$t":"2017-12-18T22:38:00.001+05:30"},"updated":{"$t":"2017-12-18T22:57:13.166+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"java interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java interview questions"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java8"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java8 feature"}],"title":{"type":"text","$t":"Java8: Oogways more advice on Optional."},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\n\u003Cdiv dir=\"ltr\" id=\"docs-internal-guid-81e9035a-6a91-1200-2ceb-ad65d0e87a2c\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Ca href=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEgZHB8jnU_jaNXTZrrL4oC_fX5rYjTeYSA_J0aFC4CTy8yfV4Y0a1_RLdbmUvnfaP4sPmrRv53T3hMjbI4YwdEBjWOWVz-m7QiN0e07P7zYtjV2MQ1XiXkr-4d-GYXyvPpZj2evdnZCbZ4\/s1600\/Optional+OOgways.jpg\" imageanchor=\"1\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"680\" data-original-width=\"1600\" height=\"272\" src=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEgZHB8jnU_jaNXTZrrL4oC_fX5rYjTeYSA_J0aFC4CTy8yfV4Y0a1_RLdbmUvnfaP4sPmrRv53T3hMjbI4YwdEBjWOWVz-m7QiN0e07P7zYtjV2MQ1XiXkr-4d-GYXyvPpZj2evdnZCbZ4\/s640\/Optional+OOgways.jpg\" width=\"640\" \/\u003E\u003C\/a\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E\u003Ca href=\"http:\/\/javaonfly.blogspot.in\/2017\/12\/java8-optional.html\"\u003EOogway's previous talk\u003C\/a\u003E clears the confusions about Why Optional is added on java8? But PO is a Dragon warrior he is the finest Java warrior so he wants more, He wants to know when is the right time to use Optional. What are the best practices so he again went to Oogways and I am very lucky PO called me to take the note of the talk.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EHere is the Conversation.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EPO\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E: Master Oogway, I completely understood why we use Optional, the crux of the Optional is -- it gives the caller a hint that output may not be available so, design your code accordingly. So it is a Conceptual improvement which force caller to tackle maybe scenario and the outcome -- less null pointer exception. But How to use it efficiently.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOogways\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E: PO, Listen carefully Optional is created to check a return value is present or not, So it is the one and the only purpose you should use Optional nothing else. Optional act as a container it wraps the return value and then applies different functions on it to determine value is present or not in an advance if the value is present it can take necessary functions on it in a functional way. but whatever the case Optional must be used with method return type. Use Optional\u0026lt;T\u0026gt; as a Composition or pass it is an input is a very lame idea and should be avoided.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EPO\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E: What is the problem to pass Optional as an input suppose I have a program to search a name so if I pass the name as Optional\u0026lt;String\u0026gt; developer don't have to do the null check.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Esee the below program.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epackage com.example.optional;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.ArrayList;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.List;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.Optional;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epublic class OptionalTest {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eprivate static List\u0026lt;String\u0026gt;nameList = new ArrayList\u0026lt;String\u0026gt;();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Estatic {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"shamik\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"samir\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"swastika\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptional\u0026lt;String\u0026gt; findName(Optional\u0026lt;String\u0026gt; name){\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Ereturn name.isPresent()?Optional.of(nameList.get(nameList.indexOf(name.get().toLowerCase()))):Optional.empty();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epublic static void main(String[] args) {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptionalTest optionalTest = new OptionalTest();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptional\u0026lt;String\u0026gt; searchedNameOptional = optionalTest.findName(Optional.of(\"Shamik\"));\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptional\u0026lt;String\u0026gt; searchedNameOptionalSecond \u0026nbsp;= optionalTest.findName(Optional.ofNullable(null));\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EsearchedNameOptional.ifPresent(System.out::println);\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EsearchedNameOptionalSecond.ifPresent(System.out::println);\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EHere, I create a method called findName which takes an Optional\u0026lt;String \u0026gt;, So that developer can check if the value is present or not if present same returns an Otional\u0026lt;String\u0026gt; else returns an Empty optional, So no null check involved and passing Optional caller signal to the developer that passing parameter may be present or absent so deal accordingly. Nice way to tackle input parameters. Master then why you are telling passing an Optional in the input is a bad idea?\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOogways\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E: PO, There is a subtle conceptual error in your thinking, \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EYou are right Optional is used for signaling value can be present or absent, But think about \u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Ewho signaling to whom, here caller signaling to the author of the code\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E, The author is the creator of the code, author is very sure about nature of method input and returns value. As he wrote the method. So here signalling is meaningless, if caller only pass the name , the author knows value may be null as default value of String is null, so he can take care of it , So here use of Optional is redundant-- here Optional means Developer of the code reminds himself passing parameter may be present or absent-- just nonsense. Optional works fine for the client of the method as the client does not know about what the method is doing inside he only knows by call findName I can get an Optional\u0026lt;String \u0026gt;, So this method may give a blank result so I need to tackle it. But the reverse perspective is just absurd. There is no need to signal developer as he controls the implementation he knows what to do with inputs, so in this case null check is better than Optional, Another thing is -- by passing an Optional you create a wrapper on a value so it takes more memory space an unnecessary complex the code violation of KISS(keep it simple stupid), Also caller has to create Optional container which is break of encapsulation. So the best way to represent your code is like that\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epackage com.example.optional;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.ArrayList;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.List;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.Optional;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epublic class OptionalTest {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eprivate static List\u0026lt;String\u0026gt;nameList = new ArrayList\u0026lt;String\u0026gt;();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Estatic {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"shamik\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"samir\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"swastika\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptional\u0026lt;String\u0026gt; findName(String name){\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Ereturn name !=null?Optional.of(nameList.get(nameList.indexOf(name.toLowerCase()))):Optional.empty();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epublic static void main(String[] args) {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptionalTest optionalTest = new OptionalTest();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptional\u0026lt;String\u0026gt; searchedNameOptional = optionalTest.findName(\"Shamik\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptional\u0026lt;String\u0026gt; searchedNameOptionalSecond \u0026nbsp;= optionalTest.findName(null);\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EsearchedNameOptional.ifPresent(System.out::println);\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EsearchedNameOptionalSecond.ifPresent(System.out::println);\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EBy this, Developer put the null check, Always remember Optional is not an option for replace null or apply some functional fancy methods apply on value like (filter\/map) etc. The Optional is for signaling a value is present or not. So don't use it in composition or input variables for just sake for using Optional.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EPO\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E: Now, I understood the same master, Now please tell some of the operation we can do using Optional?\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOogways\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E: Yes PO Now we are in a position where we can use some of the Optional 's utility methods.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EorElse and orElseGet\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E: Sometimes, you are sure about what would be the default value if a value is not present in that case you can use orElse on the Optional. Suppose if the name is not found we show \"NA\" as default value, in that case, we can change the findName method as following\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epackage com.example.optional;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.ArrayList;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.List;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.Optional;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epublic class OptionalTest {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eprivate static List\u0026lt;String\u0026gt;nameList = new ArrayList\u0026lt;String\u0026gt;();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Estatic {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"shamik\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"samir\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"swastika\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epublic String findName(String name){\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Ereturn Optional.ofNullable(name).map(val-\u0026gt;nameList.get(nameList.indexOf(val.toLowerCase()))).orElse(\"NA\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epublic static void main(String[] args) {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptionalTest optionalTest = new OptionalTest();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EString blankName = optionalTest.findName(null);\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EString name = optionalTest.findName(\"Shamik\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003ESystem.out.println(\"Name is :: \" + blankName);\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003ESystem.out.println(\"Name is :: \" + name);\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EHere I use a map function which will check the name is in name List or not if it is not found it will return \"NA\" as in orElse I provide the default value as \"NA\".\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003ENow, If the default value fetched from Database or from a locale based properties file then we need to write a separate method which returns the default value, in that case, we can use that separate method as a reference and pass a supplier interface in the orElseGet method. See the following example.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EisPresent and ifPresent\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E : Optional has two utility functions called isPresent and ifPresent, former returns true if a value present later takes a callback function which will apply on the value if the value is present. So when you see a code block like following\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eif(optional.isPresent()){\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E \u0026nbsp;doSomething();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Ereplace the same with ifPresent\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eoptional.ifPresent(val-\u0026gt;doSomething())\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Esee the below Example,\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epackage com.example.optional;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.ArrayList;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.List;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.Optional;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epublic class OptionalTest {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eprivate static List\u0026lt;String\u0026gt;nameList = new ArrayList\u0026lt;String\u0026gt;();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Estatic {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"shamik\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"samir\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"swastika\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptional\u0026lt;String\u0026gt; findName(Optional\u0026lt;String\u0026gt; name){\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Ereturn name.isPresent()?Optional.of(nameList.get(nameList.indexOf(name.get().toLowerCase()))):Optional.empty();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epublic static void main(String[] args) {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptionalTest optionalTest = new OptionalTest();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptional\u0026lt;String\u0026gt; searchedNameOptional = optionalTest.findName(Optional.of(\"Shamik\"));\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eif(searchedNameOptional.isPresent()) {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003ESystem.out.println(searchedNameOptional.get());\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EsearchedNameOptional.ifPresent(System.out::println);\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EHere, I replace isPresent with ifPresent.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EflatMap: \u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EThe\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E \u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eflatmap function works same as map function, i.e change one data structure to another data structure but if the return \u0026nbsp;data structure holds an Optional it does not create a nested Optional Structure Optional\u0026lt;Optional\u0026lt;T\u0026gt;\u0026gt; it \u0026nbsp;just returns only Optiona\u0026lt;T\u0026gt; \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Esee the example\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epackage com.example.optional;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.ArrayList;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.List;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.Optional;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epublic class OptionalTest {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epublic void flatMapTest(String name) {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptional\u0026lt;String\u0026gt; retName = Optional.of(name).flatMap(val-\u0026gt;Optional.of(val));\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003ESystem.out.println(retName);\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epublic static void main(String[] args) {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptionalTest optionalTest = new OptionalTest();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EoptionalTest.flatMapTest(\"Shamik\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Ein FlatMap fuction I delibaretly return Optional.of(val) and as flatMap returns Otional\u0026lt;T\u0026gt; it should be then Optional\u0026lt;Optional\u0026lt;String\u0026gt;\u0026gt; but it returns only\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptional\u0026lt;String\u0026gt; if we use map function we got Optional\u0026lt;Optional\u0026lt;String\u0026gt;\u0026gt;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Efilter: \u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Ewe can use filter function on Optional so we can filter the value based on some criteria\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epackage com.example.optional;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.ArrayList;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.List;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eimport java.util.Optional;\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epublic class OptionalTest {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Eprivate static List\u0026lt;String\u0026gt;nameList = new ArrayList\u0026lt;String\u0026gt;();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Estatic {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"shamik\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"samir\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EnameList.add(\"swastika\");\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptional\u0026lt;String\u0026gt; findName(Optional\u0026lt;String\u0026gt; name){\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Ereturn name.isPresent()?Optional.of(nameList.get(nameList.indexOf(name.get().toLowerCase()))):Optional.empty();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003Epublic static void main(String[] args) {\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptionalTest optionalTest = new OptionalTest();\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOptional\u0026lt;String\u0026gt; searchedNameOptional = optionalTest.findName(Optional.of(\"Shamik\"));\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EsearchedNameOptional.filter(val-\u0026gt;val.contains(\"am\")).ifPresent(System.out::println);\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: blue; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EPO\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E: Master I learned all the important functions is anything still left to know about Optional?\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EOogWays\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E: Yes, PO we have covered 90% but one more thing is still pending why Optional is not Serializable, I will give you the answer but I want you to think about it. Tomorrow I will give the answer if you are not able to answer it.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003EPO\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;\"\u003E: Ok Master.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/javaonfly.blogspot.com\/feeds\/4312651746569888136\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2017\/12\/java8-optional-tutorial.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/4312651746569888136"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/4312651746569888136"},{"rel":"alternate","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2017\/12\/java8-optional-tutorial.html","title":"Java8: Oogways more advice on Optional."}],"author":[{"name":{"$t":"Shamik Mitra"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/02620539092954103493"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"32","height":"32","src":"\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjO2_A4uqRhlGdVcT56_sL024d9he6fReKq6CmpVwX7qqUKgBabEnhONHd9y8oFkNxgq80hXHMuC0mPAGRbIVw97aE63F0pwKB7fEKo397jRnCXRsc_1S1Px7VJSxmIsts\/s113\/534152_10200300633532106_2101616363_n.jpg"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEgZHB8jnU_jaNXTZrrL4oC_fX5rYjTeYSA_J0aFC4CTy8yfV4Y0a1_RLdbmUvnfaP4sPmrRv53T3hMjbI4YwdEBjWOWVz-m7QiN0e07P7zYtjV2MQ1XiXkr-4d-GYXyvPpZj2evdnZCbZ4\/s72-c\/Optional+OOgways.jpg","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-1902220484592789830.post-3239578767314912029"},"published":{"$t":"2017-08-05T13:49:00.000+05:30"},"updated":{"$t":"2017-08-06T11:03:40.887+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"java"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java interview questions"}],"title":{"type":"text","$t":"Is Data Abstraction and Encapsulation a fancy term of Information Hiding?"},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\n\u003Cdiv dir=\"ltr\" id=\"docs-internal-guid-3e467444-b178-0d83-3ce8-9810708ba184\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003EI have seen many developer\/Architect use the term interchangeably and has the reason for it but yes there are differences-- a huge difference in terms of hiding information. I try to explain it in a Simple way.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003ELet’s start by the definition.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EEncapsulation:\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E binds data and behaviors together in a Single unit and behaviors acts on the data.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EAbstraction\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E: Hiding the implementation details and expose the functionality to the world.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003EAccording to both definition, both try to hide data from rest of the world and expose some behaviors\/method so other System\/API can use it.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003EAt this point Both are same so If someone uses those term Interchangeably they are correct.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003EBut hold why then Two fancy concepts are side by side in OOP why they are not merged into a one and called it “\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EInformation hiding\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E”\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003ETo understand the same take a look Suppose you have to implement a car.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003ESo when you rotate the steering lots of thing happening inside and eventually car is moving to the direction you rotate the steering.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003ENow let explain the Action in details\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EInput\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E: Steering rotation direction.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EOutput\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E: car moves in the Direction steering rotated.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003Ebut what is happening inside is a black box to the user—\u003Cb\u003EThat is called Abstraction\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003ESo technically it says \u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EWhat to abstract from User\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E?\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cb\u003E\u003Ci\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-variant: normal; text-decoration: none; vertical-align: baseline;\"\u003EAbstraction is a functionality which helps the developer to identify what is the functionality that should be abstracted and exposed as a function\/method which takes User input and returns desired result what User wants.\u003C\/span\u003E\u003C\/i\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003EIn a car Steering functionality, Braking functionality, Auto parking --these are the functionalities has to be abstracted from User— User less interested How it works but what they interested is What should I do(Input) and What will be the Outcome. So according to me Abstraction is\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EAbstraction\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E: \u0026nbsp;By Abstraction, developers identify the functions what should be published in API and what input it takes and What Output it returns.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003ESo, another point of view is, \u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EAbstraction\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E helps us to the generalization of a functionality-- So When you design a function’s input or output you should be very careful about the data type you used-- It should be supported all possible combination on which function can be applied.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003ENow come to Encapsulation It tells about \u003Cb\u003EHow to achieve the functionality\u003C\/b\u003E-- \u003Cb\u003Ewhich has been identified by Abstraction.\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003ESo it tells us about the packaging the data and behaviors.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003ETake the same example use steering to move the car.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" id=\"docs-internal-guid-3e467444-b178-3df1-7318-f142524ca35d\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EEncapsulation:\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E identifies the different parts associate to move the car using user instruction like steering, Wheel, Engine.Petrol . Also, it identifies the algorithm\/behaviors which will be applied to these data(wheel, steering, engine, petrol) to move the car, and help to binds or packaging as one single unit. In my perspective Encapsulation definition is.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E\u003Cb\u003EEncapsulation:\u003C\/b\u003EEncapsulation Helps to understand what are the data and functions, that should be bundled as a Single Unit so User can act on them without knowing internal details and get the job done.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003Ca href=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEj91o0T7wutb3AcdQES-fXw4FXa43o_BMGJbr1RMy18yapzWw5z-GcVfcVbUkw6DNwdh7bIaoe-5Sp7_HyxB2sDcog-sm84XAUq8DecRqWs9_HKpPs7vSpIgY5OG-bJ6Iz1TCvHDrSIp8s\/s1600\/Is+Data+Abstraction+and+Encapsulation+a+fancy+term+of+Information+Hiding_.jpg\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg alt=\"Information Hiding\" border=\"0\" data-original-height=\"540\" data-original-width=\"720\" height=\"480\" src=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEj91o0T7wutb3AcdQES-fXw4FXa43o_BMGJbr1RMy18yapzWw5z-GcVfcVbUkw6DNwdh7bIaoe-5Sp7_HyxB2sDcog-sm84XAUq8DecRqWs9_HKpPs7vSpIgY5OG-bJ6Iz1TCvHDrSIp8s\/s640\/Is+Data+Abstraction+and+Encapsulation+a+fancy+term+of+Information+Hiding_.jpg\" title=\"Information Hiding\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cdiv dir=\"ltr\" id=\"docs-internal-guid-3e467444-b608-96a8-2f60-fed6c0acd55a\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EExplanation of the figure:\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E When you design an API\/Class always there is two perspective one is Developers View and one is API User view. From Developers View Abstraction is to identify the features to be provided and Encapsulation is the process to communicate with internals things and provide the functionality. So it makes sense to have two distinct terminology Abstraction and Encapsulation.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003EBut for User of the API\/Class, It is like what functionality is exposed and what is the input and what will be the output so functionality an API provides nothing but Opaque things to them they provide input and got Output --API or Class is a barrier or Facade for them So for them it is just an Information hiding so Abstraction and Encapsulation has no meaning for them. It can be used alternatively to mention information hiding.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EConclusion\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E : \u0026nbsp;Abstraction and Encapsulation both are used for hiding information context but their purpose is different. \u0026nbsp;Abstraction helps to understand the functionality User interested for and providing the same to the user as a black box. Encapsulation is about the gathers the required data and algorithm to solve the purpose for the user and tied them in a single Unit so the user of the API \u0026nbsp;doesn't have to collects the data and apply the algorithm by itself to get the job done.\u003C\/span\u003E\u003C\/div\u003E\n\u003C\/div\u003E\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/javaonfly.blogspot.com\/feeds\/3239578767314912029\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2017\/08\/is-data-abstraction-and-encapsulation.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/3239578767314912029"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/3239578767314912029"},{"rel":"alternate","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2017\/08\/is-data-abstraction-and-encapsulation.html","title":"Is Data Abstraction and Encapsulation a fancy term of Information Hiding?"}],"author":[{"name":{"$t":"Shamik Mitra"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/02620539092954103493"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"32","height":"32","src":"\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjO2_A4uqRhlGdVcT56_sL024d9he6fReKq6CmpVwX7qqUKgBabEnhONHd9y8oFkNxgq80hXHMuC0mPAGRbIVw97aE63F0pwKB7fEKo397jRnCXRsc_1S1Px7VJSxmIsts\/s113\/534152_10200300633532106_2101616363_n.jpg"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEj91o0T7wutb3AcdQES-fXw4FXa43o_BMGJbr1RMy18yapzWw5z-GcVfcVbUkw6DNwdh7bIaoe-5Sp7_HyxB2sDcog-sm84XAUq8DecRqWs9_HKpPs7vSpIgY5OG-bJ6Iz1TCvHDrSIp8s\/s72-c\/Is+Data+Abstraction+and+Encapsulation+a+fancy+term+of+Information+Hiding_.jpg","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-1902220484592789830.post-2545079832478585440"},"published":{"$t":"2017-06-17T19:13:00.000+05:30"},"updated":{"$t":"2017-07-07T12:31:06.309+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"CQRS"},{"scheme":"http://www.blogger.com/atom/ns#","term":"DDD"},{"scheme":"http://www.blogger.com/atom/ns#","term":"design pattern"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Domain driven design"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Event driven architecture"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Event Source"},{"scheme":"http://www.blogger.com/atom/ns#","term":"event sourcing"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java interview"},{"scheme":"http://www.blogger.com/atom/ns#","term":"java pattern"}],"title":{"type":"text","$t":"5 great points why you use event source solutions?"},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\n\u003Cdiv dir=\"ltr\" id=\"docs-internal-guid-b1d29791-b648-6212-8645-9d2b084f0d78\" style=\"line-height: 1.38; margin-bottom: 3pt; margin-top: 0pt; text-align: center;\"\u003E\n\u003Cspan style=\"background-color: blue; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 26pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003EThe Event Sourcing Pattern\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003EJoe has a habit whenever he did some transaction by his Debit card he used to write them in his Personal Diary so he can track his transactions. Joe is not a technology savvy and not able to check account statements online.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003EAt the month end, his bank SMS him his current balance but he immediately notices a discrepancy between current savings what bank shows and as per his calculation based on the Diary. He immediately calls Bank helpline and arguing about the discrepancy. Then the bank sends him an Account statement with all transactions recorded.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003EWhen he is trying to match transaction records with his diary he understood one transaction not in the Diary as that day he was so sick, he thought it to write it next day but somehow forgot.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EBut the question is what we can extract from this story?\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003E \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003EIf we look minutely we discover one fact that Bank always stores all the events\/transactions happens on the Account. Like Account creation, credit, debit etc. and the current balance is nothing but the outcome of those transactions. So what I meant to say is that account balance is not a fixed column in a database rather than it is a derivative\/outcome of the transactions\/events what were applied on the Account.We called it \u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EEvent Sourcing\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003ENow think what we generally do in software if we credit or debit any amount we just add or subtract that amount from current balance and update the account with new balance right.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003ESo we have the current state but lost the information how that state is achieved some system uses Audit trail still it is not fully deterministic. So anytime anyone challenges the system this is not the desired system state we don’t have any solid proof other than pleaded to them that system can not be wrong. But if you maintain that history or cause of the state changed like Bank then you just give them the History and asking to check -- a solid way to store proofs.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003E \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EThis is a very common story may anyone of us gone through the same and then look the Account statement for doubt clearing.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003E \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003E \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003ETechnically what is Even Sourcing?\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: white; color: #303633; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: italic; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EEvent Sourcing is a technique by that we store all the changes of application state as a sequence of events. we can rebuild the state anytime from those events, also can query on the events to construct the desired state.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003ESo the two key benefits are \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E1.we store all the events in a sequence which enables huge opportunities.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E2.The state is the derivative of events so we don’t have to maintain state in the database rather we can programmatically derive state based on the event.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003ENow this opens a new direction that we don’t have to persist state rather we can derive state and it bring many advantages I will talk about 5 such advantages.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Col style=\"margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cli dir=\"ltr\" style=\"background-color: transparent; color: black; font-family: Arial; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; list-style-type: decimal; text-decoration: none; vertical-align: baseline;\"\u003E\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EState Rebuild :\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E \u0026nbsp;As we stores every event applies on an application object, we can create a blank \/initial application object and apply every event in the same sequence it applied will bring the same state, so anywhere any point of time we can rebuild a state from events. So systems must have a mechanism to apply event, Then you can rebuild a state if the state is blown up for some reason. One may argue if your application state derives from millions of events applied on it, so computing all events may take time and also storing all events need a big storage area. but the fact is nowadays memory are really cheap also we can have TB of in memory space so computation is also faster, alternatively, we can store snapshot i.e \u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003Emilestone of the state\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E and apply event and rebuild state from latest snapshot.\u003C\/span\u003E\u003C\/div\u003E\n\u003C\/li\u003E\n\u003C\/ol\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E\u003Cimg alt=\"event source\" height=\"420\" src=\"https:\/\/docs.google.com\/drawings\/d\/sFkKhQzknOKeVfMs9yNLMjQ\/image?w=516\u0026amp;h=420\u0026amp;rev=415\u0026amp;ac=1\" style=\"border: none; transform: rotate(0rad);\" title=\"event source\" width=\"516\" \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E2. \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003ETemporal Query\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E : Event sourcing is perfect for Auditors. Business analysis team always want to see the past state so they can compare the growth or loss or any valuable statistical data so they need the flexibility to query the system in all possible way to collect statistical data. So If system has a feature to build the past state by passing parameters then analyst team will be delighted and the System which maintains all the state they can easily rebuild \/compute the state by the parameters provide by the analyst team say analyst want to see the Account details for 10th December 2016, by event sourcing we can fetch all events till 10 the December and apply them in sequence to build the state and return the result to analysts -- easy job isn’t it.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Ctable align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"tr-caption-container\" style=\"margin-left: auto; margin-right: auto; text-align: center;\"\u003E\u003Ctbody\u003E\n\u003Ctr\u003E\u003Ctd style=\"text-align: center;\"\u003E\u003Cimg height=\"381\" src=\"https:\/\/docs.google.com\/drawings\/d\/sh05sKyJKNRB1fPxRa99Qkw\/image?w=519\u0026amp;h=381\u0026amp;rev=171\u0026amp;ac=1\" style=\"border: none; margin-left: auto; margin-right: auto; transform: rotate(0rad);\" width=\"519\" \/\u003E\u003C\/td\u003E\u003C\/tr\u003E\n\u003Ctr\u003E\u003Ctd class=\"tr-caption\" style=\"text-align: center;\"\u003EAdd caption\u003C\/td\u003E\u003C\/tr\u003E\n\u003C\/tbody\u003E\u003C\/table\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E3. \u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EComparing State \u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E: Sometimes in a complex system, you need to know if events were applied in different ways what would be the outcome and how much deviation it cause from the current state say, A bank saving account interest rate is 8% previously it was 8.5. Now if the bank wants to know due to the decrease of the interest what is the actual amount bank benefits so they will replay events of 8.5 percents in all accounts and compare the state with current state to know the actual benefits although it is not very easy to implement but we can.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E\u003Cimg alt=\"what is event sourcing\" height=\"381\" src=\"https:\/\/docs.google.com\/drawings\/d\/sT-XAlJnG_v_nitIyNgNzlA\/image?w=519\u0026amp;h=381\u0026amp;rev=101\u0026amp;ac=1\" style=\"border: none; transform: rotate(0rad);\" title=\"what is event sourcing\" width=\"519\" \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E4. \u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EDebugging State \u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E: Suppose there is a bug in production system and we need to debug why the bug happens by event sourcing it is very easy like copy the Account in Dev environment then change the Log level to Debug and apply event one by one in the sequence and check the outcome is predicted or not ,if not then \u0026nbsp;found the Event and check how it applies to change the application state to found the defect.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E\u003Cimg alt=\"event source solutions\" height=\"381\" src=\"https:\/\/docs.google.com\/drawings\/d\/sOya-DUBimiWLvLjKPOuQaw\/image?w=519\u0026amp;h=381\u0026amp;rev=99\u0026amp;ac=1\" style=\"border: none; transform: rotate(0rad);\" title=\"event source solutions\" width=\"519\" \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E5. \u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EFuture Prediction\u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E : \u0026nbsp;In some Business domain it is important task to analysis what will be outcome if we take some business decision, if the outcome is successful they will take the decision,But in a naked eye it is impossible to predict the application state as different services are interlinked with each other and based on one event they can change, dependent services are subscribed to certain events when that event occurs they take action on basis of event value. \u0026nbsp;say A bank’s stock share worth is 8 INR but bank analysis team predict \u0026nbsp;within 1 month it will be increased to 12 INR and they have moreover 30K stocks are public so analysis team wants to know what will be the effects of the application state if stock worth is 12 INR so they will run some ad-hoc future events on top of current state \u0026nbsp;based on two criteria.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003ETaking per stock as 12 INR\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003ETaking per stock as 8 INR\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003EThen compare two application states to find out what are the effect of this stock value increase for each interlinked services.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E\u003Cimg alt=\"event sourcing benefits\" height=\"381\" src=\"https:\/\/docs.google.com\/drawings\/d\/s8wD5J_EI5b0M-UOhP0hvWQ\/image?w=519\u0026amp;h=381\u0026amp;rev=131\u0026amp;ac=1\" style=\"border: none; transform: rotate(0rad);\" title=\"event sourcing benefits\" width=\"519\" \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 700; text-decoration: none; vertical-align: baseline;\"\u003EConclusion \u003C\/span\u003E\u003Cspan style=\"background-color: transparent; color: black; font-family: \u0026quot;arial\u0026quot;; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline;\"\u003E: Some systems are inherently Event sourced like Version control (GIT), Banking application, Order Tracking application etc. but we can implement the same in general system also.Using Event sourcing you can easily back and forth you application state by replaying events and state cloning into any environment is just a matter of time but the Irony is, This pattern not used broadly in industry. \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv dir=\"ltr\" style=\"line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003C\/div\u003E\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/javaonfly.blogspot.com\/feeds\/2545079832478585440\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2017\/06\/5-great-points-why-you-use-event.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/2545079832478585440"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1902220484592789830\/posts\/default\/2545079832478585440"},{"rel":"alternate","type":"text/html","href":"https:\/\/javaonfly.blogspot.com\/2017\/06\/5-great-points-why-you-use-event.html","title":"5 great points why you use event source solutions?"}],"author":[{"name":{"$t":"Shamik Mitra"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/02620539092954103493"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"32","height":"32","src":"\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEjO2_A4uqRhlGdVcT56_sL024d9he6fReKq6CmpVwX7qqUKgBabEnhONHd9y8oFkNxgq80hXHMuC0mPAGRbIVw97aE63F0pwKB7fEKo397jRnCXRsc_1S1Px7VJSxmIsts\/s113\/534152_10200300633532106_2101616363_n.jpg"}}],"thr$total":{"$t":"0"}}]}});