3.java Strings
3.java Strings
String:
String is a sequence of characters. In java, objects of String are immutable
which means a constant and cannot be changed once created. Strings are immutable
because java uses the concept of string literal. Suppose there are 5 reference
variables, all refers to one object. If one reference variable changes the value of the
object, it will be affected to all the reference variables. That is why string objects are
immutable in java.
Strings Comparison:
We can compare string in java on the basis of content and reference.
There are three ways to compare string in java:
By equals() method
By = = operator
By compareTo() method
A) 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.
Example:
Class Test{
public static void main(String args[]){
String s1="Sachin";
String s2="SACHIN";
System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s2));//true
}
}
Output:
false
true
B) By == operator:
Example:
class Test{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true
System.out.println(s1==s3);//false
} }
Output:
true
false
C) 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
Example:
class Test{
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:
In java, string concatenation forms a new string that is the combination of
multiple strings. There are two ways to concat string in java:
By + (string concatenation) operator
By concat() method
A) By + (String Concatenation) operator:
It is used add strings.
Example:
class Test{
public static void main(String args[]){
String s="Second"+" Campus";
System.out.println(s);
}
}
Output:
Second Campus
B) By concat() method:
It concatenates the specified string to the current String.
Example:
class Test{
public static void main(String args[]){
String s1="Second";
String s2=" Campus";
String s3=s1.concat(s2);
System.out.println(s3);
}
}
Output:
Second Campus
StringBuffer:
Java StringBuffer is a class which 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. It is synchronized.
Some important methods in StringBuffer class:
1. append(): Concatenates the given argument with this string.
2. insert(int indexPosition, String s): Inserts the given string with in the
string at the given index position.
3. capacity(): The capacity() method of StringBuffer class returns the current
capacity of the buffer. The default capacity of the buffer is 16. If the number
of character increases from its current capacity, it increases the capacity by
(oldcapacity*2)+2. For example if your current capacity is 16, it will be
(16*2)+2=34.
4. length(): The length of a StringBuffer can be found by the length( )
method.
StringBuilder:
Java StringBuilder is a class which is used to create mutable (modifiable)
string. The Java StringBuilder class is same as StringBuffer class except that it is
non-synchronized. It is available since JDK 1.5.
String Vs StringBuffer Vs StringBuilder: