(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 ) );
  }
}

A good answer might be:

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.


Can a Method Change a Primitive Type?

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.

QUESTION 11:

Examine the following code fragment:

Point B = new Point();
short x = 16, y = 12;

B.move( x, y );

The parameters x and y contained short values which had to be converted into int values for the method. Were the contents of x and y altered by this conversion?