Strings are an integral part of any programming language, and Java is no exception. In Java, a string is an object that is backed internally by a character array. Since arrays are immutable (cannot grow), strings are immutable as well. Whenever a change to a string is made, a new string object is created.
To create a string in Java, there are four ways: character array, String class, string literal, and new keyword. A character array is created using the following syntax: char arr[] = ['g', 'e', 'e', 'k', 's'];
Strings created using the String class are immutable, which makes them thread-safe by default. A string literal is created using the syntax: String str = "geeks";
Strings created using the new keyword are not immutable and can be used in multi-threaded environments. A string created using the new keyword is created using the syntax: String str = new String("geeks");
In addition to the String class, Java also has the StringBuffer and StringBuilder classes. StringBuffer is a peer class of String that provides much of the functionality of strings. String represents fixed-length, immutable character sequences while StringBuffer represents growable and writable character sequences. It is a thread-safe class and can be used in multi-threaded environments. The syntax for creating a StringBuffer is StringBuffer s = new StringBuffer("geeks");
StringBuilder in Java represents a mutable sequence of characters. Since the String Class in Java creates an immutable sequence of characters, the StringBuilder class provides an alternative to the String Class, as it creates a mutable sequence of characters. It is used in a single-threaded class and is not thread safe. The syntax for creating a StringBuilder is StringBuilder str = new StringBuilder("geeks");
Some important methods of the String class include length()
, charAt(int i)
, substring(int i)
, and substring(int i, int j)
. length()
returns the number of characters in the string. charAt(int i)
returns the character at the ith index. substring(int i)
returns the substring from the ith index character to the end of the string. substring(int i, int j)
returns the substring from i to j-1 index. For example:
rustString str = "geeks";
System.out.println(str.length());
System.out.println(str.charAt(3));
System.out.println(str.substring(2));
System.out.println(str.substring(2, 5));
Output:
5 k eks eks
Java has a feature called String literal pooling. When string objects are created using string literals, and two different strings have the same content, then both references refer to the same object. This is an auto-optimization feature of Java. When string objects are created using the new operator, different memories are allocated to different variables even if they have the same content. For example:
javascriptString s1 = "geeks";
String s2 = "geeks";
if(s1 == s2)
System.out.println("Yes");
else
System.out.println("No");
String s3 = new String("geeks");
if(s1 == s3)
System.out.println("Yes");
else
System.out.println("No");
Output:
yamlYes
No
Other useful methods of the String class include contains(CharSequence sequence)
, which returns true if the sequence of char values is found in the string; and equals(Object otherObj)
, which compares this string to the specified object. For example:
javascriptString s1 = "geeksforgeeks";
String s2 = "geeks";
System.out.println(s1.contains(s2));
String s3 = "geeky";
System.out.println(s1.contains(s3));
System.out.println(s1.equals("GeeksForGeeks"));
// Output: // true // false // false
In this example, we create three strings: s1, s2, and s3. We then use the contains() method to check if s1 contains s2 and s3. Since s2 is a substring of s1, the contains() method returns true when comparing s1 and s2. However, s3 is not a substring of s1, so contains() returns false when comparing s1 and s3.
Next, we use the equals() method to compare s1 to a string literal "GeeksForGeeks". The equals() method returns false because the comparison is case-sensitive, and the capitalization in the two strings is different.
Overall, the String class provides a wide range of methods for manipulating and comparing strings, making it a powerful tool for text processing in Java.