Strings in Java are Objects that are backed internally by a char array. Since arrays are immutable(cannot grow), Strings are immutable as well. Whenever a change to a String is made, an entirely new String is created.
Below is the basic syntax for declaring a string in Java programming language.
<String_Type> <string_variable> = “<sequence_of_string>”;
String str = "Geeks";
Memory allotment of String
Whenever a String Object is created, two objects will be created- one in the Heap Area and one in the String constant pool and the String object reference always points to heap area object.
String str = "Geeks";
An Example that shows how to declare String
String s = GeeksforGeeks String s1 = GeeksforGeeks
Interfaces and Classes in Strings in Java
- CharBuffer: This class implements the CharSequence interface. This class is used to allow character buffers to be used in place of CharSequences. An example of such usage is the regular-expression package java.util.regex.
- String: String is a sequence of characters. In java, objects of String are immutable which means a constant and cannot be changed once created.
Creating a String
There are two ways to create string in Java:
- String literal
String s = “GeeksforGeeks”;
- Using new keyword
String s = new String (“GeeksforGeeks”);
- String literal
- StringBuffer: 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.
StringBuffer s = new StringBuffer("GeeksforGeeks");
- StringBuilder: The StringBuilder in Java represents a mutable sequence of characters. Since the String Class in Java creates and immutable sequence of characters, the StringBuilder class provides an alternate to String Class, as it creates a mutable sequence of characters.
StringBuilder str = new StringBuilder(); str.append("GFG");
- StringTokenizer: StringTokenizer class in Java is used to break a string into tokens.
A StringTokenizer object internally maintains a current position within the string to be tokenized. Some operations advance this current position past the characters processed.
A token is returned by taking a substring of the string that was used to create the StringTokenizer object.
- StringJoiner: StringJoiner is a class in java.util package which is used to construct a sequence of characters(strings) separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix. Though this can also be with the help of StringBuilder class to append delimiter after each string, StringJoiner provides an easy way to do that without much code to write.
public StringJoiner(CharSequence delimiter)
- Compare two Strings in Java
- Compare two strings lexicographically in Java
- Numbers in Java (With 0 Prefix and with Strings)
- How to Initialize and Compare Strings in Java?
- Printing Integer between Strings in Java
- Check whether two Strings are Anagram of each other using HashMap in Java
- Output of Java Programs | Set 52 (Strings Class)
- Swap two Strings without using third user defined variable in Java
- CompileTime Vs RunTime Resolution of Strings
- Find all palindromic sub-strings of a given string | Set 2
- Longest Common Substring in an Array of Strings
- Print all possible strings of length k that can be formed from a set of n characters
- Number of character corrections in the given strings to make them equal
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.function.BiPredicate interface in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : anthonycathers