String in Java
In Java, a String is a class that represents a sequence of characters.
It is widely used for text manipulation, storing textual data, and performing various
string-related operations.
Java's String class is a part of the core Java API and provides a rich set of methods
for working with strings.
Here are some key aspects of working with strings in Java:
Creating Strings: Strings in Java can be created in multiple ways:
String str1 = "Hello, world!"; // Using string literal
String str2 = new String("Hello"); // Using the new keyword
In Java, you can create a string object using either string literals or the new keyword. While
both methods create String objects, there are some important differences between them:
1. String Literals:
When you create a string using a string literal (enclosed in double quotes), Java checks
the string pool to see if an identical string already exists. If it does, the existing reference
is returned; otherwise, a new String object is created in the pool. This is known as
"string interning."
String str1 = "Hello"; // Created in the string pool
String str2 = "Hello"; // Reuses the same object from the string pool
Since string literals are automatically interned, they save memory and can improve
performance when comparing strings.
1. Using the new Keyword:
When you create a String using the new keyword, a new String object is always created
in the heap memory, regardless of whether the string content is the same as an existing
string.
String str1 = new String("Hello"); // Always creates a new object in the heap
String str2 = new String("Hello"); // Always creates another new object in
the heap
Using the new keyword is less memory-efficient compared to using string literals, as it
creates separate objects even if the content is the same.
Methods of the String class in Java:
1. Length and Character Access:
1. String str = "Hello, Java!";
int length = str.length();
char firstChar = str.charAt(0);
char lastChar = str.charAt(length - 1);
2. Concatenation and Joining:
String firstName = "John";
String lastName = "Doe";
String fullName = firstName.concat(" ").concat(lastName); // "John Doe"
String joined = String.join(", ", "Apple", "Banana", "Orange"); // "Apple,
Banana, Orange"
3. Substring and Trimming:
String sentence = "Java is fun and powerful!";
String substring = sentence.substring(8); // "fun and powerful!"
String part = sentence.substring(0, 4); // "Java"
String withWhitespace = " Hello, Java! ";
String trimmed = withWhitespace.trim(); // "Hello, Java!"
4. Case Conversion:
String original = "Java Programming";
String lowerCase = original.toLowerCase(); // "java programming"
String upperCase = original.toUpperCase(); // "JAVA PROGRAMMING"
5. Searching and Replacing:
String text = "Java is great, Java is powerful!";
int firstIndex = text.indexOf("Java"); // 0
int lastIndex = text.lastIndexOf("Java"); // 13
boolean containsPowerful = text.contains("powerful"); // true
String replaced = text.replace("Java", "Python"); // "Python is great,
Python is powerful!"
6. Equality and Comparison:
String str1 = "apple";
String str2 = "banana";
boolean isEqual = str1.equals(str2); // false
boolean isEqualIgnoreCase = str1.equalsIgnoreCase("Apple"); // true
int comparison = str1.compareTo(str2); // Negative value
int comparisonIgnoreCase = str1.compareToIgnoreCase("Banana"); // Negative
value