Tag: java21

  • 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 – 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 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

    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…