1. Introduction
This tutorial aims at reviewing some basic knowledge of Java. If you are preparing for an interview or want to enhance your understanding of Java language, you can subscribe to this series. My plan is to create serval tutorials covering topics from JVM, Hashmap, basic data structure, spring and more!
2. JVM
The Java Virtual Machine (AKA. JVM) is the core component of Java programing language. To better understand, we can treat JVM as a program that helps us execute other programs. To handle daily work better, we need to understand many crucial functions and principles in JVM. This tutorial will focus on better memory allocation and higher GC efficiency in JVM management.
3. Stack Memory in Java
Static memory allocation and the execution of a thread
1. primitive values that are specific to a method
2. references of objects in hemp
3. When new block is created, a space is created in the stack, methods finish, space out
4. The stack is fast and threadsafe since each thread operates in its stack.
Hemp space in Java
Hemp space is used for the dynamic memory allocation of Java objects and JRE classes at runtime. New Object is stored in heap space while its reference is stored in the stack.
The hemp memory space can be further divided into three smaller parts:
- Young Generation: this is where all new objects are stored. A new GC process will be executed when this part is filled up.
- Old or tenured Generation: when the new Generation's time is up, it will be sent to this Generation.
- Permanent Generation: JVM metadata, runtime classes and application methods
- This memory, in contrast to stack, isn't automatically deallocated. It needs Garbage Collector to free up unused objects to keep the efficiency of the memory usage. Changing the memory allocation of different generations is the crucial logic of JVM performance maintenance.
Red-black tree
The red-black tree is a kind of binary search tree. However, the most crucial feature is that the red-black tree is self-balanced by default. Compared to a typical binary search tree, the red-black tree solved unbalanced structure problems that happen in some extreme instances. There are a few essential rules that each red-black tree must follow:
- It must follow the standard binary search tree rule.
- The root node must be black.
- Each node is either red or black.
- the end leaf, which is always black
- the child node of red nodes must be black
Finally is the crucial rule: every path from the root node to the nil node must contain the same number of black nodes.
When the above rules are followed, a standard red-black tree will be formed automatically! The hashmap data structure applies the red-black tree, which is why it can use the null key and value.
The difference between Hashmap and Hashtable
Hashmap and Hashtable are two commonly used map collections in Java. However, many people are unfamiliar with these two classes' differences. Here, I listed some basic information about the difference between Hashmap and Hashtable:
- Since Java 1.8, Hashmap uses the red-black tree as its memory method, while Hashtable still uses array and LinkedList.
- HashMap's key and value could be null, but Hashtable couldn't.
- The hash algorithm is different between Hashmap and Hashtable. Hashtable uses the class's own hashcode() method.
- Hashmap is not synchronized while Hashtable is. In other words, the efficiency of Hashmap is higher than Hashtable, although Hashtable is concurrency secured.
These are some essential points in differentiating Hashmap and Hashtable. In the following tutorial, we will look at some basic information about the life cycle of servlet and spring, as well as HTTP and tcp protocol.