Memory Management in Java – JVM Memory Model

Java (JVM) Memory Model At broad level, JVM Heap memory is physically divided into two parts – Young Generation and Old Generation. Young Generation Young generation is the place where all the new objects are created. When young generation is filled, garbage collection is performed. This garbage collection is called Minor GC. Young Generation is divided into three parts – Eden Memory and two Survivor Memory spaces. Important Points about Young Generation Spaces: Most of Read more…

Java is Pass by Value

One of the biggest confusion in Java programming language is whether java is Pass by Value or Pass by Reference. First of all, we should understand what is meant by pass by value or pass by reference. Pass by Value: The method parameter values are copied to another variable and then the copied object is passed, that’s why it’s called pass by value. Pass by Reference: An alias or reference to the actual parameter is Read more…

Reverse a String is Java

There are many ways to reverse a String in Java. We will see traditional ways and using classes that provide an in-build function. 1. Storing in reverse Order In the above piece of code. We have assigned a char of length of String passed to it. In the loop we are taking a character from start index and adding it to the last index of above-assigned array. Finally, we combine all Character as String and return Read more…

Difference between StringBuffer and StringBuilder

Based on my previous posts related to StringBuffer and StringBuilder let’s see the difference. StringBuffer StringBuilder StringBuffer was the only choice for String manipulation till Java 1.4 StringBuilder came into picture from Java 1.5. all of its public methods are synchronized all of its public methods are not synchronized StringBuffer provides Thread safety. StringBuilder doesnt provides Thread safety StringBuilder is slower because of synchronization. StringBuilder is faster and preferred.

StringBuffer in Java

We will find an answer for the following questions What is StringBuffer Available Constructors Commonly used methods StringBuffer Java StringBuffer class is used to create a mutable (modifiable) string. The StringBuffer class in java is the same as the String class except it is mutable i.e. it can be changed. The Java StringBuffer class is the same as the StringBuilder class except that this one is synchronized. So its thread-safe and can be used if Object Read more…

StringBuilder in Java

We will find an answer for the following questions What is StringBuilder Available Constructors Commonly used methods StringBuilder Java StringBuilder class is used to create a mutable (modifiable) string. The Java StringBuilder class is the same as the StringBuffer class except that it is non-synchronized. So its not thread-safe and can’t be used if Objects are shared among multiple threads. It is available since JDK 1.5. If sb refers to an instance of a StringBuilder, then sb.append(x) Read more…

HttpSession

When a client visits the webapp for the first time, and HttpSession is obtained for the first time the servlet container creates a new HttpSession object, generates a long and unique ID (which you can get by session.getId()), and store it in the server’s memory. The servlet container also sets a Cookie in the Set-Cookie header of the HTTP response with JSESSIONID as its name and the unique session ID as its value. As per Read more…

HttpServletRequest and HttpServletResponse

What happens when Rest API is fired? The servlet container is attached to a web server that listens for HTTP requests on a certain port number (port 8080 for development and port 80 in production). When a client (user with a web browser) sends an HTTP request, the servlet container creates new HttpServletRequest and HttpServletResponse objects and passes them through any defined Filter chain and, eventually, the Servlet instance. In the case of filters, the Read more…

ServletContext and load-on-startup

What happens behind the scene when Web Application is deployed in Web Server like Tomcat? ServletContext It provides a set of methods that a servlet uses to communicate with its servlet container. There is one context per “web application” per Java Virtual Machine. A “web application” is a collection of servlets and content installed under a specific subset of the server’s URL namespace such as /catalog and possibly installed via a .war file. Workflow When a Read more…

Difference between Hashtable and HashMap

Based on my previous posts related to Hashtable and HashMap let’s see the difference. HashMap Hashtable Non-Synchronized. Fast. Synchronized. Slow Allows one null key and any number of null values. Hashtable doesn’t allow null keys and null values. Use LinkedHashMap to maintains the insertion order. TreeMap to sort mappings based on the ascending order of keys. Hashtable doesn’t guarantee any kind of order. Iterator in HashMap is fail-fast. Enumerator in Hashtable is not fail-fast.