Posts

String, StringBuilder, StringBuffer

 String Immutable class Thread Safe It is a final class When we create a String using double quotes, JVM first looks for the String with the same value in the string pool. If found, it returns the reference of the string object from the pool. Otherwise, it creates the String object in the String pool and returns the reference. JVM saves a lot of memory by using the same String in different threads. If the new operator is used to create a string, it gets created in the heap memory. Whenever we do String manipulation like concatenation, substring, etc. it generates a new String and discards the older String for garbage collection. These are heavy operations and generate a lot of garbage in heap. StringBuffer or StringBuilder is used for string manipulation StringBuilder Mutable class Not thread safe Faster StringBuffer Mutable class Thread safe Slower

Entity, Value Object and Data Transfer Object

 Entity: Entity objects are uniquely identified by the ID and not by the attributes. Value Object: Value object is associated with entity. It is used to describe some entity attributes. Data Transfer Object (DTO): In web applications, DTO are used to transfer data between the layers (UI and back end). Entity   public record TaxableEntity ( int id , String name , int age , char sex ,                                         String location , TaxParamVO taxVO ) { } Value Object public record TaxParamVO ( double basic , double da , double hra , double cess , double allowance , double deduction ,                                  double surcharge) { } DTO public record TaxDTO ( int id , String name , int age , char sex , String location...

Wrapper Class

  Java wrapper classes  provide a convenient and efficient way to work with primitive data types . They offer advantages such as encapsulation, type conversion, autoboxing and unboxing, utility methods, thread safety, and immutability. 

Microservices

  Microservices - also known as the   microservice architecture   - is an architectural style that structures an application as a collection of services that are: Independently deployable Loosely coupled Services are typically organized around business capabilities. Each service is often owned by a single, small team.  Developers can build these services in several programming languages, deploy, scale, and maintain them independently, and enable communication between them via well-defined APIs.  Larger applications can remain mostly unaffected by the failure of a single module. Communication between services is complex, more services equals more resources, debugging problems can be harder, deployment challengers, microservices are great for large companies, but can be slower to implement and too complicated for small companies who need to create and iterate quickly, and don’t want to get bogged down in complex orchestration. Micro service Architectural Patterns ...

Constructor

  Constructors are used to initialize the object. It has the same name as the class and returns void. When a new keyword is called, it invokes the constructor and the run time creates the object and returns the reference. If the class is inherited from another class, the superclass constructor is called before the current class constructor. We can explicitly call the superclass constructor by calling super(). Copy constructors are special constructors where a new object is created by copying values from the existing one. class HelloWorld extends First { String name = "" ; HelloWorld () { System. out .println( "HelloWorld Called" ) ; } HelloWorld (String name) { super (name) ; this . name = name ; System. out .println( "HelloWorld Called " + name) ; } HelloWorld (HelloWorld obj) { this . name = obj. name ; System. out .println( "HelloWorld obj Called " ) ; } public sta...

Internal working of Spring

Spring is popular for its auto-configuration feature. It means it automatically configures our application based on the added dependencies during the project creation. Beans are stored in IoC container. Spring will look for main class with SpringApplication.run method and annotated with @SpringBootApplication. @SpringBootApplication is the combination of three annotations. @ComponentScan: Also called user configuration which is first step or phase. It detects and registers spring managed components (@Controller, @Service, @Repository, @RestController etc) with Application Context. @ComponentScan without arguments tells the Spring to scan the current package and the sub-packages. @EnableAutoConfiguration: Auto-configuration phase. It enables the Spring to auto-configure the application context. Therefore it automatically creates and registers beans based on jars in the classpath and the beans defined by the user. @Configure: Indicates the annotated class declares one or more beans d...

Call a third party API from Spring Web Application

  To call a third-party API from a Spring web application, you can use various methods depending on your requirements and preferences. Here are a few common approaches: Using RestTemplate: RestTemplate is a synchronous HTTP client provided by Spring, which simplifies consuming RESTful services. You can use RestTemplate to make HTTP requests to the third-party API and handle the responses in your Spring application.              Example:                RestTemplate restTemplate = new RestTemplate();                ResponseEntity<String> response = restTemplate.getForEntity("https://api.example.com/data",                          String.class);                String responseBody = response.getBody();        ...