Blog

public class Test { public static void main(String[] args) { // create an object of Random class Random random = new Random(); // Generate random integer 0 to 100 int randomInteger1 = random.nextInt(101); // Generate random integer 0 to 10000 int randomInteger2 = random.nextInt(10001); // Generate random double value double randomDouble = random.nextDouble(); // Generate…

Read More Generate a Random Number in Java

public class Test { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(7, 19, 14, 1, 12, 22, 18, 3, 28, 5)); System.out.println(“Maximum value: ” + Collections.max(list)); System.out.println(“Minimum value: ” + Collections.min(list)); } } Output: Maximum value: 28 Minimum value: 1 Finding the maximum and minimum values in List using the Java 8…

Read More Find Max and Min Values of a List in Java

Output: Java   Methods from the StringUtils class that we can use to get a substring from String in Java: import org.apache.commons.lang3.StringUtils; public class Test { public static void main(String[] args) { String str = “Hello, my name is Tom, and I live in USA”; String substringBefore = StringUtils.substringBefore(str, “,”); String substringAfter = StringUtils.substringAfter(str, “,”); String substringBetween = StringUtils.substringBetween(str,…

Read More Get Substring from String in Java

In this tutorial, I’ll guide you on how to use the new OAuth2 Authorization Server(v0.0.3) built by the Spring Team. Below are the new features that have been introduced in the newer version: Enforce one-time use for authorization code Introduce OAuth2 Tokens Add Refresh Token grant Implement Token Revocation Endpoint OAuth2 is an authorization method…

Read More The New OAuth2 Authorization Server

public class GenericClass <T> GenericClass<Type> obj = new GenericClass<Type>(); List<String> list = new ArrayList<>(); Map<Integer, String> map = new HashMap<>(); Set<String> set = new HashSet<>(); public class Test <T> { private T data; public Test(T data) { this.data = data; } public static void main(String[] args) { Test<Integer> test1 = new Test<>(10); Test<String> test2 =…

Read More Generics in Java with Examples

public class TestMultithreading extends Thread { public void run() { System.out.println(“The thread is running…”); } public static void main(String args[]) { TestMultithreading test = new TestMultithreading(); test.start(); } } Output: The thread is running…   Get the id and name of a thread public class TestMultithreading extends Thread { public void run() { System.out.println(“ID: “…

Read More Multithreading in Java

import java.io.Serializable; public class User implements Serializable { transient int userId; String name; public User(int userId, String name) { this.userId = userId; this.name = name; } } import java.io.* public class Test { public static void main(String args[]) { try { User user1 = new User(15, “Ryan”); //Write the object in a stream FileOutputStream outputStream…

Read More Transient Keyword in Java (Explained!)

In this tutorial, we will cover the basics of serialization and deserialization, including the importance of the ObjectOutputStream and ObjectInputStream classes, the use of the transient keyword, and the implementation of custom serialization methods. We’ll also provide code examples and address some common questions about these concepts. Let’s get started! What are Serialization and Deserialization…

Read More Serialization and Deserialization in Java: A Comprehensive Guide

public void printData(int data) { System.out.println(data); printData(data); } public static void printData(int data) { if (data > 0) { System.out.println(data); printData(–data); } } Output: 5 4 3 2 1 public class Test { public static void main(String[] args) { System.out.println(factorial(5)); } static int factorial(int num) { if (num == 1) { return 1; } else…

Read More What is Recursion in Java?

class ClassA implements Cloneable { private int someNum; private ClassC nestedObjectReference; @Override public Object clone() throws CloneNotSupportedException { ClassA classA = (ClassA) super.clone(); // with this, we are ensuring that deep copy will be performed classA.setNestedObjectReference((ClassC) this.nestedObjectReference.clone()); return classA; } // constructor, getters and setters } class ClassC implements Cloneable { private String classData; public…

Read More What is a Deep Copy in Java?

An important property of modern web apps is resilience. In simple terms, resilience refers to the ability of a system or feature to fail gracefully without impacting the entire system. In the context of web apps, we want to ensure that the entire system will not go down if a remote service, such as a…

Read More Enforcing Resilience in Spring Boot with Resilience4J

protected Object clone() throws CloneNotSupportedException class ClassA implements Cloneable { private int someNum; private ClassC nestedObjectReference; @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } // getter and setter methods } class ClassC { private String classData; public ClassC(String classData) { this.classData = classData; } // getter and setter methods } public class Test…

Read More Object Cloning in Java