Java Reference
In-Depth Information
This program generates the following output:
As you can see, in this case, the actions inside change( ) have affected the object used as
an argument.
Ask the Expert
Q :
Is there any way that I can pass a primitive type by reference?
A : Not directly. However, Java defines a set of classes that wrap the primitive types in
objects. These are Double , Float , Byte , Short , Integer , Long , and Character . In
addition to allowing a primitive type to be passed by reference, these wrapper classes
define several methods that enable you to manipulate their values. For example, the
numeric type wrappers include methods that convert a numeric value from its binary
form into its human-readable String form, and vice versa.
Remember, when an object reference is passed to a method, the reference itself is passed
by use of call-by-value. However, since the value being passed refers to an object, the copy
of that value will still refer to the same object referred to by its corresponding argument.
Returning Objects
A method can return any type of data, including class types. For example, the class Er-
rorMsg shown here could be used to report errors. Its method, getErrorMsg( ) , returns
a String object that contains a description of an error based upon the error code that it is
passed.
Search WWH ::




Custom Search