Invocation Chaining

by Gabriela C. Perez.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on java  

You are here: Categories » Computers and technology » JAVA

Invocation chaining means that you are not limited to merely accessing one class/object member in a given statement with the . operator but may continue to access further members in a given statement. For example, let's say that we wanted to convert an integer value to a String object representation and then retrieve the first digit from the string as a character. We might perform this task as follows:

int i = 72;
   String str = String.valueOf(i);
   char firstChar = str.charAt(0);
   System.out.println(firstChar);   // prints 7

This code is perfectly fine, but we could have also implemented this code in a neater fashion using invocation chaining as follows.

int i = 72;
   char firstChar = String.valueOf(i).charAt(0);
   System.out.println(firstChar);    // prints 7 also

It's quite easy to see how this works. The . operator has a left (left to right) precedence.. With this in mind, we can see that the following statement is evaluated first of all:

String.valueOf(i)

This will return a new String object representation of the integer variable i passed to it. Then the method charAt is invoked on the new String object, returning the first character in the string to the variable firstChar. You should look at the statement String.valueOf(i) as a reference to the String object itself, which it is, as this is what the method returns. You can then access members of the String object like charAt that we accessed.

If we said that we had a Person object inside a Planet object that in turn was inside a SolarSystem object, and the SolarSystem object was inside a Universe object, we may access the Person object from a reference to the Universe object as follows.

Person bob = myUniverse.mySolarSystem.myPlanet.myPerson;
Leave a comment or ask a question
Total comments: 0

JAVA Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
SELECTION STATEMENTS IN JAVA - Like other programming languages java offers the control statements to control the execution of a program. The control statements in java are the selection statements, loop constructs and the jump (more...)
JAVA and its Advantages - In today's highly competitive world, JAVA has become one of the most secure technologies for website and software development. Prime benefits of JAVA are platform independency and easy availability (more...)
Java Tactics: Earn A Fortune With Java - Get the right Knowledge Get the right Papers Get the right Direction Get the Money Hi, my name's (more...)
Introduction to Object Oriented Programming (OOP) - The transition from a procedural programming (non-OOP) language to an object-oriented programming language is a large step for many programmers. It is true that both methods of programming can ulti (more...)
Operator Precedence in programming languages - Operator precedence deciphers the order in which calculations in an expression occur. Looking at the calculation example 3 + 4 * 6, the answer could be calculated by adding 3 and 4, which gives (more...)
Arithmetic Assignment Operators - The following assignment operators are similar to the increment and decrement operators that we have just seen. They are used so that you do not need to enter the source variable twice when ass (more...)
Bitwise Operators - The following table shows the standard bitwise operators in Java and a description of them. (more...)
Regular Expressions in Java - A regular expression is a code that is used to match a pattern in a given string and is new to Java 1.4. Regular expressions are made up of normal characters and metacharacters. Normal characters a (more...)
Character Escape Sequences - Character escape sequences allow for a character to be interpreted differently than its literal value. Character escape sequences are defined using the backslash (\) character, followed by th (more...)
Conditional Statements - The ability to choose the path that your program takes, based on any given data, is the key to all functionality in programming. In order to create conditional statements, we must first learn a (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.