Posts

Java - Builder Design Pattern - Example

Builder is a creational pattern that provides an efficient way to build objects. Compared with the traditional way of using multiple constructors, using the Builder pattern can ensure the readability and stability of the code. This mode is particularly useful when creating complex objects. Use Builder pattern in the following situations: When the algorithm for creating a complex object should be independent of the object's components and how they are assembled When the construction process must allow the constructed object to have different representation Advantages 1. It provides a clear disseverment between the construction and representation of an object. 2. It provides better control over the construction process. 3. It fortifies transmuting the internal representation of objects. Implementation   Let's see an example and learn how to implement a builder pattern. Consider a POJO of Employee below. public class Employee { // All final attributes private final S

Java-Linked List with example

Image
The java.util.LinkedList class operations perform we can expect for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index. The important points about Java LinkedList are:  It is an ordered set comprising a variable number of data items. No need to specify; grow and shrink during execution. Element position is assigned during run time. Stored randomly Insertion and deletion of element will be Easier, fast and efficient. Compared to array ,Linked list required more memory Hierarchy of LinkedList class Method & Description boolean add(E e): It is used to append the specified element to the end of a list. void add(int index, E element): It is used to insert the specified element at the specified position index in a list. boolean addAll(Collection<? extends E> c): It is used to append all of the elements in the specified collection to the end

Java -Loops

Image
Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. In Java we have three types of basic loops for loop do-while loop while loop 1. while loop A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. Syntax: while ( boolean condition) { statements... } Note: The important point to note when using the while loop is that we need to use increment or decrement statement inside the while loop so that the loop variable gets changed on each iteration, and at some point, the condition returns false. This way we can end the execution of the while loop otherwise the loop would execute indefinitely. Simple while loop example public class KnfWhileLoopExample { public static void main( String [] args) { int i = 0 ; while (i <= 10 ) { System.out.println(i); i++;

Java-Data Types-example

Image
Variables reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables. There are two types of data types in Java: Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double. Non-primitive data types:  he non-primitive data types include Classes, Interfaces, and Arrays. Primitive data Primitive data are only single values; they have no special capabilities. There are 8 primitive data types boolean: boolean data type represents one bit of information There are only two possible values: true and false This data type is used for simple flags that track true/false conditions Def

Java -Singleton class-Singleton Design pattern-Why?How?

Image
Singleton class means you can create only one object for the given class. You can create a singleton class by making its constructor private so that you can restrict the creation of the object. Provide a static method to get the instance of the object, wherein you can handle the object creation inside the class only.  Advantage of Singleton design pattern Saves memory because the object is not created at each request. Only a single instance is reused again and again. Usage of Singleton design pattern Singleton pattern is mostly used in multi-threaded and database applications. It is used in logging, caching, thread pools, configuration settings, etc. How to create a Singleton design pattern? To create the singleton class, we need to have a static member of the class, a private constructor, and a static factory method. Static member:  It gets memory only once because of static, it contains the instance of the Singleton class. Private constructor:  It will prevent to instantiate