Java String
In Java, string is basically an object that represents sequence of char
values.
An array of characters works same as Java string. For example: 1. char[]
ch={'j','a','v','a','t','p','o','i','n','t'}; 2. String s=new String(ch);
is same as:
1. String s="javatpoint";
What is String in java
Generally, String is a sequence of characters.
But in Java, string is an object that represents a sequence of
characters.
The [Link] class is used to create a string object.
How to create a string object?
There are two ways to create String object:
1. By string literal
1) String Literal
Java String literal is created by using double quotes. For Example:
1. 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: 1. String s1="Welcome";
2. String s2="Welcome";//It doesn't create a new instance
2) By new keyword
1. String s=new String("Welcome");//creates two objects and one
reference variable
In such 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 s will refer to the object in a heap (non-pool).
public class StringExample
public static void main(String args[])
String s1="java";//creating string by java string literal
char ch[]={'s','t','r','i','n','g','s'};
String s2=new String(ch);//converting char array to string
String s3=new String("example");//creating java string by new keyword
[Link](s1);
[Link](s2);
[Link](s3);
class Testimmutablestring
{
public static void main(String args[])
String s="Sachin";
[Link](" Tendulkar");//concat() method appends the string at the end
[Link](s);//will print Sachin because strings are immutable objects
Output: Sachin
String compare by equals() method
The String equals() method compares the original content of the string. It
compares values of string for equality. String class provides two methods:
● public boolean equals(Object another) compares this string to the specified
object.
● public boolean equalsIgnoreCase(String another) compares this String to
another string, ignoring case.
Hello 1
HELLO 2
hello 3
} class Teststringcomparison1
{
public static void main(String args[])
{ String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
String s4="Saurav";
[Link]([Link](s2));//true
[Link]([Link](s3));//true
[Link]([Link](s4));//false
}
class Teststringcomparison1
public static void main(String args[])
String s1="Sachin";
String s2="sachin";
String s3=new String("Sachin");
String s4="Saurav";
[Link]([Link](s2));//true
[Link]([Link](s2));//false
[Link]([Link](s3));//true
[Link]([Link](s4));//false
String compare by compareTo()
method The String compareTo() method compares values lexicographically and
returns an integer value that describes if first string is less than, equal to or
greater than second string.
Suppose s1 and s2 are two string variables. If
: ● s1 == s2 :0
● s1 > s2 :positive value
● s1 < s2 :negative value
class Teststringcomparison4
public static void main(String args[])
String s1="Sachin";
String s2="Sachin";
String s3="Ratan";
[Link]([Link](s2));//0
[Link]([Link](s3));//1(because s1>s3)
[Link]([Link](s1));//-1(because s3 < s1 ) } }
Output:
0
-1
Sachin
Sachon
Sachin 6
compareToIgnoreCase()
String Concatenation by concat() method
The String concat() method concatenates the specified string to the end of
current string. Syntax: public String concat(String another)
Example
class TestStringConcatenation3
public static void main(String args[])
String s1="Sachin ";
String s2="Tendulkar";
String s3=[Link](s2);
// String s3=[Link](s1); // Tendulkar Sachin
[Link](s3);//Sachin Tendulkar
}}
OUTPUT : Sachin Tendulkar
Java String toLowerCase()
The java string toLowerCase() method returns the string in lowercase letter.
In other words, it converts all characters of the string into lower case letter.
The toLowerCase() method works same as toLowerCase([Link]())
method.
It internally uses the default locale
public class StringLowerExample
public static void main(String args[])
{ String s1="JAVACLASS stARTING";
String s1lower=[Link]();
// String s1lower=[Link](); // Make all the character upper case
[Link](s1lower);
}}
OUTPUT: javaclass starting
trim() method
The string trim() method eliminates white spaces before and after string.
String s=" Sachin ";
[Link](s); Sachin
sachin
[Link]([Link]());
startsWith() and endsWith() method
String s="Sachin";
[Link]([Link]("Sa"));
[Link]([Link]("n"));
true
true
charAt() method
The string charAt() method returns a character at specified index.
String s="Sachin";
[Link]([Link](0));
[Link]([Link](3));
length() method
The string length() method returns length of the string.
1. String s="Sachin";
2. [Link]([Link]()); //6
Java StringBuffer class
Java StringBuffer class is used to create mutable (modifiable) string. The
StringBuffer class in java is same as String class except it is mutable i.e. it can be
changed.
Constructor of StringBuffer
StringBuffer()
creates an empty string buffer with the initial capacity of 16.
StringBuffer(String str)
creates a string buffer with the specified string.
StringBuffer(int capacity)
creates an empty string buffer with the specified capacity as length .
Difference between String and StringBuffer
1) String class is immutable.
StringBuffer class is mutable.
2) String is slow and consumes more memory when you concat too many strings
because every time it creates new instance.
StringBuffer is fast and consumes less memory when you cancat strings.
3) String class overrides the equals() method of Object class. So you can compare
the contents of two strings by equals() method.
StringBuffer class doesn't override the equals() method of Object class