You are on page 1of 3

Primitive casting and conversions in Java

.Primitive type conversion


Suppose you need to convert from one numeric type to another when coding in Java. It's possible for the primitive type of a Java data value to change. In some cases, Java changes the data type automatically. When this change happens, it is known as conversion. Conversion can take place when a value is assigned to a variable when a value is passed to a method within arithmetic expressions

When a value is assigned to a variable


This code initializes two variables, an int and a long: int a = 5;long b = 0; You can assign the value of the int to the long using the line b = a; and Java will convert the value accordingly. This is called a widening conversion because the new data type 64 bits can accept and store a greater range of values than the original data type 32 bits. However, an attempt to assign the value of an int to a short results in an error message when the code is compiled. An attempt to convert an int to a short is a narrowing conversion, which is illegal in Java. This code, for example, would not compile: int g = 6;short h = 0; h = g; // will not compile! The Java compiler also does not allow any conversion that involves a boolean data type, such as in the following: int i = 3; boolean j;i = j; // invalid!

When a value is passed to a method


Conversions that are carried out when a method expects a particular data type use the same rules as assignment conversion. In this code, a float is passed to the sin method of the Math class: float myFloat = 12.3f; double myResult = Math.sin(myFloat); Because this method takes a single argument of type double, Java automatically promotes myfloat to a double.

Within arithmetic expressions

When you use arithmetic, bitwise, or shift operators in code, the Java compiler automatically performs any necessary widening conversions before completing the operations these are known as arithmetic promotions. Because of these arithmetic promotions, the results of these operations are always at least of type int.

The rules applied to binary arithmetic promotions


If one operand is a double, the other is converted to a double. For example, a is converted to a double in the following code: int a = 4;double b = 5; if (a + b < 12) System.out.println ("less than 12"); If neither operand is a double, but one of the operands is a float, the other is converted to a float. In this example, a is converted to a float: int a = 4;float b = 5; if (a + b < 12) System.out.println ("Less than 12"); If neither operand is a float or double, but one of the operands is a long, the other is converted to a long. In the following example, a is converted to a long: int a = 4;long b = 5; if (a + b < 12) System.out.println ("less than 12"); If none of the above cases apply, both operands are converted to int data types for example, this code requires both a and b to be converted to the int data type: short a = 4;short b = 5; if (a + b < 12) System.out.println ("less than 12"); In expressions that use unary arithmetic operators and the bitwise complement operator,bytes, shorts, and chars are automatically converted to ints. Other types of operand are not converted. So in this example, the operand g is converted to an intbefore it is decremented: short g = 4;int h = 3; int j = --g * h; In addition, if the code uses any of the shift operators, the same promotion rules that apply to unary operators are applied to each operand in turn. For example, in the following code x and y are converted to int data types before their bit patterns are shifted: short x = 3;short y = 2; int z = x >> y;

Primitive type casting


You can explicitly instruct the Java compiler to convert a value to a different primitive data type by performing a casting operation. You do this when you want to perform a narrowing conversion, because widening

conversions are carried out automatically. However, you can cast widening conversions explicitly in order to make your code more readable. You cast a conversion by preceding a value with the name of the new data type in parentheses. For example, in this code you precede the variable a with (byte) to convert the int to a byte: When converting an int to a byte, the compiler discards all but the last eight bits of the number. In this case, 1 0000 0001 (257) becomes 00000001 (1), so the value of b is 1. You need to take care when performing narrowing conversions because it is possible to lose or radically alter data when you do so. Consider the following code: short a = -4;short b = 1; short c = (short)(a >>> b); In this case, because the first operand in the expression is a short, it is automatically converted to an int for the operation to be performed. So the bit pattern of the short(1111 1111 1111 1100) 16 bits, becomes 1111 1111 1111 1111 1111 1111 1111 1100 32 bits. And because the operator is the unsigned right shift operator, the new bit is set to 0. This results in a bit pattern of 0111 1111 1111 1111 1111 1111 1111 1110, even though the operand is a negative number. However, when the result is cast back to a short, the new bit is lost and the leftmost bit of the short is set to 1. The result of the operation is a negative number just as if you had used the signed right shift operator (>>). So in this example, the original result, 0111 1111 1111 1111 1111 1111 1111 1110, is cast to short as 1111 1111 1111 1110.

Summary
Primitive data types can be converted from one type to another when you assign a value to a variable, when you pass a value to a method, or when using an arithmetic expression. In arithmetic expressions, conversion is known as arithmetic promotion. Promotion follows a number of rules, depending on the type of the operands in the expression and whether the operator is binary or unary. The Java compiler performs widening conversions that is, where the new data type can accept a wider range of values than the old data type automatically. You can explicitly instruct Java to convert a value using casting. You do this by preceding the value you want to convert with the new data type in parentheses. Casting allows you to perform narrowing conversions, which are otherwise illegal in Java. However, you should be careful when performing these conversions because they can result in meaningless or unexpected values.

You might also like