Java String
This document analyzes Java Strings.
1. Java String
| |
Java provides the String Class for string processing. [Code 1] shows part of Java’s String Class. Looking at the Member variables of the String Class, you can see that they all have final attached. This means that strings stored once in a String Instance cannot be changed. Therefore, when concatenating strings using the + operator of the String Class, a new String Instance is created. Because of this characteristic, if you perform many string concatenations using the + operator in Java Code, String Instances can occupy the Heap Memory area and cause Heap Memory space shortage.
1.1. StringBuilder, StringBuffer
| |
| |
Java recommends using the StringBuilder Class or StringBuffer Class rather than the String Class when string changes occur frequently. [Code 2] shows the StringBuilder class, and [Code 3] shows the AbstractStringBuilder Class, which is the parent Class of the StringBuilder Class. Looking at the Member Variables of the AbstractStringBuilder Class, you can see that there is a Character Array without final attached.
Looking at the Methods of the AbstractStringBuilder Class, you can see that it uses the Character Array as a Memory Pool and directly changes the contents of the Character Array when manipulating strings. Therefore, when manipulating strings using a StringBuilder Instance, unnecessary Heap Memory usage can be prevented.
The StringBuffer Class performs the same role as the StringBuilder Class but has synchronized attached to Methods, so it can be used Thread-safely even in multi-thread environments. On the other hand, it has lower performance than the StringBuilder Class in single-thread environments. Therefore, use the StringBuilder Class in single-thread environments and the StringBuffer Class in multi-thread environments.
1.2. String Literal
| |
There are two ways to initialize a String Instance: using a Constructor and using a String Literal. Lines 4 and 5 of [Code 4] show the method using a Constructor, and Lines 7 and 8 of [Code 4] show the method using a String Literal. Since all String Instances have the string “ssup2”, when comparing using the equal() Method, the result shows that the strings are the same, but you can see different results when comparing with the “==” operator.
When initializing a String Instance with a Constructor, the String Instance is newly allocated in the Heap area. Therefore, the address of strConstuctor1 and the address of strConstuctor2 are different. On the other hand, when initializing using a String Literal, if the strings are the same, the same String Literal is shared. Therefore, the address of strLiteral1 and the address of strLiteral2 are the same.
String Literals are located in the Constant String Pool. The Constant String Pool is located in the “Permanent Generation” area of the Heap in Java 6 Version and below, and is located in the “Young/Old Generation” of the Heap from Java 7 Version onwards, making it subject to Garbage Collection.