Friday, August 21, 2009

Method chaining in Java 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.For more on methods click here.

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.

Useful links:-

How JVM handles string objects
Important String class methods

Sponsored Links :-
Check your General Knowledge
Get Your SCJP Exam Kit for Free

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP