الأحد، 20 يوليو 2025

Java 8 - How to convert Calendar to LocalDateTime?

1. Overview

In this article, You'll learn how to convert Calendar to LocalDateTime object in java 8 DateTime api

LocalDateTime api can be used completely replacement to the Date class as all features of Date functions are done in a simple and precise way such as adding minutes to the current date.

Java 8 - How to convert Calendar to LocalDateTime?

Ultimate Guide to Java Virtual Threads: Build Scalable Applications with Ease

Are Java Virtual Threads the Future of Concurrency? Absolutely. Here's Why You Should Care.

If you're a Java developer, you already know: every line of code runs on a thread. Threads are the fundamental unit of scheduling and concurrency in any Java application.

Traditionally, developers have relied on multiple OS-level threads to handle concurrent tasks. For example, Spring Boot with embedded Tomcat comes configured with 200 threads by default, allowing it to process up to 200 concurrent requests. Sounds powerful, right?

But here’s the catch...

🧠 Why Traditional Threads Limit Scalability

Each request in a Java web application is processed by a dedicated OS thread. If each request takes 1 second, a 200-thread system can handle 200 requests per second — that’s your application throughput.

So, why not just add more threads?

Because OS threads are heavyweight:

Each thread consumes significant memory.

The OS has limits on how many threads can be created.

Managing thousands of threads leads to context switching overhead, degrading performance.

That’s where Java Virtual Threads come into play — a game-changing concurrency model introduced in Project Loom.

السبت، 19 يوليو 2025

Mastering @JsonFormat in Jackson: A Complete Guide with Examples

If you're working with Java and JSON, the Jackson library is likely part of your tech stack. One of the most powerful yet underused annotations it provides is @JsonFormat.

In this ultimate guide, we’ll break down what @JsonFormat does, when to use it, and how to handle advanced scenarios with fully working examples. This post is optimized for SEO to help developers find quick and accurate answers.


✅ What is @JsonFormat in Jackson?

@JsonFormat is a Jackson annotation that helps control how Java fields are serialized (Java to JSON) or deserialized (JSON to Java). This is especially useful for dates, enums, and number formats.

Import Statement:


import com.fasterxml.jackson.annotation.JsonFormat;

Dependencies

Make sure you include the correct Jackson modules for java.time:


 <dependency>
   <groupId>com.fasterxml.jackson.datatype</groupId>
   <artifactId>jackson-datatype-jsr310</artifactId>
   <version>2.15.3</version>
 </dependency>
 

And register it:


 ObjectMapper mapper = new ObjectMapper();
 mapper.registerModule(new JavaTimeModule());
 

الخميس، 5 سبتمبر 2024

Java Program To Reverse A String Without Using String Inbuilt Function reverse()

1. Introduction


In this tutorial, You'll learn how to write a java program to reverse a string without using string inbuilt function reverse().
This is a very common interview question that can be asked in many forms as below.

A) java program to reverse a string without using string inbuilt function
B) java program to reverse a string using recursion
C) java program to reverse a string without using the reverse method
C) Try using other classes of Java API (Except String).



The interviewer's main intention is not to use the String class reverse() method.

Anonymous Comparator In Java For Sorting

1. Overview

In this tutorial, We'll learn how to sort arrays or lists using an anonymous comparator in java.

A complete guide on how to use comparator in java?

Anonymous class means creating the class and providing the implementation at the same without a class name.

For example, we have an interface Job and it has the method post() to post the new job. here, a new job1 instance is created for the implementation class of Job interface without a class name.

Example 1

interface Job {
	void post();
}

Job job1 = new Job() {

	@Override
	public void post() {
		System.out.println("Posting job 1 now");

}


This is similar to the anonymous implementation of the abstract class or interface. This is the actual implementation of the Comparator without creating a class and implementing the Comparator interface.

الأحد، 1 سبتمبر 2024

Java Thread.join() Examples to Wait for Another Thread

1. Introduction


In this tutorial, We'll learn how to use Thread.join() method in java. And also how to join the multiple threads at one place after completing the execution of all threads or one thread or any other threads.

join() method is part of the Thread class and it is part of the java.lang package. All the classes in java.lang package is not needed to add import statements. So, directly you can use it in the programs. join() is mainly used to sleep or wait the current thread until completion of another thread execution.

Similar to the wait() and notify(), join() also widely used in the thread intercommunication.

2. Thread.join() Syntax


below is the syntax from Thread API. join() method is an overloaded method so we should be careful which method should be used.

public final void join() throws InterruptedException

public final void join(long millis) throws InterruptedException

public final void join(long millis, int nanos) throws InterruptedException


Java 8 String API also has join() method.

الجمعة، 7 يناير 2022

Java Program To Find First Non-Repeated Character In String (5 ways)

1. Overview


In this article, We will be learning and understanding the various ways to find the first non repeated character in a given string in various ways along with Java 8 Streams. Many programs may face this type of question in programming interviews or face to face round.
Let us start with the simple approach and next discover the partial and single iteration through the string.

Java Program To Find First Non-Repeated Character In String (5 ways)