Strings are an integral part of programming. In Java, we have a String class for creating and manipulating strings. Also, there is an interface called 'Char Sequence' used for representing a character sequence. The String class is one of the classes which implement this interface. Hence, String is basically an object that represents a sequence of char values.
How to create a string object?
There are two ways to create String object:
1. By string literal
2. By new keyword
1) String Literal
Java String literal is created by using double quotes. For Example:
String s1-:"Java"//It doesn't create new instance.
How does this work internally?
String Constant Pool
It is a pool or storage area in Java heap memory that is dedicated to storing string literals in Java. Whenever we create a string, JVM (Java Virtual Machine) looks into the "String constant pool" to see if the same value is already present or not.
If it is present, then the address (or reference) existing string object is stored in the reference variable and if it is not present there, a new string object is created and stored inside the string pool. Then the reference of this newly created string object is returned.
2.By New Keyword
We can also create a string by using the new keyword in Java. As the new keyword works for creating an instance of any other class, in the same way, it works for creating an instance of the String class.
The main difference between creating a string using string literals and the new keyword is that whenever the string is created with the new keyword a new object of String is created in the heap memory outside the string constant pool.
All string objects created using the new keyword are allocated space in the heap memory (outside the string constant pool) irrespective of whether the same valued strings are already present in the heap memory or not.
Immutability of Strings in Java
In Java, string objects are immutable, which simply means something that cannot be changed or modified. Once a string object is created, its data or state can't be modified.
In the given example
· A string buffer is like a String but can be modified.
· It contains a sequence of characters, but the size and content of the sequence can be changed via the various methods provided in the class.
· String buffers are thread safe. The principal operations on a String Buffer are the ‘append ’and ‘insert' methods.
· Characters and substrings can be placed in the middle or appended to the end of a String Buffer. To accommodate such changes, String Buffer automatically expands in size.
· String buffer has a predefined capacity. As long as the size of the character sequence does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it automatically increases its size.
· StringBuilder objects are similar to String objects, however, they can be changed.
· These objects are internally handled as variable-length arrays containing a sequence of characters. Method invocations can change the length and content of the sequence at any time.
· String Builder class provides an API compatible with StringBuffer and is used as a replacement for StringBuffer.
· String builder class is used in preference to StringBuffer as it is faster in terms of implementation. The principal operations on a StringBuilder are the ‘append’ and ‘insert’ methods,
· Unlike strings, every string builder has a capacity, which is the number of character spaces allocated. It automatically extends to accommodate new string builder additions.
Methods of Java Strings
String methods are very useful as they make it very easy to work with Strings in Java. We can find the length of a string, concatenate multiple strings, check if two strings are equal or not, convert the characters of a string to uppercase or lowercase, and much more using string methods in Java. Let us discuss some of the very popular and widely used string methods in Java. All of these string methods are provided by the java.lang.String class in Java.
Java String trim() method
The String class trim() method eliminates white spaces before and after the String.
Java String toUpperCase() and toLowerCase() method
The Java String toUpperCase() method converts this String into uppercase letter and String toLowerCase() method into lowercase letter.
Java String startsWith() and endsWith() method
The method startsWith() checks whether the String starts with the letters passed as arguments and endsWith() method checks whether the String ends with the letters passed as arguments.
Java String charAt() Method
The String class charAt() method returns a character at specified index.
Java String length() Method
The String class length() method returns length of the specified String.
Java String replace() Method
The String class replace() method replaces all occurrence of first sequence of character with second sequence of character.
String concat(String string1) Method
The concat() method of string in Java is used for concatenating two strings.
In this blog, we learned about Java Strings, how to create them, and various string methods with the help of examples. In Java, Strings are the Objects which are internally a sequence of characters. In simple words, Strings are the collection/combination of characters. Newly created strings are stored in a special area in the heap called String pool or String Constant Pool· Strings can be created using the string literals as well as using the new keyword as strings are objects in Java· Strings created using the new keyword are allocated memory in the heap memory outside the string constant pool. There are different methods of Strings in Java that can be used to make it easy to work with string in Java.