possible to return values from parameters in Java?
as I know, Java always invoke a method by "pass-by-value". but I see the reference for Android's NotificationManager.notify(String, int, Notification):
Returns
the id of the notification that is associated with the string
identifier that can be used to cancel开发者_JAVA百科 the notification
please refer to the reference: http://developer.android.com/reference/android/app/NotificationManager.html
How could this happend? Is there something I mis-understand?
BR, Henry
About this statement:
"Java passes primitives by value, but passes objects by reference."
This is not exact. Java passes everything by value and it doesn't pass objects at all.
- for primitives: copies are transmitted to methods (don't forget that String is not a primitive) - it's correct what you said
- for reference variables: they are also transmitted by value: a copy of the reference variable is transmitted to the method. So the object itself is never transmitted. The object can be altered in the method (by invoking some of its methods) and you will see the modifications after returning from the method (e.g. you change the "name" member from a Person object), but if you change the reference, this change will not be visible outside the method:
changing the reference is made by "new" operator or by an assignment like param = some_other_reference (where some_other_referece points to some other object on the heap). Changing the reference will not impact the "original" reference, but only the "copy reference" (the reference used inside the method).
Seems like the API Reference for NotificationManager is a bit messed up.
Here's the code as found via Google Code Search on NotificationManager and Android:
/**
* Persistent notification on the status bar,
*
* @param tag An string identifier for this notification unique within your
* application.
* @param notification A {@link Notification} object describing how to
* notify the user, other than the view you're providing. Must not be null.
* @return the id of the notification that is associated with the string identifier that
* can be used to cancel the notification
*/
public void notify(String tag, int id, Notification notification)
{
int[] idOut = new int[1];
INotificationManager service = getService();
String pkg = mContext.getPackageName();
if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
try {
service.enqueueNotificationWithTag(pkg, tag, id, notification, idOut);
if (id != idOut[0]) {
Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
}
} catch (RemoteException e) {
}
}
Obviously the parameter doesn't return a value. They meant to have a similar JavaDoc but probably made a mistake.
Look at the code for the other variant of notify
:
/**
* Persistent notification on the status bar,
*
* @param id An identifier for this notification unique within your
* application.
* @param notification A {@link Notification} object describing how to
* notify the user, other than the view you're providing. Must not be null.
*/
public void notify(int id, Notification notification)
{
notify(null, id, notification);
}
As you can see, this overloaded version just calls the primary implementation with a default tag
String value of null
.
Regarding the general question of passing by value and passing by reference, the simple/vulgarized explanation is:
- Java passes primitives by value,
- but passes objects by reference.
Refer to the comments by arnivan and Patrick for clarification.
The documentation seems wrong to me. The declaration says:
public void notify (String tag, int id, Notification notification)
but at the same time it says that it returns something.
I interpret it as this: the id
uniquely maps to the notification in question, and can be used when canceling the notification.
Agree with previous posters: the doc seem to be incorrect. The method needs three parameters but in the javadoc only two of them are mentioned. Just replace @return through @param and you've got the description for the id parameter:
the id of the notification that is associated with the string identifier that can be used to cancel the notification
edit: you can define this id yourself and use it later to cancel the notification.
精彩评论