Posts

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

Spring Boot-Schedule a task with SpringBoot-Download Source Code

Hello everyone, today we will learn how to schedule tasks in Spring Boot using @Scheduled annotation. To schedule jobs in the spring boot application to run periodically, spring boot provides @EnableScheduling and @Scheduled annotations. Add @EnableScheduling to Spring Boot Application class Add @EnableScheduling annotation to your spring boot application class. @EnableScheduling is a Spring Context module annotation. It internally imports the SchedulingConfiguration via the @Import(SchedulingConfiguration.class) instruction @ SpringBootApplication @ EnableScheduling public class KnowledgefactoryScheduler { public static void main( String [] args) { SpringApplication.run(KnowledgefactoryScheduler.class, args); } } 1. Scheduling a Task with Fixed Rate You can schedule a method to be executed at a fixed interval by using fixedRate parameter in the @Scheduled annotation. In the following example, The annotated method

Java -Exception-hierarchy-types-example

Image
An exception is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled. The Exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner. Exception Hierarchy All exception and errors types are sub-classes of class Throwable. One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. IndexOutOfBoundsException is an example of such an exception. Another branch, Error is used by the Java run-time system(JVM) to indicate errors having to do with the run-time environment itself(JRE). OutOfMemoryError is an example of such an error. We have three categories of Exceptions Checked exceptions     A checked exception is an exception that is checked (no