Java String
Java String
1) String Literal
Java String literal is created by using double quotes. For Example:
String s="welcome";
Each time you create a string literal, the JVM checks the "string constant pool"
first. If the string already exists in the pool, a reference to the pooled instance is
returned. If the string doesn't exist in the pool, a new string instance is created and
placed in the pool. For example:
3. String s1="Welcome";
4. String s2="Welcome";//It doesn't create a new instance
Note: String objects are stored in a special memory area known as
the "string constant pool".
Why Java uses the concept of String literal?
To make Java more memory efficient (because no new objects are
created if it exists already in the string constant pool).
2) By new keyword
1. class Teststringcomparison3{
2. public static void main(String args[]){
3. String s1="Sachin";
4. String s2="Sachin";
5. String s3=new String("Sachin");
6. System.out.println(s1==s2);//true (because both refer to same instance)
7. System.out.println(s1==s3);//
false(because s3 refers to instance created in nonpool)
8. }
9. }
3) By Using compareTo() method
The String concat() method concatenates the specified string to the end of
current string. Syntax:
public String concat(String another)
1. class TestStringConcatenation3{
2. public static void main(String args[]){
3. String s1="Sachin ";
4. String s2="Tendulkar";
5. String s3=s1.concat(s2);
6. System.out.println(s3);//Sachin Tendulkar
7. }
There are some other possible ways to concatenate Strings in Java,
. String concatenation using StringBuilder class
StringBuilder is class provides append() method to perform concatenation operation.
The append() method accepts arguments of different types like Objects, StringBuilder,
int, char, CharSequence, boolean, float, double. StringBuilder is the most popular and
fastet way to concatenate strings in Java. It is mutable class which means values stored
in StringBuilder objects can be updated or changed.
1. public class StrBuilder
2. {
3. /* Driver Code */
4. public static void main(String args[])
5. {
6. StringBuilder s1 = new StringBuilder("Hello"); //String 1
7. StringBuilder s2 = new StringBuilder(" World"); //String 2
8. StringBuilder s = s1.append(s2); //String 3 to store the result
9. System.out.println(s.toString()); //Displays result
10. }
2. String concatenation using format() method
String.format() method allows to concatenate multiple strings using format specifier like
%s followed by the string values or objects.
1. public class StrFormat
2. {
3. /* Driver Code */
4. public static void main(String args[])
5. {
6. String s1 = new String("Hello"); //String 1
7. String s2 = new String(" World"); //String 2
8. String s = String.format("%s%s",s1,s2); //String 3 to store the result
9. System.out.println(s.toString()); //Displays result
10. }
11. }
3. String concatenation using String.join() method
The String.join() method is available in Java version 8 and all the above versions.
String.join() method accepts arguments first a separator and an array of String
objects.
1. public class StrJoin
2. {
3. /* Driver Code */
4. public static void main(String args[])
5. {
6. String s1 = new String("Hello"); //String 1
7. String s2 = new String(" World"); //String 2
8. String s = String.join("",s1,s2); //String 3 to store the result
9. System.out.println(s.toString()); //Displays result
10. }
Substring in Java
You can get substring from the given String object by one of the two
methods:
1. public String substring(int startIndex):
This method returns new String object containing the substring of the given
string from specified startIndex (inclusive). The method throws an
IndexOutOfBoundException when the startIndex is larger than the length
of String or less than zero.
2. public String substring(int startIndex, int endIndex):
This method returns new String object containing the substring of the given
string from specified startIndex to endIndex. The method throws an
IndexOutOfBoundException when the startIndex is less than zero or
startIndex is greater than endIndex or endIndex is greater than length of
1. public class TestSubstring{
2. public static void main(String args[]){
3. String s="SachinTendulkar";
4. System.out.println("Original String: " + s);
5. System.out.println("Substring starting from index 6: " +s.substring(
6));//Tendulkar
6. System.out.println("Substring starting from index 0 to 6: "+s.substr
ing(0,6)); //Sachin
7. }
8. }
The above Java programs, demonstrates variants of the substring() method of String class.
The startindex is inclusive and endindex is exclusive.
Using String.split() method:
The split() method of String class can be used to extract a substring from a sentence. It accepts
arguments in the form of a regular expression.
1. import java.util.*;
2.
3. public class TestSubstring2
4. {
5. /* Driver Code */
6. public static void main(String args[])
7. {
8. String text= new String("Hello, My name is Sachin");
9. /* Splits the sentence by the delimeter passed as an argument */
10. String[] sentences = text.split("\\.");
11. System.out.println(Arrays.toString(sentences));
12. }
In the above program, we have used the split() method. It accepts an argument \\. that
checks a in the sentence and splits the string into another string. It is stored in an array of
String objects sentences.