String class is defined in java.lang package. String objects are immutable, means once created their value can not be changed. Although there are several methods defined in String class which can modify the value of a String object and the modified value is referred by another String object. These methods are called String manipulating methods.
Methods in String class
String class has several methods
1. int length () Method of string class returns the number of characters in a string , including space.
e.g String str=”this is java’; The output of the statement System.out.println(str); would be 12
2. String toString () method returns string version of any non string value. This method is defined in Object class which is defined in java.lang package and this class is super class of all java classes. This method has several overloaded versions.
3. String valueOf () method also returns string version of primitive values. This has also several overloaded versions.
4. char charAt (int index) method extract a single character from a string and returns it. This method takes the location or index of the character in the string.
e.g String str=”This is java”;
char ch=str.charAt(2); will return the character ‘i’. Index always starts from 0.
5. boolean equals () method of string class takes one string as argument and the method is invoked on another string object. This method compares the argument and the invoking string for equality. If both are same it will return true otherwise false. This method is case sensitive. “JAVA”.equals(“java”); will return false.
6. boolean equalsIgnoreCase () method has all the features same as equals () method excepting it is not case sensitive.
7. boolean startsWith (). This method takes a string as argument and is invoked on another string. If the invoking string starts with the argument string, it returns true otherwise false.
e.g Burdwan.startsWith(“Bu”); will return true
8. boolean endsWith () also works like startsWith () method excepting it checks whether the invoking string ends with the argument string.
e.g Burdwan.endsWith(“Bu”); will return false and Burdwan.startsWith(“an”); will return true
9. Difference between equals () method and == operator.
equals () method compares values of two string object whereas == operator or equality operator checks whether both the objects refers to the same instance.
e.g String str1=”Hi”; and String str2=”Hi”;
In this case str1.equals(str2) will return true but str1==str2 will return false.
Again String str1=”Hi”; and String str2=str1;
Here both str1.equals(str2) and str1==str2 will return true as both str1 and str2 refers the same location.
THIS PART WILL BE CONTINUED
Related Post: BlueJ Programs on String/Sentence
Methods in String class
String class has several methods
1. int length () Method of string class returns the number of characters in a string , including space.
e.g String str=”this is java’; The output of the statement System.out.println(str); would be 12
2. String toString () method returns string version of any non string value. This method is defined in Object class which is defined in java.lang package and this class is super class of all java classes. This method has several overloaded versions.
3. String valueOf () method also returns string version of primitive values. This has also several overloaded versions.
4. char charAt (int index) method extract a single character from a string and returns it. This method takes the location or index of the character in the string.
e.g String str=”This is java”;
char ch=str.charAt(2); will return the character ‘i’. Index always starts from 0.
5. boolean equals () method of string class takes one string as argument and the method is invoked on another string object. This method compares the argument and the invoking string for equality. If both are same it will return true otherwise false. This method is case sensitive. “JAVA”.equals(“java”); will return false.
6. boolean equalsIgnoreCase () method has all the features same as equals () method excepting it is not case sensitive.
7. boolean startsWith (). This method takes a string as argument and is invoked on another string. If the invoking string starts with the argument string, it returns true otherwise false.
e.g Burdwan.startsWith(“Bu”); will return true
8. boolean endsWith () also works like startsWith () method excepting it checks whether the invoking string ends with the argument string.
e.g Burdwan.endsWith(“Bu”); will return false and Burdwan.startsWith(“an”); will return true
9. Difference between equals () method and == operator.
equals () method compares values of two string object whereas == operator or equality operator checks whether both the objects refers to the same instance.
e.g String str1=”Hi”; and String str2=”Hi”;
In this case str1.equals(str2) will return true but str1==str2 will return false.
Again String str1=”Hi”; and String str2=str1;
Here both str1.equals(str2) and str1==str2 will return true as both str1 and str2 refers the same location.
THIS PART WILL BE CONTINUED
Related Post: BlueJ Programs on String/Sentence
No comments:
Post a Comment