Java Method Chaining Within Strings

Method chaining in Strings is an exceptional and tricky concept in Java Programming. Method chaining in Strings can be widely used as it can perform various and multiple string operations in single statement. Chained methods may contain all the methods of String class.

The general form or Syntax of Chained methods is given as follows:-

result = method1(). method2(). …..method n()

There are theories that you can chain as many method you want but in practice not more than three methods are chained.

So what you are supposed to do when you want to use the chained methods. There are certain rules to follow. Let us take an example :-

You wish to concat a string in the end if give string then convert it to uppercase and then replace a character in the resulting string.

First of all Accomplish first task.

suppose our string is ‘ s ‘ and we have to append ” World” in the end then we would write
String s=”Java”;
s. concat(” World”);

Then Convert to uppercase,remember we are in continuation. That is we have to apply the method on the result of previous step thus we would write

s. concat(” World”). toUpperCase();

Similarly now add the replace method in the chain we will get something like this

s. concat(” World”). toUpperCase(). replace(‘V’,'W’);

and when you assign the result you will have to write

s = s. concat(” World”). toUpperCase(). replace(‘V’,'W’);

The output would be
“JAWA WORLD”

In Java examination the question on String methods chaining is frequently asked. What you need to know is the reverse process in deciphering the chained methods.

There are few rules too you must keep in your mind.
1. Determine what the leftmost method call will return. (Call it x)
2. Use this x as the invoking object on the method. If it is the last method then the result of this method call will be the result otherwise repeat the process as in step 1.

Take an example:-

s = s. concat(” World”). toUpperCase(). replace(‘V’,'W’);

First consider only concat method whose output would be “Java World”.
Now let it be x and invoke uppercase method on x,output would be “JAVA WORLD”.
Then the last call made on new x and final output is “JAWA WORLD”.

That is how it works I thinks it would be clear now and if you have any query dont hesitate to ask me. My mail id is given below in the box.

Comments

Vaibhav Pandey
About Vaibhav Pandey
Vaibhav Pandey got offered employment from an Indian Multinational IT Company. He is 21 years old. He has a huge interest in Java programming and has liked it from his study days. Vaibhav loves to blog and share his experiences and thoughts. He now resides in Lucknow, a state capital in India. Check out his blog at http://javatutorialsworld.blogspot.com.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>