Monday, May 3, 2010

Numeric promotion

When binary operations are applied to numeric arguments (integer and floating-point), numeric
promotion is performed before the operation takes place. The numeric promotion consists of converting
the values of the operands to a common type. The rules for this conversion are straightforward.
  •  If one of the operands is a double, then the other is converted to a double.
  •  Otherwise, if one of the operands is a float, then the other is converted to a float.
  •  Otherwise, if one of the operands is a long, then the other is converted to a long.
  •  Otherwise, both operands are converted to int values.
Make sure that you remember these rules. You won't be tested on the rules themselves, but you'll need
to know them to work through related questions.

One thing that you probably will see on the test is + operations involving a numeric value and a String value. These questions test your knowledge of the difference between the arithmetic + operator and the String + operator. The String + operator results in the concatenation of two String objects. For example, "abc" + "def" results in "abcdef". But what about, 10 + "0" or "2.4" + 2.5?
The rule that you should remember is that when one of the operands in a + operation is a String, then
the other operand is converted to a String. Therefore, 10 + "0" results in the String "100" and
"2.4" + 2.5 results in the String object "2.42.5". The other operand is converted to a String
object in one of two ways, depending on whether the operand is an object or a value of a primitive type.
If the other operand is an object, then its toString() method is invoked to convert it to a String
object. If the other operand is a value of a primitive type, then an object of the type's wrapper class is
created and the object's toString() method is invoked
to convert it to a String object.

No comments:

Post a Comment

Chitika