java

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?

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

In this tutorial, you will learn how to call stored procedures in a Spring Boot RESTful API project that uses Spring Data JPA. Using a stored procedure to execute parts of the application logic in the database is a widely used approach in huge, data-heavy applications. A stored procedure is a group of predefined SQL…

Read More Calling a Stored Procedure in Spring Boot REST with JPA

Multiple data source implementations are very crucial in instances where we want to secure the application from vulnerabilities such as a database failure. This will ensure that the application can still function even if part of the database went down. This tutorial will use student and course entities to demonstrate how multiple data sources can…

Read More Multiple Datasources in Spring Application

In this tutorial, you will learn how to use specification and predicate in Spring Data JPA using the Spring Boot RESTful API project. Spring Data JPA Specifications allow us to create dynamic database queries by using the JPA Criteria API. It defines a specification as a predicate over an entity. Spring has a wrapper around…

Read More Specification & Predicate: Advance Search and Filtering in JPA

This tutorial will be part of our Series on Reactive Programming in Java and Spring Framework. If you have not read the previous article, I would strongly advise you to do so. This will help you to understand better as we go further. In the previous blog post, we discussed publishers and subscribers in Java.…

Read More Reactive Programming: Creating Publishers and Subscribers in Java.

public sealed class Vehicle permits Car, Truck, Bus {} final class Bus extends Vehicle { } non-sealed class Truck extends Account { } sealed class Car permits BlueCar, RedCar{ } public abstract sealed class User permits PremiumUser, AdvancedUser { public abstract String getUserData(); } non-sealed class PremiumUser extends User { @Override public String getUserData() {…

Read More Sealed Classes and Interfaces in Java

class Test { public static void main(String[] args) { String name = null; System.out.println(name.toUpperCase()); } } Output: Exception in thread “main” java.lang.NullPointerException at com.example.Test.main (Test.java:9)   Here, we got an error message that the NPE has occurred on line 9. Unfortunately, we can’t conclude which value exactly was null from the message. class Test { public static…

Read More Helpful NullPointerExceptions (NPE) in Java

class User { private String name; private String username; private String membershipType; private String address; public User(String name, String username, String membershipType, String address) { this.name = name; this.username = username; this.membershipType = membershipType; this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public…

Read More Java Records