CHAINED METHODS IN STRING:

result = method1().method2().method3();
when you encounter them:
1. Determine what the leftmost method call will return (let’s call it x).
2. Use x as the object invoking the second (from the left) method. If there
are only two chained methods, the result of the second method call is
the expression's result.
3. If there is a third method, the result of the second method call is used
to invoke the third method, whose result is the expression's result—
for example,
String x = "abc";
String y = x.concat("def").toUpperCase().replace('C','x');
//chained methods
System.out.println("y = " + y); // result is "y = ABxDEF"
The literal def was concatenated to abc, creating a temporary, intermediate String (soon to be lost), with the value abcdef.
The toUpperCase() method created a new (soon to be lost) temporary String with
the value ABCDEF. The replace() method created a fi nal String with the value ABxDEF,
and referred y to it.

No comments:

Post a Comment