Strings in Java are objects that represent sequences of characters. They are used extensively in Java programming for representing and manipulating text. The String class is part of the java.lang package, which is imported by default, so you don't need to explicitly import it.
You can declare and initialize a string variable in Java as follows:
String name = "Anubhav";
System.out.println("Name: " + name);Here, name is a string variable holding the value "Anubhav".
You can concatenate two strings using the + operator:
String firstName = "Anubhav";
String lastName = "Raj";
String fullName = firstName + " " + lastName;
System.out.println("Full Name: " + fullName);The concat() method can also be used to concatenate strings:
String fullName2 = firstName.concat(" ").concat(lastName);
System.out.println("Full Name (using concat): " + fullName2);The charAt() method returns the character at a specified index:
System.out.println("First character of the name: " + name.charAt(0));Output: First character of the name: A
The length() method returns the length of the string:
System.out.println("Length of the name: " + name.length());Output: Length of the name: 7
The replace() method replaces each occurrence of a character with another character:
String newString = name.replace('A', 'B');
System.out.println("New String: " + newString);Output: New String: Bnubhav
The substring() method extracts a substring from the string:
String subString = name.substring(1, 5);
System.out.println("Substring: " + subString);Output: Substring: nubh
- Immutability: Strings in Java are immutable, meaning once a string is created, it cannot be changed. Any modification results in the creation of a new string.
- StringBuilder and StringBuffer: For mutable strings (strings that need to be modified frequently), consider using
StringBuilderorStringBufferfor better performance. - String Pool: Java maintains a pool of strings to optimize memory usage. When you create a string literal, Java checks the pool first. If the string exists, it returns the reference; otherwise, it adds the new string to the pool.
- Comparison: Avoid using
==to compare strings. Use the.equals()method to compare the content of two strings. The==operator compares references. - Case Sensitivity: Use methods like
.equalsIgnoreCase()for case-insensitive comparisons. - Whitespace: Use the
.trim()method to remove leading and trailing whitespace. This is useful for processing user input. - Efficient Concatenation: For multiple string concatenations in loops, use
StringBuilderorStringBufferto avoid creating many temporary string objects.
Here is a complete example demonstrating various string operations:
package NonPrimitiveDataTypes;
public class Strings {
public static void main(String[] args) {
// Declaring and initializing a string variable
String name = "Anubhav";
System.out.println("Name: " + name);
// String concatenation - Adding two strings together
String firstName = "Anubhav";
String lastName = "Raj";
// Using concatenation operator (+) to concatenate strings
String fullName = firstName + " " + lastName;
System.out.println("Full Name: " + fullName);
// Using the concat() method to concatenate strings
String fullName2 = firstName.concat(" ").concat(lastName);
System.out.println("Full Name (using concat): " + fullName2);
// charAt() method - Returns the character at the specified index
System.out.println("First character of the name: " + name.charAt(0));
// length() method - Returns the length of the string
System.out.println("Length of the name: " + name.length());
// replace() method - Replaces each occurrence of a character with another character
String newString = name.replace('A', 'B');
System.out.println("New String: " + newString);
// substring() method - Extracts a substring from the string
String subString = name.substring(1, 5);
System.out.println("Substring: " + subString);
}
}By exploring these methods and concepts, you'll gain a thorough understanding of how to work with strings in Java. Happy coding!