Posts

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

Java - Inheritance in java with example-How to use inheritance in Java?

Image
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviours of a parent object. It is an important part of OOPs (Object Oriented programming system). The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields to your current class also. Inheritance represents the IS-A relationship which is also known as a parent-child relationship. "The main advantage of Inheritance is Code Reusability" Important terminology Super Class/Parent Class : The class whose features are inherited is known as superclass(or a base class or a parent class). Sub Class/Child Class:   The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the supe

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