0% found this document useful (0 votes)
2 views3 pages

Optional.ofNullable()

The document discusses the Optional design pattern in Java, which helps reduce null checks and improve code readability through methods like Optional.ofNullable(), orElse(), orElseGet(), and orElseThrow(). It also highlights new features introduced in Java 11, including string manipulation methods and file reading/writing capabilities. Additionally, the document provides insights into static variables and methods, the final keyword, and a brief overview of the author's experience and current project involving the CitiKYC application.

Uploaded by

Karthik Fazer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Optional.ofNullable()

The document discusses the Optional design pattern in Java, which helps reduce null checks and improve code readability through methods like Optional.ofNullable(), orElse(), orElseGet(), and orElseThrow(). It also highlights new features introduced in Java 11, including string manipulation methods and file reading/writing capabilities. Additionally, the document provides insights into static variables and methods, the final keyword, and a brief overview of the author's experience and current project involving the CitiKYC application.

Uploaded by

Karthik Fazer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

The Optional design pattern that was designed to reduce the number of null checks

in code. With Optional, you can write cleaner and more concise code that is easier
to maintain.

Optional.ofNullable()
The Optional.ofNullable() method returns a Non-empty Optional if a value present in
the given object. Otherwise returns empty Optional. Optional.empty() method is
useful to create an empty Optional object.
Optional<String> opt = Optional.ofNullable("Ramesh");
if (opt.isPresent()) { System.out.println(opt.get().getName());}

orElse
It is used to return the value wrapped in the Optional, if it is present. if the
Optional is empty, It takes a default value from an argument.
String name = Optional.ofNullable(nullName).orElse(new Student("no one", 0,
"Unknown"));
String name = Optional.ofNullable(nullName).orElseGet(() -> "Ramesh");
System.out.println(name);

orElseGet
String name = Optional.ofNullable(nullName).orElseGet(() -> "Ramesh");
System.out.println(name);

orElseThrow
This will throw exception
String name =
Optional.ofNullable(nullName).orElseThrow(IllegalArgumentException::new);
System.out.println(name);

Optional.of()
The Optional.ofNullable() method returns a Non-empty Optional if a value present in
the given object. Otherwise returns java.lang.NullPointerException
Optional<String> opt = Optional.of("Ramesh");

empty()
this method will simply generate an empty Optional (no value ).
Optional<T> empty = Optional.empty();

orElse() vs orElseGet()
orElse() will always call the given function whether you want it or not, regardless
of Optional.isPresent() value
orElseGet() will only call the given function when the Optional.isPresent() ==
false
In real code, you might want to consider the second approach when the required
resource is expensive to get.

// Always get heavy resource


getResource(resourceId).orElse(getHeavyResource());

// Get heavy resource when required. used Supplier


getResource(resourceId).orElseGet(() -> getHeavyResource())

-----------------------------------------------------------------------------------
--
JAVA 11 features :

Among other changes, JDK 11 introduces 6 new methods for java.lang.String class:
repeat(int) - Repeats the String as many times as provided by the int parameter
lines() - Uses a Spliterator to lazily provide lines from the source string
isBlank() - Indicates if the String is empty or contains only white space
characters
stripLeading() - Removes the white space from the beginning
stripTrailing() - Removes the white space from the end
strip() - Removes the white space from both, beginning and the end of string

Method Origin Basis White Space Handling


trim() Early Java ASCII values Limited (<=32 ASCII)
strip() Java 11 Unicode characters Comprehensive

readString() & writeString()


Path localFile = Path.of("C:\\MaryZheng\\Temp\\test.txt");
String sampleString = "Some example of text";
Files.writeString(localFile, sampleString);
String readData = Files.readString(localFile);

-----------------------------------------------------------------------------------
---
Static---
Memory efficiency: It is allocated memory only once during the execution of the
program, which can resuse.

A Static block code is executed only once when the class is loaded into memory.
Mostly it’s used to create static resources when the class is loaded. We can’t
access non-static variables in the static block.
A static method and attributes, we can access without creating an object of a class
A static method can access only static variables of class and invoke only static
methods of the class.

The static keyword means the value is the same for every instance of the class.

static block vs constructor

Final---
A final class cannot be subclassed.
A final method can't be overridden by subclasses
A final keyword once the variable is assigned a value it can never be changed.

-----------------------------------------------------------------------------------
------------
Hi, I’m Karthik and I have tatally 8.5 years experience in softwre development. I
specialize in Java, Spring boot, microservices and extensive experience in
Healthcare, Insurance, Automobile and Banking domains. I am passionate about
staying up-to-date with the latest technologies and trends in the industry. My
goal is to continue to develop my skills and work on projects that make a positive
impact on society.

101262492346
About my project,
Currently working on CitiKYC application for last three years and it is used to
validate the customer details before account opening in citi bank.

CitiKYC application is used to validate the customer details before account opening
in citi bank.
This application has different login users and each user has different roles like
KYC Maker, KYC Checker, KYC Owner, Management.
Once login, In the application inbox we can find customer kyc records. Each kyc
record contains different sections like client profile Identification and
verification, Screening Additional and Beneficial members. Once all sections
verified, user able to submit the kyc record to next level user queue. Once passes
all queue's, customer can open the account in citi bank.

we have developed the microservices for each section crud operations by using
springboot, microservices.
code repository maintained by bitbucket. tasks managed by JIRA.
Using jenkins to Build and deployment by tectia, openshift.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy