no title...

Page 83

char converts byte or short int converts to byte, short, or char long converts to byte, short, char, or int float converts to byte, short, char, int, or long double converts byte, short, char, int, long, or float

Expressions Operators, variables, constants, and methods (calls and returns) are components of an expression. A component joined with an operator forms an expression. It is possible to create a compound expression by combining multiple expressions. However, the data types must be compatible to construct a valid compound expression. For example, an int data type can be mixed with a long data type because both are numeric.

Through the use of type promotion rules, the mixture of data types is converted to the same data type. Values that are returned from an expression depend on the data type. Data types char, byte, and short are advanced to int. An expression is promoted to long if one of the operands is a long data type. The same goes for float data types, if one of the operands is a float then the whole expression is a float. Likewise, an expression is promoted to a double data type if one of the operands is a double. The following is an expression example with mix data types byte and float:

public class Operators { public static void main (String[] args) { byte b = 7; float f = 34.56f; float total = (b * f); System.out.println("The total of byte 'b' times float 'f' (7 * 34.56) is: " + total); } }


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.