String In Java
What are Strings in Java?
• In Java, string is basically an object that represents sequence of
char values. An array of characters works same as Java string
Example:
String name = "Geeks";
String
• Java String class provides a lot of methods to perform operations on
strings such as compare(), concat(), equals(), split(), length(),
replace(), compareTo(), intern(), substring() etc.
• The [Link] class
implements Serializable, Comparable and CharSequence interfaces.
• In Java, string is an object that represents a sequence of characters.
The [Link] class is used to create a string object.
CharSequence Interface
CharSequence Interface is used for representing the sequence of Characters.
String, StringBuffer and StringBuilder classes implement it. It means, we can create
strings in Java by using these three classes.
String
• The Java String is immutable which means it cannot be
changed. Whenever we change any string, a new instance
is created. For mutable strings, you can use StringBuffer
and StringBuilder classes.
Syntax:
String str= "geeks";
or
String str= new String("geeks")
Ways of Creating a String
There are two ways to create a string in Java:
❑String Literal
❑Using new Keyword
String literal
To make Java more memory efficient (because no new objects are
created if it exists already in the string constant pool).
Example:
String st = “Welcome to Java Programming”
String objects are stored in a special memory area known as
the "string constant pool".
Using new keyword
String s = new String(“Welcome”);
In such a case, JVM will create a new string object in normal
(non-pool) heap memory and the literal “Welcome” will be placed in
the string constant pool. The variable st will refer to the object in the
heap (non-pool)
Example:
String st = new String (“Welcome to Java Programming”);
StringBuffer
StringBuffer is a peer class of String, it is mutable in nature and it is
thread safe class , we can use it when we have multi threaded
environment and shared object of string buffer i.e, used by
mutiple thread. As it is thread safe so there is extra overhead, so it
is mainly used for multithreaded program.
Example:
StringBuffer demoString = new StringBuffer("GeeksforGeeks");
StringBuilder
StringBuilder in Java represents an alternative to String and StringBuffer Class, as it
creates a mutable sequence of characters and it is not thread safe. It is used only
within the thread , so there is no extra overhead , so it is mainly used for single
threaded program.
Syntax:
StringBuilder demoString = new StringBuilder();
[Link]("GFG");
Java String Operations
• Get the Length of a String
• class Main {
• public static void main(String[] args) {
• // create a string
• String greet = "Hello! World";
• [Link]("String: " + greet);
• // get the length of greet
• int length = [Link]();
• [Link]("Length: " + length);
• } Output
• }
String: Hello! World
Length: 12
Join Two Java Strings
We can join two strings in Java using the concat() method.
For example,
Output:
First String: Java
Second String: Programming
Joined String: Java Programming
Compare Two Strings
In Java, we can make comparisons
between two strings using the
equals() method. For example,
Methods of Java String
Methods Description
contains() Checks whether the string contains a substring.
substring() Returns the substring of the string.
join() Joins the given strings using the delimiter.
Replaces the specified old character with the
replace()
specified new character.
replaceAll() Replaces all substrings matching the regex pattern.
replaceFirst() Replaces the first matching substring.
Returns the character present in the specified
charAt()
location.
Methods of Java String
Returns the position of the specified character in
indexOf()
the string.
compareTo() Compares two strings in the dictionary order.
compareToIgnoreCase() Compares two strings, ignoring case differences.
trim() Removes any leading and trailing whitespaces.
split() Breaks the string into an array of strings.
toLowerCase() Converts the string to lowercase.
toUpperCase() Converts the string to uppercase.
Methods of Java String
Checks whether the string matches the
matches()
given regex.
Checks if the string begins with the given
startsWith()
string.
Checks if the string ends with the given
endsWith()
string.
isEmpty() Checks whether a string is empty or not.
[Link]