Class String in Java
What are the features of the String class
- It is an immutable and finalized data type;
- The JVM stores all String objects in a string pool;
- An object of the String class can be obtained using double quotes;
- You can use the + operator to concatenate strings;
- Since Java 7, strings can be used in a switch statement.
Why is String an immutable and finalized class
There are several advantages to strings being immutable:
- A string pool is only possible because the string is immutable, so the VM saves more free space in the Heap, since different string variables point to the same variable in the pool. If the string were mutable, then interning the strings would not be possible, because changing the value of one variable would also affect the rest of the variables referring to that string.
- If the string is mutable, then it becomes a serious security risk for the application. For example, the database username and password are passed as a string to get a database connection, and in socket programming, the host and port details are passed as a string. Since the string is immutable, its value cannot be changed, otherwise an attacker could change the value of the link and cause security problems for the application.
- Immutability avoids synchronization: strings are thread safe and a single string instance can be shared by different threads.
- The strings are used by the classloader and immutability ensures that the class is loaded correctly.
- Since the string is immutable, its hashCode() is cached at the time of creation and there is no need to calculate it again. This makes a string a great candidate for a key in a HashMap since its processing is faster.
Why is char[] preferred over String for storing password
From the moment of creation, the string remains in the pool until it is removed by the garbage collector. Therefore, even after the end of using the password, it remains available in memory for some time and there is no way to avoid this. This poses a certain security risk as anyone with access to the memory will be able to find the password in plain text. In the case of using a character array to store the password, it is possible to clear it as soon as you finish working with the password, avoiding the security risk inherent in the string.
Why is string a popular key in HashMap in Java
Since strings are immutable, their hash code is calculated and cached at the time of creation, without requiring recalculation when used later. Therefore, as the key of the HashMap, they will be processed faster.
What does the intern() method in the String class do
The intern() method is used to store a string in a string pool or to get a reference if such a string is already in the pool.
Can strings be used in switch constructs
Starting with Java 7, you can use strings in a switch statement, earlier versions of Java do not support this. Wherein:
- the strings involved are case sensitive;
- the equals() method is used to compare the obtained value with the case values, therefore, to avoid NullPointerException, you should provide a check for null.
- According to the Java 7 documentation for strings in a switch, the Java compiler generates more efficient bytecode for strings in a switch construct than for chained if-else conditions.
What's the main difference between String, StringBuffer, StringBuilder
The String class is immutable - you cannot modify an object of this class, you can only replace it by creating a new instance.
StringBuffer class is mutable - Use StringBuffer when you need to modify content frequently.
The StringBuilder class was added in Java 5 and is identical in every way to the StringBuffer class, except that it is not synchronized and therefore its methods are much faster.
Read also:
- Type casting in Java
- Autoboxing in Java and rules for wrapping primitive types in wrapper classes
- Transaction isolation levels in database
Comments
Post a Comment