Answer» Strings are Objects in Java that are internally supported by a char array. Strings are immutable (that is, their content cannot be changed once initialised) because arrays are immutable. Every time you make a change to a String, a new String is produced. String syntax in java:-
<String_Type> <string_variable> = "<sequence_of_string>";In Java, there are two ways to make a string: Using String literal:
String str = "Interview Bit";Using new keyword:
String str = new String ("Interview Bit");Difference between String literal and String object: The following table lists the differences between String literal and String object:
| String literal | String Object |
|---|
| In Java, a string literal is a collection of characters formed by enclosing them in a pair of double-quotes. | String Object is a Java object that represents a set of characters produced with the new() operator. | | If the String already exists in a literal, the new reference variable will point to the currently existing literal. | A new String object will be produced whether the String currently exists or not. |
The syntax for creating a String literal is as follows:
String string_name = “CONTENT_OF_STRING”;
|
The syntax for creating a String object is as follows:
String object_name = new String(“CONTENT_OF_STRING”);
|
String Pool: String pool is a Java heap storage area where string literals are stored. String Intern Pool or String Constant Pool are other names for it. It’s the same as object allocation. It is empty by default and is maintained privately by the Java String class. When we create a string, the string object takes up some memory in the heap. Creating a large number of strings may raise the cost and memory requirements, lowering performance. During the initialization of string literals, the JVM takes several efforts to improve efficiency and reduce memory usage. The String class keeps a pool of strings to reduce the number of String objects created in the JVM. When we construct a string literal, the JVM looks it up in the String pool first. It returns a reference to the pooled instance if the literal is already existing in the pool. If the literal isn’t found in the pool, the String pool is filled with a new String object.
In the above image, we can see that the String pool is a portion of the heap memory maintained by Java to store String literals. We can see that the literals ‘str1’ and ‘str2’ point to the same memory area. However, we can see that a new String object is always created whether or not the String exists already. However, we can make the String object to check if the String already exists by using the intern() method. Built-in String Methods:
| Method | Return type | Use Case |
|---|
| charAt() | char | The character at the provided index is returned by charAt(). | | codePointBefore() | int | Returns the Unicode of the character before the specified index. | | codePointAt() | int | codePointAt() returns the Unicode of the character at the specified index. | | compareTo() | int | It compares two strings lexicographically | | compareToIgnoreCase() | int | It compares two strings lexicographically, ignoring case differences. | | concat() | String | A string is appended to the end of another string by this function. | | contains() | boolean | Determines whether a string contains a given character sequence. | | contentEquals() | boolean | Checks whether a string includes the same character sequence as the provided CharSequence or StringBuffer. | | copyValueOf() | String | It returns a String containing all of the characters in the character array. | | endsWith() | boolean | endsWith() determines whether a string contains the provided character at the end. | | equals() | boolean | This function compares two strings. If the strings are equal, returns true; otherwise, returns false. | | equalsIgnoreCase() | boolean | equalsIgnoreCase() compares two strings without taking case into account. | | hashCode() | int | hashCode() returns the hash code of a string. | | indexOf() | int | In a string, this function returns the position of the first discovered occurrence of provided characters. | | intern() | String | intern() returns the string object’s canonical representation | | isEmpty() | boolean | Determines whether or not a string is empty. | | lastIndexOf() | | In a string, this function returns the position of the last discovered occurrence of a provided character. | | length() | int | This function returns the length of the string. | | replace() | String | replace() looks for a specified value in a string and returns a new string with the provided values replaced. | | replaceAll() | String | Each substring of this string that satisfies the supplied regular expression is replaced with the specified replacement by replaceAll(). | | split() | String[] | split() creates an array of substrings from a string | | startsWith() | boolean | startsWith() determines whether a string begins with the characters supplied. | | substring() | String | substring() generates a new string that is a substring of the given string. | | toLowerCase() | String | Converts a string to lowercase letters. | | toString() | String | Returns the value of a String object. | | toUpperCase() | String | Converts a string to upper case letters. | | trim() | String | Removes whitespace from the beginning and end of a string. | | valueOf() | String | The string representation of the provided value is returned. |
StringBuffer: A StringBuffer is a peer class that provides a lot of the same functionality as a String. StringBuffer represents growable and writable character sequences, whereas string represents fixed-length, immutable character sequences.
StringBuffer str = new StringBuffer("Interview Bit"); StringBuilder: A mutable series of characters is represented by the StringBuilder in Java. Because Java’s String Class creates an immutable sequence of characters, the StringBuilder class provides an alternative by creating a mutable sequence of characters.
StringBuilder str = new StringBuilder(); str.append("Interview Bit");String Buffer vs String Builder: The following table lists the differences between String Buffer and String Builder in Java:
| String Buffer | String Builder |
|---|
| StringBuffer is thread-safe since it is synchronised. It means that two threads can’t execute the StringBuffer functions at the same time. If they call the StringBuffer function at the same time, only one of the two threads acquires the lock and executes the method. The second thread has to wait until the execution of the first thread has been completed. | StringBuilder is not thread-safe because it is not synchronised. It indicates that two threads can use StringBuilder’s methods at the same time. In this case, no thread has to wait for the execution of the other thread. | | String Buffer is considered to be less efficient than String Builder. | String Builder is considered to be more efficient than String Buffer. | | String Buffer was introduced in Java 1.0. | String Builder was introduced in Java 1.5. |
StringTokenizer: StringTokenizer is a Java class that is used to split a string into tokens.
In the above image, we can see that StringTokenizer divides the inputted string into tokens based on the default delimiter which is space. Internally, a StringTokenizer object keeps track of where it is in the string being tokenized. Some procedures move this current location beyond the characters that have been processed. By taking a substring of the string that was used to generate the StringTokenizer object, a token is returned.
|