Java Reference
In-Depth Information
This scenario is an example of a concept called operator overloading . Some OO
languages allow operators such as + to be overloaded, or redefined by the developer.
In this way, special processing can be performed based on the class type. Although
this is a very powerful tool, it can easily lead to programs that are hard for anyone
but the original developer to understand. For example, a developer might look at
the statement x - y, and expect a straightforward algebraic function. But if the -
operator has been overridden when applied to y, then the developer may ask,
“What does x - y actually mean in this case, and why is it so different from a - b
in the previous statement?” Java does not allow operators to be overloaded by the
developer, although the compiler uses the concept itself in this case.
The conversion of objects and data types into String s is accomplished by the
toString() method. Every Java object and data type has a method of this name. The
+ operator simply calls this method and concatenates the String returned by the
method with the current String . (This is actually a good demonstration of the
power of polymorphism.)
Java's class definition for String s includes quite a few very useful methods. Some
are described in Table 8.1, along with the (roughly) equivalent COBOL statements.
TABLE 8.1 STRING MANIPULATION IN JAVA AND COBOL
Method
Description
COBOL Equivalent
char charAt (int index)
Return the character at position
Reference modification:
index in String.
STRING1 (3:1)
int compareTo
Compare two Strings; return -1, 0,
IF STRING1 <,=,> STRING2
(String string2)
or 1 depending on relative position
in Unicode order.
String concat
Return a new String where all
STRING STRING1 , STRING2
(String string2)
string2's characters have been
INTO STRING3 DELIMITED BY
appended to string1's.
SPACES (or SIZE)
boolean endsWith
Test if String ends in string2.
IF STRING1 (CTR:SIZE) =
(String string2)
STRING2
IF STRING1 = STRING2 1
boolean equals
Test if the text in String equals the
(object anyObject)
text in object.
continued
 
Search WWH ::




Custom Search