STRINGS
Strings are generally immutable in nature., Whereas StringBuffer & String Builder are mutable
Reason - Security is the reason for Strings they are made immutable. because with strings you can able to access databases and files. sometimes strings store passwords & usernames that cannot be modified
StringBuffer - mutable, thread-safe
StringBuilder - mutual, not thread-safe
'String' - Not a keyword, It is not a primitive data type, it is a derived type
Security is the reason for Strings they are made immutable. because with strings you can able to access databases and files. sometimes strings store passwords & usernames which cannot be modified.
String Constant Pool: It is a Special memory Region where JVM stores String Instances
What is the String Constant Pool?
- The String Constant Pool (also known as the String Intern Pool) is a special area within the Java heap where string literals are stored.
- When you create a string literal (e.g.,
"Hello"), it is automatically placed in the String pool. - The String pool is maintained by the Java runtime and is privately managed by the
Stringclass.
How Does It Work?
- When you create a string literal, the JVM first checks if that literal already exists in the String pool.
- If the literal is already present, the JVM reuses the existing instance (reference) from the pool.
- If the literal is not in the pool, a new string object is created and added to the pool.
- This pooling mechanism helps reduce memory usage and improves performance.
String Literal vs. Using
newKeyword:- String Literal:
- Example:
String str1 = "Java"; - The string literal
"Java"is placed in the String pool. - If you create another string literal with the same content (e.g.,
String str2 = "Java";), it reuses the existing reference.
- Example:
- Using
newKeyword:- Example:
String str3 = new String("Java"); - The
newkeyword creates a new string object in the Java heap (not in the String pool). - It does not check the pool for existing instances.
- Use this approach when you explicitly want a new instance.
- Example:
- String Literal:
It is located in the Java heap memory
Example Code:
public class StringPoolExample {
public static void main(String[] args) {
String s1 = "Java"; // Placed in String pool
String s2 = "Java"; // Reuses existing reference
String s3 = new String("Java"); // New instance in heap
String s4 = new String("Java").intern(); // Explicitly add to pool
System.out.println((s1 == s2) + ", Strings are equal."); // true
}
}
- In this example,
s1ands2share the same reference because they both point to the pooled instance of"Java". - The
intern()method explicitly adds the string to the pool.
When you use the .intern() method with the new keyword in Java, it explicitly adds the string to the String Constant Pool (also known as the String Intern Pool). Let me explain this further:
For comparing the content of two string objects, it's recommended to use the equals() method rather than the == operator, as equals() compares the actual contents of the strings
Difference Between == & .equals():
.equals()Method- used for content comparison
It comapres the values stored in the objects, not their memory locations.
public class ReferenceComparisonExample {
public static void main(String[] args) {
String s1 = "HELLO";
String s2 = "HELLO";
String s3 = new String("HELLO");
System.out.println(s1.equals(s2)); // true (same memory location)
System.out.println(s1.equals(s3)); // false (different memory locations)
}
}
- == Operator - used for reference comparison
It checks whether two object references point to the same memory locations in the heap.
To convert String into Char away - 'toCharArray' is used
public class ReferenceComparisonExample {
public static void main(String[] args) {
String s1 = "HELLO";
String s2 = "HELLO";
String s3 = new String("HELLO");
System.out.println(s1.equals(s2)); // true (same memory location)
System.out.println(s1.equals(s3)); // true (different memory locations).
}
}
Comments
Post a Comment