Tag: java
-
Java 21 – Scoped Values
How do you pass a value from one part of your code to another. Let’s say from one class to another. By using method arguments. Is there any other way that you can access a shared variable? For example, Let’s say you need to pass the user name who logged into your web app from…
-
Java 21 – Structured Concurrency
Let’s say you want to perform a group of tasks in Java. And all these tasks are part of a single unit of work. And all these tasks can be executed independently. How can this be done in Java? You can execute each task sequentially. So you execute task 1 , then task 2 and…
-
Java 21 – Virtual Threads
Java supports concurrency. You can perform multiple tasks at the same time in a program. And how does Java implement this? Using Threads! A thread is the basic unit of concurrency in Java. If you want to perform multiple tasks in parallel you create one thread for each task and perform them in parallel. And…
-
Java 21 – Sequenced Collections
How do you get the last element of a list in Java? Using a code similar to below: That doesn’t look elegant! And how do you get the last element for a Sorted Set? That looks much more elegant but then different collections are having different ways to get the last element. There is no…
-
Java 21 – String templates
Contents: The Problem “String” is a basic and widely used data type in any programming language. Most of the data we deal with are texts which are represented by String datatype. One of the operations we often do on text is replacing part of the text with dynamic values . This is called interpolation. Languages…
-
Java 21 Features – Unnamed classes and instance main methods
Java 21 allows you to print Hello World message without creating a class and using public static keyword for main class.
-
Java 21 – Unnamed Patterns and Variables
Let’s say you have declared a variable in your Java program. But you are not going to use it for some reason. May be you declared an exception variable in a catch block but not going to use the variable at all: Or in a for loop statement: Or in Pattern Matching of Records (introduced…
-
Java 21 – Record Patterns
Pattern Matching is a new area where Java has been introducing quite a few changes recently. It started with instanceof operator. Previously in Java , if you had to access an object whose instance type you didnt know prior, you had to use instanceof operator this way: You had to do a cast operation as…
-
How to share data across multiple components for a single request in a Spring Boot application?
Let’s say that you make a REST API request to a spring boot web application. There is a security requirement that you need to log the user id across all the method calls in the application for this particular request. Something like this: So , if your application flow starts from a controller class and…
-
The Real Benefits of Java Lambdas
Java 8 introduced the concept of Lambda expressions. They provide a concise way to represent functions as objects. A lambda expression is essentially an anonymous function that can be treated as an object and passed around as an argument to methods or stored in variables. Here is an example: In this example, the MathOperation interface…