Java String
Java String
• Concept of String
• Immutable String
• String Comparison
• String Concatenation
• In java, string is basically an object that
represents sequence of char values. An array
of characters works same as java string. For
example:
char[] ch={'j','a','v','a','t',‘p',‘a',‘n',‘e',‘l'};
String s=new String(ch);
• Java String class provides a lot of methods to
perform operations on string such as
compare(), concat(), equals(), split(), length(),
replace(), compareTo(), intern(), substring()
etc.
• The java.lang.String class
implements Serializable, Comparable and Char
Sequence interfaces.
• The CharSequence interface is used to
represent sequence of characters. It is
implemented by String, StringBuffer and
StringBuilder classes. It means, we can create
string in java by using these 3 classes.
String is Immutable??
• The java String is immutable i.e. it cannot be
changed. Whenever we change any string, a
new instance is created. For mutable string,
we can use StringBuffer and StringBuilder
classes.
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 java.lang.String
class is used to create string object.
How to create String object?
There are two ways to create String object:
(1)By string literal
(2)By new keyword
1)By String Literal
• String s=new String("Welcome");//creates two
objects and one reference variable
//In this 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 heap(non pool).//
Java String Example
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
newkeyword
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}}
Java String class methods
class immutablestring{
public static void main(String args[]){
String s=“ABC";
s.concat(“DEF ");//concat() method appends the string at the end
System.out.println(s);//will print ABC because strings are immutab
le objects
}
}
• But if we explicitly assign it to the reference variable,
it will refer to “ABCDEF" object.For example:
class immutablestring{
public static void main(String args[]){
String s=“ABC";
s=s.concat(" DEF");
System.out.println(s);
}
}
Why string objects are immutable in java?
class stringcomparison{
public static void main(String args[]){
String s1=“ABC";
String s2=“ABC";
String s3=new String(“ABC");
System.out.println(s1==s2);//true (because both refer to same ins
tance)
System.out.println(s1==s3);//false(because s3 refers to instance cr
eated in nonpool)
}
}
Output:true
false
3) String compare by compareTo() method
Output:0
1
-1
S2 =s1.toLowerCase;
S2 =s1.toUpperCase;
S2 =s1.replace(‘x’ , ‘y’);
S2 =s1.trim();
S1.equals(s2);
S1.equalsIgnoreCase(s2)
S1.length()
S1.CharAt(n)
S1.CompareTo(s2)
S1.substring(n)
S1.substring(n,m)
S1.indexOf(‘x’)
S1.indexOf(‘x’, n)
String Class
S1.setCharAt(n, ‘x’) modifies the nth character to x
S1.append(s2) Appends the string s2 to s1 at the end
S1.insert(n, s2) Insert the string s2 at the position n of the string s1
S1.setLength(n) sets the length of the string s1 to n. If n<s1.length()
s1 is truncated. If n>s1.length() zeros are added to s1
StringBuffer Class
String Comparision
• There are three ways to compare string in
java:
• By equals() method
• By = = operator
• By compareTo() method
String compare by equals() method
• class Teststringcomparison1{
• public static void main(String args[]){
• String s1="Sachin";
• String s2="Sachin";
• String s3=new String("Sachin");
• String s4="Saurav";
• System.out.println(s1.equals(s2));//true
• System.out.println(s1.equals(s3));//true
• System.out.println(s1.equals(s4));//false
• }
• }
Output:
• true
• True
• false
String compare by == operator
class Teststringcomparison3{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true (because both refer to same instance)
System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool)
}
}
Output:
true
false
The String compareTo() method compares String compare by compareTo()and
values lexicographically method
returns an integer value that
describes if first string is less than, equal to or greater than second string.
class Teststringcomparison4{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3="Ratan";
System.out.println(s1.compareTo(s2));//0
System.out.println(s1.compareTo(s3));//1(because s1>s3)
System.out.println(s3.compareTo(s1));//-1(because s3 < s1 )
}
}
Output:
0
1
-1
String Concatenation by + (string concatenation) operator
class TestStringConcatenation3{
public static void main(String args[]){
String s1="Sachin ";
String s2="Tendulkar";
String s3=s1.concat(s2);
System.out.println(s3);//Sachin Tendulkar
}
}
• Output: Sachin Tendulkar
String Length()
• length() method is used to find out length of string. String class has one method length()
called on string object.
• class len
{
public static void main(String arg[])
{
String s=new String("hello world");
String b="this is demo";
System.out.println("Length of s:"+s.length());
System.out.println("Length of b:"+b.length());
}
}
/* Output:
Length of s:11
Length of b:12
*/