Optional class is a public final class and used to deal with NullPointerException in Java application. You must import java.util package to use this class. It provides methods which are used to check the presence of value for particular variable.
четверг, 2 мая 2024 г.
Optional class in Java
вторник, 23 апреля 2024 г.
Date Time in Java
Java LocalDate class is an immutable class that represents Date with a default format of yyyy-mm-dd. It inherits Object class and implements the ChronoLocalDate interface.
Java LocalTime class is an immutable class that represents time with a default format of hour-minute-second. It inherits Object class and implements the Comparable interface.
Java LocalDateTime class is an immutable date-time object that represents a date-time, with the default format as yyyy-MM-dd HH-mm-ss.zzz. It inherits object class and implements the ChronoLocalDateTime interface.
Java Period class is used to measures time in years, months and days. It inherits the Object class and implements the ChronoPeriod interface.
Java Duration class is used to measures time in seconds and nanoseconds. It inherits the Object class and implements the Comparable interface.
понедельник, 22 апреля 2024 г.
Generics in Java
Generics in Java provide a way to create classes, interfaces, and methods that operate with types as parameters. They enable you to define classes and methods with placeholder types which are specified when the classes, interfaces, or methods are instantiated or called, respectively.
There are mainly 3 advantages of generics. They are as follows:
1) Type-safety: We can hold only a single type of objects in generics.
2) Type casting is not required: There is no need to typecast the object.
Generic class
A class that can refer to any type is known as a generic class. Here, we are using the T type parameter to create the generic class of specific type.
Type Parameters
The type parameters naming conventions are important to learn generics thoroughly. The common type parameters are as follows:
- T - Type
- E - Element
- K - Key
- N - Number
- V - Value
Like the generic class, we can create a generic method that can accept any type of arguments. Here, the scope of arguments is limited to the method where it is declared. It allows static as well as non-static methods.
Wildcard in Generics
Wildcard is a special symbol used in type declarations to represent an unknown type. Wildcards are denoted by the ?
character and are used to make generic types more flexible.
An upper bounded wildcard in Java generics, denoted by ? extends T
, allows you to specify that a generic type can be any subtype of a particular type T
or T
itself. This means that the generic type can be any type that is a subclass of T
. Upper bounded wildcards are useful when you want to make your code more flexible by accepting collections of any type that is a subtype of a particular class or interface.
A lower bounded wildcard in Java generics, denoted by ? super T
, allows you to specify that a generic type can be any supertype of a particular type T
or T
itself. This means that the generic type can be any type that is a superclass of T
. Lower bounded wildcards are useful when you want to make your code more flexible by accepting collections of any type that is a supertype of a particular class or interface.
суббота, 6 апреля 2024 г.
Prototype Design Pattern
Prototype pattern refers to creating duplicate object while keeping performance in mind. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
Prototype design pattern is used when the Object creation is a costly affair and requires a lot of time and resources and you have a similar object already existing. Prototype pattern provides a mechanism to copy the original object to a new object and then modify it according to our needs. Prototype design pattern uses java cloning to copy the object.
Output of the above prototype design pattern example program is:
Builder Design Pattern
Builder pattern builds a complex object using simple objects and using a step by step approach. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
Builder pattern was introduced to solve some of the problems with Factory and Abstract Factory design patterns when the Object contains a lot of attributes. There are three major issues with Factory and Abstract Factory design patterns when the Object contains a lot of attributes.
- Too Many arguments to pass from client program to the Factory class that can be error prone because most of the time, the type of arguments are same and from client side its hard to maintain the order of the argument.
- Some of the parameters might be optional but in Factory pattern, we are forced to send all the parameters and optional parameters need to send as NULL.
- If the object is heavy and its creation is complex, then all that complexity will be part of Factory classes that is confusing.
пятница, 5 апреля 2024 г.
Abstract Factory Design Pattern
Abstract Factory patterns work around a super-factory which creates other factories. This factory is also called as factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.