Java Reference
In-Depth Information
// Use the DecimalFormat Static method to convert a complex
// String to a double.
try {
d2 = new DecimalFormat().parse("3.0").doubleValue();
}
catch (ParseException e) {
d2 = 0;
}
System.out.println ("Integer value = " + i);
System.out.println ("Long value = " + l);
System.out.println ("Double value = " + d);
System.out.println ("Second Double value = " + d2);
6. Compile and run this class. The output should look like this:
...
compare reference variables = true
equals = true
compare reference variables = false
Integer value = 3
Long value = 3
Double value = 3.0
Second Double value = 3.0
7. Let's do some math with the wrapper classes. Add these Java statements to
the end of the previous statement (before the last two curly braces):
// Math and the numeric wrapper classes
// Make two objects of type Double.
// Try out both Constructors.
Double dd = new Double ("3.0");
Double dd2 = new Double (dd.doubleValue());
// Perform an addition function.
// Since the wrapper classes do not have any math function
// themselves,
// you have to convert them to primitive data types (by using
// the method
// doubleValue()) and then use the result as a constructor
// parameter to a
// new wrapper class.
d = dd.doubleValue ();
d2 = dd2.doubleValue ();
d += d2;
dd = new Double (d);
Search WWH ::




Custom Search