Passing objects or primitives involves the recognition of the method signature that is located immediately after the method name in parentheses. In the parentheses, the type of primitive/object required by the code to execute is followed by a name given to the primitive or object. In order to pass a parameter, the appropriate object or primitive must be placed in the parenthesis when the method is called.
The general format for the calling of an object is as follows:
objectName.methodName(required object/primitive);
Returning an object or primitive from a method requires that the method be defined with the keyword return. Once the method is called, the returned object must be assigned to a space in memory either declared at the time the method was called or previously declared in the program. When a method returns a value, it must return it into a container of the appropriate type. If a method returns an integer, there must be a variable of type integer waiting to receive it. Sample code follows:
String number = new String("2");
int a;
a = Integer.parseInt(number);
In this example, the variable number is the required parameter as defined in the API. The Integer.parseInt
method's signature asks for a string and returns an int representation of that string to the assignment operator.