(Thought question:) Do you think that the following will work:
class CosEg4
{
public static void main ( String arg[] )
{
long w = 0;
System.out.println( "cos is:" + Math.cos( (double)w ) );
}
}
Yes—the type cast (double)
is not really needed
since it asks for a conversion that would be done anyway.
But sometimes programmers like to do this to make it clear what is
happening.
Examine the following code fragment:
SomeClass s = new SomeClass(); // make a new SomeClass object int x = 32; s.someMethod( x ); // call a method of the object s System.out.println( "x is: " + x ); // what will be printed?
When a primitive variable is used as a parameter for any method at all, the method will not change the value in the variable. So the above fragment will print:
x is: 32
This will be true, regardless of what class someClass
is
and what method someMethod
is.
When an object is used as a parameter for some methods of some classes, the object's data will change. You have to read the documentation of the class to know when this is the case. Most methods do not change their parameters, since doing so can cause confusion and errors. For the most part, parameters are used to pass data into a method to tell it what to do.