2.9. Character Data Type and Operations

 
[Page 40 ( continued )]

2.8. Numeric Type Conversions

Sometimes it is necessary to mix numeric values of different types in a computation. Consider the following statements:

    byte    i =   100   ;   long   k = i *   3   +   4   ; double d = i *   3.1   + k /  2  ; 

Are these statements correct? Java allows binary operations on values of different types. When performing a binary operation involving two operands of different types, Java automatically converts the operand based on the following rules:

  1. If one of the operands is double , the other is converted into double .

  2. Otherwise, if one of the operands is float , the other is converted into float .

  3. Otherwise, if one of the operands is long , the other is converted into long .

  4. Otherwise, both operands are converted into int .

For example, the result of 1 / 2 is , because both operands int values. The result of 1.0 / 2 is 0.5 , because 1.0 is double and 2 is converted to 2.0 .

The range of numeric types increases in this order:

You can always assign a value to a numeric variable whose type supports a larger range of values; thus, for instance, you can assign a long value to a float variable. You cannot, however, assign a value to a variable of a type with smaller range unless you use type casting . Casting is an operation that converts a value of one data type into a value of another data type. Casting a variable of a type with a small range to a variable of a type with a larger range is known as widening a type . Casting a variable of a type with a large range to a variable of a type with a smaller range is known as narrowing a type . Widening a type can be performed automatically without explicit casting. Narrowing a type must be performed explicitly.

The syntax for casting gives the target type in parentheses, followed by the variable's name or the value to be cast. For example:

   float   f = (   float   )   10.1   ;   int   i = (   int   )f; 


[Page 41]

In the first line, the double value 10.1 is cast into float . In the second line, i has a value of 10 ; the fractional part in f is truncated.

Caution

Casting is necessary if you are assigning a value to a variable of a smaller type range, such as assigning a double value to an int variable. A compilation error will occur if casting is not used in situations of this kind. Be careful when using casting. Lost information might lead to inaccurate results.


Note

Casting does not change the variable being cast. For example, d is not changed after casting in the following code:

   double   d =   4.5   ;   int   i = (   int   )d;  // d is not changed  


Note

To assign a variable of the int type to a variable of the short or byte type, explicit casting must be used. For example, the following statements have a syntax error:

   int   i =   1   ;   byte   b = i;  // Error because explicit casting is required  

However, so long as the integer literal is within the permissible range of the target variable, explicit casting is not needed to assign an integer literal to a variable of the short or byte type. Please refer to §2.7.2, "Numeric Literals."


Listing 2.4 gives a program that displays the sales tax with two digits after the decimal point.

Listing 2.4. SalesTax.java
 1   public class   SalesTax {  2   public static void   main(String[] args) {  3   double   purchaseAmount =   197.55   ;  4   double   tax = purchaseAmount *   0.06   ;  5     System.out.println((   int   )(tax *   100   ) /   100.0   );  6   }  7 } 

Variable purchaseAmount is 197.55 (line 3). The sales tax is 6% of the purchase, so the tax is evaluated as 11.853 (line 4). The statement in line 5 displays the tax 11.85 with two digits after the decimal point. Note that (int)(tax * 100) is 1185 , so (int)(tax * 100) / 100.0 is 11.85 .

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net