Wrapper objects: The wrapper classes discussed in Chapter 5 also provide you with the ability to instantiate objects that are wrapped versions of primitive variables. For example, to create a wrapped version of the
doublenumber x, you can do this:
double x = 55.0;Double xWrapped = new Double (x)
This instantiates an object of type
Double,which is a wrapped version of the primitive variable, x. Then it assigns a reference to that object to the reference variable,
xWrapped.The
Doubleclass has a number of prebuilt methods that work with
Doubleobjects. You can read about these methods in the Java API documentation on the
Doubleclass. The following program illustrates some of these methods:
/************************************************************** Wrapper.java* Dean&Dean** This program exercises some wrapped primitive numbers.*************************************************************/public class Wrapper { Public static void main(String[] args) { double x = 44.5; double y = 44.5; Double xW = new Double(x); // the object: wrapped x Double yW = new Double(y); // the object: wrapped y System.out.println(“object == object? “ + (xW == yW)); System.out.println(“value == value? “ + (xW.doubleValue( ) == yW.doubleValue( ))) ; System.out.println( “object.equals(object)? “ + xW.equals(yW) ) ; System.out.println(“object.compareTo(object) ? “ + xW.compareTo (yW) ) ; yW = new Double(y + 3.0); System.out.println(“object.compareTo(largerObject) ? “ + xW.compareTo (yW) ) ; yW = new Double(Double.NEGATIVE _ INFINITY); System.out.println(“-infinity isInfinite() ? “ + yW.isInfinite() ) ; } // end main } // end Wrapper classCompile and run this program, and display the output. Read about the
Doubleclass in the Java API documentation, and explain why each of the outputs comes out the way it does.
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.