go to previous page   go to home page   go to next page

Answer:

Yes — so no conversion is required.


Examples with Floating Point Types

Here is another program. In this case, the 32 bit integer value in z can be converted to the required 64 bit floating point value without loss of information, so the compiler does it automatically.


class CosEg2
{

  public static void main ( String arg[] )
  {
    int z = 0;

    System.out.println( "cos is:" + Math.cos( z ) );
  }
}

Here is another case:

class CosEg3
{

  public static void main ( String arg[] )
  {
    long w = 0;

    System.out.println( "cos is:" + Math.cos( w ) );
  }
}

Data of type long can be converted to type double, but some precision may be lost. In this case, also, the compiler does the conversion automatically. (In this particular case, no precision is lost because 0 can be represented accurately in both types. But in general, there may be loss of precision in converting from long to double.)


QUESTION 11:

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