9. Using object methods
Using object methods
The Java String() object
The Java libraries that are part of the API contain many class definitions which you the programmer can include in your code. Some of these objects are intrinsic to the Java language, such as the String() object. This object is part of the java.lang package that contains a large number of intrinsic Java classes. When you install the Java SDK, all these packages with their objects are automatically installed on your system. Not all of them can be automatically accessed however - some of them have to be specially included in your code; we'll get to this a little later.
String() being an intrinsic object, is available to all Java code automatically. The String() object has a length() method that returns an integer which is the number of characters in the string object calling the method. The string object may be a string literal such as "Java", or it may be a string object variable. To use an object method, we use the name of the object or the object itself, followed by a period, followed by the method name, with any required arguments in the method parentheses:
//called by string literal (automatically a String object); returns 4 int result = "Java".length(); String myString = new String("Hello World!"); //instantiate String object result = myString.length(); //called by String object; returns 12
Note that because length() is an instance method, it is called on the name of the instance and does not take the instance as argument:
int result = length(myString); //illegal method call
Any string in Java is an instance of the String() object, so the length() method can be called on any string. In fact, because Java automatically converts any string into a String() object, the instantiation:
String myString = new String("Hello World!");
is redundant - simply String myString = "Hello World!"; will do.
The String() object has a large number of methods - some of the more useful are:
- charAt(int indx) - returns the character in the string at position
indx - indexOf(String str) - returns the index within the string of the first occurence of substring
str - lastIndexOf(String str) - returns the index within the string of the last occurence of substring
str - substring(int beginIndex, int endIndex) - returns a new string that is a substring of this string
- toLowerCase() - returns a lower-case copy of the string
- toUpperCase() - returns an upper-case copy of the string
- trim() - returns a copy of the string with leading and trailing white space removed
- toString() - converts the string object to a string literal, i.e. it returns the string itself.
Please note: the indices of characters are zero based, but the length is not - this allows a null string to have a length of 0. Note also that in Java, strings are immutable - no method of the String object can change that String object. For example, the toUpperCase() method does not change the string to uppercase characters - it merely returns a copy of the string in which all the characters have been converted to uppercase characters.
In the code for the MyStringApp class below you will see the two chars \n used several times. This is an escaped n and in Java it is a newline instruction when included in a string, i.e. it causes the portion of the string that follows it to be written to a new line in the output. The Java escape character is a backslash and it is also used to make the interpreter interpret the character that follows it literally. In the code below we see an example of this when we need to include a double quote in a string. Normally a double quote on its own indicates the beginning or end of a string, so if we want Java to see a double quote as a literal character and not a string delimiter, we must escape it with a backslash: \".
Compile and run the Java class below - MyStringApp.java demonstrates calling the methods of a String object, but exactly the same technique is used to call any method of any object, namely:
objectName.methodName(arguments);
Left click this link to download MyStringApp.java.
/**
* The MyStringApp class implements an application that
* demonstrates the use of some String object methods.
*/
public class MyStringApp {
public static void main(String[] args) {
System.out.println("\n\"This is a string literal of length: \" " +
"\"This is a string literal of length: \" ".length());
String myString = "James James Morrison Morrison Weatherby George Du Preez";
System.out.println(myString.toString());
System.out.println(myString);
System.out.println("The first index of J is: " + myString.indexOf("J"));
System.out.println("The last index of J is: " + myString.lastIndexOf("J"));
System.out.println("The first index of Morrison is: " +
myString.indexOf("Morrison"));
System.out.println("The substring from index 30 to 46 is: " +
myString.substring(30, 46));
System.out.println(myString.toUpperCase());
}
}






