public class StringMethods { public static void main(String[] arg) { String poem1 = " Tell me not, in mournful numbers "; //use a tab String poem2 = "Life is but an empty dream! "; String poem3 = "For the soul is dead that slumbers, "; String poem4 = "And things are not what they seem."; System.out.println (poem1); } // end main } // end StringMethods
Cascading is the process of sending a message to an object to create a new object, which in turn is sent a message to create another new object, which in turn is sent a message to create yet another new object, and so on. The results of messages are used as receivers of additional messages.
intitials = firstInit.concat(middleInit).concat(lastInit);In this example, a concat message (with middleInit as argument) is sent to firstInit. The firstInit object returns a reference to a new String object ("JF"). Another concat message (with lastInit) is sent to the new String object whose reference was just returned. A reference to yet another new String object is returned.
Composition is the process of sending a message to an object to create a new object whose reference is used as an argument in a message. In turn, this may yield a reference to another new object, which then could be used as an argument in yet another message, and so on. The results of messages are used as arguments in additional message. An example can be found from code in the book that performs the same function as the code in the cascading example.
initials = firstInit.concat(middleInit.concat(lastInit));In this example, a concat message (with lastInit as argument) is sent to middleInit. The middleInit object returns a reference to a new String object (consisting of the middle and last initials). The reference to this new object is sent as an argument in another concat message. A reference to yet another new String object is returned.
String poem1 = "Tell me not, in mournful numbers "; String poem2 = "Life is but an empty dream! "; String poem3 = "For the soul is dead that slumbers, "; String poem4 = "And things are not what they seem.";