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

Answer:

Yes, you should suspect something is wrong with the statement.

int sum = value + 41; // sum is an int, value is an object reference, 41 is an int literal

But the statement is correct and does what you want. (The program is not very sensible, though, since there is no real need for the Wrapper.)


UnBoxing

The Java compiler figures out what you want and creates bytecode that extracts the int from value, performs the addition, and puts the result in the sum. Extracting a primitive value from a Wrapper is called unboxing.

Here is some more suspicious code. The way to compute the sine of an angle (in radians) is Math.sin( theta ) where theta is a double. But in the code, angle is an object reference.

For more about Java math functions see chapter 13.


public class ShowSine
{
  public static void main ( String[] args )
  {
    Double angle = 0.5;   // angle in radians (auto boxed)
    double sine = Math.sin( angle );  // compute sine of the angle
    
    System.out.println( "sine of: " + angle " is " + sine ); 
  }
}

QUESTION 23:

Will the above code work?


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