Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Conversions and Casting

With seven different numeric types that may be freely intermixed in expressions, it’s important to understand the rules by which this intermixing takes place. Java converts between primitive data types in expressions, in assignment statements, as a result of explicit casts, and during method invocations. You need to understand when conversion can occur and what happens when it does.

Using a cast

Java enables you to explicitly change the type of a value using a cast. A cast is just the name of the type to which you wish to change the value, enclosed in parentheses. For example, suppose you’ve read a byte into the byte variable b, perhaps using DataInputStream’s readByte() method. Then you can cast that variable to the int type like this:

     int n = (int) b;

This doesn’t permanently change the type of b. It just makes a temporary copy of the value of b and puts it in an int. This int is then assigned to the int variable n.

The second place in which conversion of primitive types takes place is in arithmetic expressions. Expressions range from simple ones, like a + b, to considerably more complex ones such as 1.65 * (32 / -9.8 - c++)/0.65. The expression is evaluated using the widest type present in the expression, where doubles are wider than floats, which are wider than longs, which are wider than ints. Thus, if any of the operands are doubles, all operands are promoted to doubles. If no operands are doubles but some are floats, then all operands are promoted to floats. If no operands are floats or doubles but some are longs, then all operands are promoted to longs. Finally, if an expression contains no floats, doubles, or longs, then all operands are promoted to ints. All arithmetic in Java uses at least ints. Shorts, bytes, and chars are never used directly in arithmetic expressions.

The third place in which conversions take place is in assignment statements; that is statements like

     long a = 3 + 4;

In this example 3 is an int, 4 is an int, and the result of their addition is the int value 7. This must be promoted to a long before being assigned to a. Conversions in the other direction may lose information. Not all longs have equivalent int values. For example, 5294967295L is a valid long, but it’s more than two times larger than the largest int:

     int n = 5294967295L;

If you try to assign 5294967295L to an int variable, you get the compile-time error Error: Incompatible type for declaration. Explicit cast needed to convert long to int. The compiler sees that you may lose information and warns you about it. However, the compiler isn’t that smart. The following assignment, which does not lose information, also causes a compiler error:

     int m = 3L;

In both of these cases, you can tell the compiler that you’re aware of the problem, that you accept that your assignment may lose information, and that you want it to go ahead anyway. You do this with an explicit cast to the type on the left side. For example:

     int m = (int) 3L;
     int n = (int) 5294967295L;

This tells the compiler that you know what you’re doing, that you’ve given thought to whether this cast will lose data. Java tries to prevent you from performing operations that may lose data, but it does allow you to do so if you use a cast to tell it that you know what you’re doing.

The final place where conversions take place is in method calls. Suppose you try to call MethodA(24). The compiler first tries to find a perfect match, a version of MethodA that takes as an argument a single int. However, if it fails in this effort, it will next look for a MethodA that takes a long as an argument. If it finds one, it promotes 24 to 24L and calls MethodA(long). Failing to find a MethodA that takes a long, Java next looks for one that takes a float. Failing to find that, it looks for one that takes a double. Only if it can’t find any of these will Java produce a compile-time error.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.