Posts

Java 25 Features

Java 25 Features Major Features in Java SE 25 1. Scoped Values (Finalized) Scoped values provide a safer alternative to thread-local variables, allowing values to be shared across threads in a controlled way. ```java import java.util.concurrent.Executors; import jdk.incubator.concurrent.ScopedValue; public class ScopedValueDemo {     static final ScopedValue<String> USER = ScopedValue.newInstance();     public static void main(String[] args) {         ScopedValue.where(USER, "Alice").run(() -> {             System.out.println("Running as user: " + USER.get());             Executors.newSingleThreadExecutor().submit(() ->                 System.out.println("Child thread user: " + USER.get())             );         });     } } ``` 2. Flexible Constructor Bodies Constructors now all...

Advance Java

SERVLETS Java Servlets are server-side Java programs that handle client requests and generate dynamic web content, typically in response to HTTP requests.  They act as a middle layer between a web browser (or other client) and a backend database or application. What Exactly Is a Servlet? A Servlet is a Java class that runs inside a Servlet container (like Apache Tomcat) and extends the capabilities of servers that host applications accessed via a request-response model.  It’s a core component of Java EE (now Jakarta EE) for building web applications. Key Features - Runs on the server-side : Handles HTTP requests and responses. - Dynamic content generation : Can generate HTML, JSON, XML, etc. - Efficient and scalable : Better performance than older CGI-based solutions. - Platform-independent : Written in Java, runs on any OS with a compatible JVM. - Session management : Supports cookies, URL rewriting, and HTTPS sessions. Servlet Lifecycle 1. Initialization (` init() `): Called...

Java 11 features

Java 11 features 1. New HTTP Client API (Standardized) Replaces `HttpURLConnection` with a modern, fluent API. ```java HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder()     .uri(URI.create("https://api.github.com"))     .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body()); ``` 2. String API Enhancements ```java String str = "  Hello Java 11  "; System.out.println(str.isBlank());              // false System.out.println(str.strip());                // "Hello Java 11" System.out.println(str.stripLeading());         // "Hello Java 11  " System.out.println(str.stripTrailing());        // "  Hello Java 11" System.out.println("Java".repeat(3));           // "JavaJavaJava"...

JMS (Java Message Service)

JMS (Java Message Service) Java Message Service (JMS) is a Java API for sending and receiving messages between distributed systems, enabling loosely coupled, reliable, and asynchronous communication. What Is JMS? - JMS (Java Message Service) is a standard API provided by Java EE for messaging between software components in distributed applications. - It allows asynchronous communication, meaning the sender and receiver do not need to interact with the message at the same time. - JMS is designed to be loosely coupled, which improves scalability and flexibility in system architecture. Core Concepts - Message : The data being transmitted. JMS supports different message types like `TextMessage`, `ObjectMessage`, `BytesMessage`, etc. - Producer : The component that sends messages. - Consumer : The component that receives messages. - Destination : The target for messages, which can be a **Queue** (point-to-point) or a **Topic** (publish-subscribe). - ConnectionFactory : Used to create connec...

JPA

JPA Stored Procedure Q. Calling a Stored Procedure in JPA? In JPA (Java Persistence API), calling a stored procedure is typically done using the ` @NamedStoredProcedureQuery ` annotation or the ` EntityManager ` API .  Here's a breakdown of how it works, especially in the context of Spring Boot + JPA, which you're already fluent with: 1. Using `@NamedStoredProcedureQuery` (Annotation-Based) Step-by-step setup : a. Define the stored procedure in your entity : ```java @Entity @ NamedStoredProcedureQuery (     name = "User.getUserByEmail",     procedureName = "get_user_by_email",     parameters = {         @StoredProcedureParameter(mode = ParameterMode.IN, name = "email", type = String.class),         @StoredProcedureParameter(mode = ParameterMode.OUT, name = "user_id", type = Long.class)     } ) public class User {     @Id     private Long id;     private String email; ...

OOP OOAD

Object  means a real-world entity such as a pen, chair, table, computer, watch, etc.  Object-Oriented Programming  is a methodology or paradigm to design a program using classes and objects. Object Any entity that has state and behavior is known as an object.  An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Class Collection of objects is called class. It is a logical entity. Inheritance(IS-A) When one object acquires all the properties and behaviors of a parent object, it is known as inheritance.  It provides code reusability.  It is used to achieve runtime polymorphism. Polymorphism If one task is performed in different ways, it is known as polymorphism. Uuse method overloading and method overriding to achieve polymorphism. Abstraction Hiding internal details and showing functionality is known as abstraction. In Java, use abstract class and interface to achieve abstraction. Encapsulation ...

Internal Workings Of

  ConcurrentHashMap: The ConcurrentHashMap class is introduced in JDK 1.5 - java.util.concurrent package,  - implements ConcurrentMap as well as Serializable interface. - is a thread-safe implementation of the Map interface in Java. - allows concurrent access to the map. - is divided into number of SEGMENTs. In ConcurrentHashMap, the Object is divided into a number of segments according to the concurrency level. Part of the map called Segment (internal data structure) is only getting locked while adding or updating the map. ConcurrentHashMap allows concurrent threads to read the value without locking at all. The default concurrency-level of ConcurrentHashMap is 16. In ConcurrentHashMap, at a time any number of threads can perform retrieval operation but for updated in the object, the thread must lock the particular segment in which the thread wants to operate.  This type of locking mechanism is known as Segment locking or bucket locking.  Hence at a time, 16 update o...