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

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

Java - Blowfish Encryption and decryption Example

Image
Blowfish is an encryption method developed by Bruce Schneier in 1993 as an alternative to the DES encryption method. It is significantly faster than DES and provides good encryption speed, although no effective cryptanalysis technique has been found to date. It is one of the first secure block ciphers that is not protected by any patents and is therefore freely available for anyone to use. This is a symmetric block cipher algorithm. Block size: 64 bits Key size: variable size from 32 to 448 bits number of subsections: 18 [P-array] number of rounds: 16 number of substitution blocks: 4 [each with 512 records of 32 bits each] develop  Example 1: Encryption and decryption using Blowfish import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Base64; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeExc