Wednesday , January 12 2022

Method Chaining in Java Android– Unnamed Objects in Android

Method Chaining in Java Android– Unnamed Objects in Android – Whenever a method returns a data, the reference to that data is held in an object that has a name. Such Objects are called Named Objects.

Method Chaining in Java Android– Unnamed Objects in Android
Method Chaining in Java Android– Unnamed Objects in Android

For example, if you understand from the previous Code Fragement, etFN and etSN are a type of Named Objects because they have the Memory Address Stored of the Text Content of EditText Controls with R.id.etFN and R.id.etSN Layout IDs.

That is, whatever data we assign in any Named Object, it is really just the address of a memory location and not the actual data, and the computer system accesses and manipulates any data from its memory location and not Named. From objects.

For example, suppose that when we create a Layout of our Current Android App, the EditText Object with R.id.etFN Layout ID is stored at Memory Location 1000.

        EditText etFN = (EditText) findViewById (R.id.etFN);

So after the findViewByID () method is executed, the Memory Location Address Return as Return Value is 1000. Therefore, if we write the above statement in Normalized Form, then we can also write as follows-

EditText etFN = 1000

That is, the EditText object named etFN is actually referring to Memory Location 1000, not etFN itself holding an EditText Object as data, so etFN is really just a Named Object or in other words, etFN. Actually, the name of a memory location is so that that memory location can be identified not by its number but by its name.

Assigning a memory location to a named object is done so that the data of that memory location can be re-accessed and manipulated later when needed.

So once we assign the reference of the EditText object with R.id.etFN Layout ID to the Named Object named etFN, then if we need to convert the Stored Text Content to String on the Memory Location 1000 referenced by this etFN Object , Then to fulfill this need, we can write the following statement-

        String strFN = etFN.toString ();

Because the address of the Memory Location of the Text Content is already Saved in the Named Object named etFN. So if we want to do any kind of process with Stored Text Content on that Memory Location 1000, then we can access and Manipulate its Memory Location by etFN Named Object.

That is, if we say in simplest terms, etFN can also be called as Memory Location Address Number 1000 and the Compiler of any Programming Language internally recognizes memory location addresses only. So when the compiler executes this etFN.toString () statement, first it gets the address 1000 of the memory location where the Text Content Exist is from the etFN Named Object. So internally, before statement execution it is normalized as follows-

        String strFN = 1000.toString ();

And then convert the Text Content with that Memory Location 1000 to a String Value, say another Memory Location Address, and store it at 2000 and the Address 2000 of that new Memory Location returns as Return Value. Therefore, after execution, if we write the above statement again in normalized form, then we can write as follows-

        String strFN = 2000

Now again Memory Location Number 2000 is stored in a Named Object of a String Type named strFN. As a result, we can access and manipulate the Saved String Content on the Memory Location Number 2000 via strFN whenever we want, because the Memory Location Address 2000 as a value in strFN is Stored and not a String Value Stored as Data.

In this way, in fact internally all the work is done through Memory Addresses and not through Named Object and when we specify a Named Object, then internally, that Named Object would be representing a Memory Address only. is.

So if we want, we can access and manipulate Stored Data at any memory location without creating a Named Object. But apart from C / C ++, no modern compiler provides the facility to access and manipulate the physical memory addresses of computer systems. Therefore we cannot access & Manipulate any specific memory location even in Java programs.

The Named Object is needed only so that we can later access & Manipulate Stored Data at a Specific Memory Location as needed and if we do not hold the Memory Address of the data generated from a Calculation to a Named Object, So as soon as that statement is executed and the program control will move to the next statement, the data generated from Calculation will become completely Inaccessible. As a result, the Garbage Collector of Java will collect the memory location of that inaccessible data and free its resources completely.

For example when (EditText) findViewById (R.id.etFN); The statement is Execute and the Reference Memory Location of the EditText Control with R.id.etFN Layout ID is stored at 1000 and Return as the ReturnValue of the findValueById () method, then if we etFN the address of this Memory Location Do not Assign the Name of the EditText Type to the Named Object, so as soon as the Program Control executes this statement and moves to the next statement, the Returned Memory Location Address 1000 by this Statement will be completely Inaccessible. As a result, Java’s Garbage Collector will collect this completely free memory location and make its resources completely free.

Now the result of this process will be that in any case we will not be able to access & Manipulate the EditText Control with that R.id.etFN Layout ID. Because we did not hold his memory address in a named object, so we do not have his reference.

Now you will think that such a situation never arises, so you should always hold Memory Locations of Returned Values ​​by some methods in some Named Object.

Because many times the situation is such that we have to access, manipulate or process the data returning from any method only once in the entire program. Creating a Named Object to hold this type of One Time Data is not a sensible task as each CPU Create uses multiple CPU Cycles and each Extra CPU Cycle affects the performance of our Android App.

Therefore, instead of holding one time data returning from a method’s processing or generating an execution of an expression in a named object, it can be directly passed as a parameter in a method by the method chaining process. For that, any method can be Directory Invoke.

Method Chaining in Java Android– Unnamed Objects in Android

Because when a Processed Data Generate is generated by the execution of an operation, it is stored at some memory location only and as the Return Value, the address of that memory location is the Return, which is given as the value in a Named Object. Assign is done and later when an operation is performed by invoke a method on that Named Object, the compiler performs the operation on the data containing the address of the Stored Memory Location in that Named Object.

So when the address of a memory location is already available with us, instead of using it again in a Named Object and re-using it again, calling another method as Method Chaining on it directly to any other operation. Can be performed or it can be used directly as a value in any other method by passing like Parameter in any other operation and it is generally best to do so when the data that returns or generates. One has to use only once in any operation of the program. So when we write the following statement:

        double fn = Double.parseDouble (etFN.getText (). toString ());

So the compiler executes it by normalizing it as follows –

        double fn = Double.parseDouble (1000.toString ());

// 1000 is Memory Location of Text Content

Here 1000 is the address of the memory location where the Text Content Store of the EditText Control with R.id.etFN Layout ID is and when the toString () method is Execute, then the Compiler executes it by normalizing it as follows-

        double fn = Double.parseDouble (2000);

        // 2000 is Memory Location of String Content after conversion of Text Content

And here 2000 is the address of the memory location where Text Content is stored after converting it to String Content and when Double.parseDouble () method is executed, then Compiler, Stored String Content on Memory Location 2000 as Parameter And converts it to a value of double Data Type and stores it in the Named Variable of the Double Data Type named fn, so that the Stored Numerical Value in this Variable as Data in the following Statements is given as different types of Arithmetical Operations. Can be used in

Similarly, in the next statement as follows, the Text Content of the EditText Object with R.id.etSN Layout ID is converted to the value of double Data Type by the Double.parseDouble () method and stored in the Variable of Double Data Type named sn. , So that the Stored Numerical Value in this Variable can be used as Data in various types of Arithmetical Operations in the following Statements –

        double sn = Double.parseDouble (etSN.getText (). toString ());

As we discussed above, instead of creating a Named Object to store it in Named Object and then invoke a method like toString () for that Named Object, we want to return by etFN.getText () and etFN.getText () method. You can also invoke a method like toString () directly on the Unnamed memory address.

In this way, the process of applying the operation of another method to the data returning from one method is known as Method Chaining and using Dot Operator, we can make this Method Chaining according to the need any further Can continue.

That is, we can invoke the second method through Dot Operator on Returned Data from the first method, Invoke the third method using the Dot Operator in the same way on the Returned Data from the second method, on the Returned Data from the second method. Invoke the fourth method, and continue this Chaining up to any level as per the need.

In Modern Programming, this method of Method Chaining is used a lot, because there are many benefits of using it, some of which are advanced.

Read Also – Event Handling in Android using Interface Type

About dmtechnolab

Check Also

Simplified Coding

Android Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

Leave a Reply

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