Understanding the String Class in Java | Set 1

Understanding the String Class in Java | Set 1

The String class in Java is one of the most widely used classes. It is part of the java.lang package and represents a sequence of characters. Strings in Java are immutable, meaning once a String object is created, it cannot be changed.

This article provides an overview of the String class, focusing on its properties, how it works, and common operations.


1. Key Characteristics of Strings in Java

  • Immutable: Once created, a String cannot be modified. Any operation that seems to modify a string creates a new String object.
  • Interned: Java maintains a pool of String objects to save memory. If two string literals have the same content, they will point to the same object in the pool.
  • Char Array Representation: Internally, String objects use a character array to store the sequence of characters.

2. Creating Strings in Java

There are two primary ways to create strings in Java:

Using String Literals

When a string is created using double quotes (""), it is stored in the string pool.

String str1 = "Hello";
String str2 = "Hello"; // Points to the same object as str1        

Using the new Keyword

This creates a new object in the heap, even if the content is the same as an existing string.

String str3 = new String("Hello"); // Creates a new object        

3. Commonly Used String Methods

The String class provides a rich set of methods for manipulating and querying strings. Here are some of the most commonly used ones:

Commonly Used String Methods

4. Examples of Common Operations

Getting String Length

String str = "Hello, World!";
System.out.println("Length: " + str.length()); // Output: 13        

Extracting a Substring

String str = "Programming";
System.out.println("Substring: " + str.substring(0, 6)); // Output: Program        

Checking Equality

String str1 = "Java";
String str2 = "java";
System.out.println(str1.equals(str2));         // Output: false
System.out.println(str1.equalsIgnoreCase(str2)); // Output: true        

Converting Case

String str = "Hello";
System.out.println(str.toUpperCase()); // Output: HELLO
System.out.println(str.toLowerCase()); // Output: hello        

Trimming Whitespace

String str = "   Java   ";
System.out.println("Trimmed: '" + str.trim() + "'"); // Output: 'Java'        

Replacing Characters

String str = "hello";
System.out.println(str.replace('l', 'w')); // Output: hewwo        

5. Immutability of Strings

Since strings are immutable, every operation that modifies a string creates a new object.

Example:

String str = "Hello";
str.concat(" World");
System.out.println(str); // Output: Hello        

The concat method creates a new string "Hello World", but str still points to "Hello".

6. String Pool in Java

Java maintains a string pool to optimize memory usage. Strings created using literals are stored in the pool, and identical strings share the same memory.

Example:

String str1 = "Java";
String str2 = "Java";

System.out.println(str1 == str2); // Output: true        

If you create strings using the new keyword, they are not added to the pool:

String str3 = new String("Java");
System.out.println(str1 == str3); // Output: false        

7. Performance Considerations

For performance-sensitive applications:

  • Use StringBuilder or StringBuffer for mutable strings and frequent modifications.
  • Use String for fixed content or fewer modifications.


Conclusion

The String class in Java is a powerful and frequently used class. Understanding its immutability, string pool mechanism, and the methods it offers is essential for Java developers. In this first set of topics about the String class, we have focused on the basics. Stay tuned for more advanced topics in Set 2.


要查看或添加评论,请登录