Java Reference
In-Depth Information
System.out.println ("dd after addition = " + dd);
// Or combine the previous expressions into one statement:
dd = new Double (dd.doubleValue () + dd2.doubleValue());
System.out.println ("dd after addition = " + dd);
// Compare two Double objects.
// Note that the wrapper classes do have comparison methods
// (compare(), and
// equals()), so you don't have to extract the value into a
// primitive data type.
if (dd.equals (dd2)) {
System.out.println ("dd equals dd2");
}
else {
System.out.println ("dd does not equal dd2");
}
// Let's use the autoboxing features,
// Let the compiler perform any conversions
// necessary to add 11 to dd
dd = dd + 11;
System.out.println ("dd after addition with autoboxing = "
+ dd);
8. Compile and run this class. The output should look like this:
nd Double value = 3.0
dd after addition = 6.0
dd after addition = 9.0
dd does not equal dd2
dd after addition with autoboxing = 20.0
9. Next you'll experiment with the StringBuffer class. Add these Java state-
ments to the end of the previous statement (before the last two curly braces):
// Experiment with StringBuffers.
// Convert this String array to a StringBuffer.
String[] inputWords = {"These", "are", "words", "in", "a",
"sentence"};
// Build a sentence.
StringBuffer sent = new StringBuffer();
for (i = 0; i < inputWords.length; i++) {
// Build up the sentence. Place each word in the StringBuffer
// followed by a
// space. The StringBuffer will be automatically resized for
// each append.
Search WWH ::




Custom Search