String class in java

String is sequence of characters. In java String is class which used widely in java programming. Java organizes the string in its own way.

Java provides the java.lang.String class to deal with string.

• The String is immutable class, once created and initialized, it cannot be change.

• The + operator can be used for concatenation two strings.

• We can create the string object without new operator.

• String is a final class, no other class can be extend it.

• Java uses the String constant pool to store the string object.

• String class has overridden toString () and equals () method.

Example of String:


String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);// True
System.out.println(s1.equals(s2));// True

In the above example the s1 and s2 string are same, so even if you compare the string from == operator. The result it gives true. And using the equals method it give true, because the equals method has been overridden in String class. Now we will see the String example created by new operator.


String s3 = new String("abc");
String s4 = new String("abc");
System.out.println(s3 == s4);// False
System.out.println(s3.equals(s4));// True

If you see the s3==s4 give the result as false, because s3 and s4 has been created by new operator and they are referring to different memory locations. And s3.equals(s4) gives you the true as it compare the content the strings.

String class methods:

There is several method in java.lang.String class, which are very much usefully while dealing with string object. Some of them are listed below with example.

length ()

Count the no of characters in the string.


String s3 = new String("abc");
System.out.println(s4.length());// give as 3

trim()

This method return the String after removing the space characters from both of side of given string.


String s1 = " abc ";
System.out.println(s1.trim());

charAt(int)

If you know the index of a char you can get a char.


String s1 = "mahendra";
System.out.println(s1.charAt(3));// return as e

indexOf(char)

This is just opposite the charAt() method, if you know the char and want to get the first index of that char, you can use this method.


String s1 = "mahendra";
System.out.println(s1.indexOf('e'));// return as 3

lastIndexOf(char)

If you know the char and want to get the last index of that char, you can use this method.


String s1 = "mahendra";
System.out.println(s1.lastIndexOf('e'));// return as 3

split()

This method returns String array, it splits the given string by parameter string or char.


String s1 = "this is java string";
String[] s2 = s1.split(" ");
for (String string : s2) {
System.out.println(string);
}

Output as :


this
is
java
string

There are more methods in String class.

Apart from java.lang.String, java provides the StringBuffer and StringBuilder classes also. Both classes have all the methods of String in addition of some useful method also. And both classes have some advantage in term of use on the java.lang.String class. We will lean about them in separate tutorial.