String Class
The String class of java.lang package represents character strings. All string literals in Java
programs, such as "abc", are treated as instances of String class. Strings are constant; their
values cannot be changed after they are created. The String class is final, i.e. we cannot inherit
the String class.
Constructors
The String class supports several constructors. They are described below:
1 To create an empty String, we call the default constructor. For example,
String s = new String (); will create an object of String class with no characters in it.
2 String (char p [ ]) It will create an object of String class and initializes the object with an
array of characters. For example:
char p[] = { 'a', 'b', 'c' };
String s = new String (p); The constructor initializes the string with “abc”.
3 You can specify a sub-string of a character array to initialize a string using the following
constructor:
String(char p[ ], int startIndex, int numChars)
Here, startIndex specifies the index at which the sub-string begins, and numChars
specifies the number of characters to use. Here is an example:
char p[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
String s = new String(chars, 2, 3);
It creates a string and initializes the string with “cde”.
4 You can construct a String object that contains the same characters as another String
object using this constructor: String(String strObj);
class MakeString
{
public static void main(String s[])
{
char c[] = {'J', 'a', 'v', 'a'};
String s1 = new String(c);
String s2 = new String(s1);
System.out.println(s1);
System.out.println(s2);
}
}
The output from this program is as follows:
Java
Java
5 A simple String can be created using a string literal enclosed inside double quotes as
shown;
String str1 = “Learning Java”;
Methods:
1. char charAt(int index)
It returns the character at the specified index. If the specified index is <0 or >size of the string,
then StringIndexOutOfBoundsException is thrown.
String str ="study to night";
System.out.println (str. charAt (2));
Output: u
2. boolean equalsIgnoreCase(String s)
It checks whether two String objects are equal or not, ignoring the case of the characters. That is
upper case or lower case doesn't matters with this function.
String str = "java";
System.out.println (str. equalsIgnoreCase ("JAVA"));
Output: True
3. int length ()
The function returns the number of characters in a String.
String str=”This is a String”;
int len=str.length();
System.out.println (“Length is “+len);
Output: Length is 16
4. String concat (String str)
It concatenates the specified string to the end of this string and returns the concatenated String.
class StringDemo1
{
public static void main(String s[])
{
String s1="Java";
String s2=s1.concat ("World");
System.out.println (s1);
System.out.println (s2);
}
}
Output:
Java
JavaWorld
5. int compareTo(String str)
This method compares two Strings and returns an int value. It returns –
i) 0, if this string is equal to the string argument
ii) a value less than 0, if this string is less than the string argument
iii) a value greater than 0, if this string is greater than the string argument
class StringDemo2
{
public static void main(String s[])
{
String s1="Dog";
String s2="Lion";
int N1=s1.compareTo(s2);
int N2=s2.compareTo(s1);
System.out.println ("N1= "+N1);
System.out.println ("N2= "+N2);
}
}
Output:
N1= 8
N2= -8
6. int compareToIgnoreCase(String str)
Compares two strings, ignoring case differences and returns same type of values.
7. String substring(int index)
The method returns a new string which is a substring of this string. The substring begins from the
given index.
class StringDemo3
{
public static void main(String s[])
{
String s1="God is Great";
String s2=s1.substring(4);
System.out.println(s2);
}
}
Output: is Great.
8. String substring(int beginIndex, int endIndex)
The method returns a new string that is a substring of this string. The sub-string begins from the
index beginIndex and ends at (endIndex-1).
class StringDemo4
{
public static void main(String s[])
{
String s1="God is Great";
String s2=s1. substring (4,8);
System.out.println(s2);
}
}
Output: “is G”
9. String toUpperCase()
Converts all of the characters in this String to upper case and returns a new string.
10. String toLowerCase()
Converts all of the characters in this String to lower case and returns a new string.
Concatenation of Strings
Apart from concat() method, two strings can be appended using the + operator also. For example,
String s = "Hello" + "World";
System.out.println(s);
Output: HelloWorld.
Look at the following example:
String s = "Java";
s = s + "World";
From the above example, it looks like the string has been modified. But we know that string cannot
be modified and here practically the string has not been modified. Actually here an object of
StringBuffer class is created. The procedure is given below:
1. A StringBuffer object is created
2. The 1st string, i.e., “Java” is copied to the newly created StringBuffer object.
3. The 2nd string, i.e., “World” is appended to the StringBuffer (concatenation)
4. The result is converted to back to a String object.
5. Now the reference variable s refers to that new String.
6. The old String, that s previously referenced, is then garbage collected.
toString() Method
The toString() method of Object class is used when we need a string representation of an object.
This method can be overridden to get the String representation of an Object. Whenever we want
to concatenate any other primitive data type, or object of other classes with a String object,
toString() method is called automatically to change the other object or primitive type into string. For
example:
int age = 10;
String str = "He is" + age+ "years old.";
In above case 10 will be automatically converted into string for concatenation using valueOf()
method.
A number can be converted in to string using the valueOf() method of String class. It can take any
of the primitive data type numbers as an argument and produce a String:
int x= 20;
String s = String. valueOf(x); It converts the value of x from int to string and stores the string
value in s. We can use the toString method of any of the wrapper classes:
String s = Integer. toString(x);