1 of 15

Type Conversions and Casting

Kaustubh Kulkarni

2 of 15

Automatic Conversion

  • When one type of data is assigned to another type of variable, an automatic type conversion will take place if the following two conditions are met:
    • The two types are compatible.
    • The destination type is larger than the source type.
  • When these two conditions are met, a widening conversion takes place.
  • For example, an int type variable is large enough to hold byte values.
  • For widening conversions, the numeric types, including integer and floating-point types, are compatible with each other.

3 of 15

Examples:

  •         // assigning byte to short
  •         byte b = 127;
  •         short s = b; // Valid, byte is smaller than short
  • �        // assigning short to int
  •         int i = s; // Valid, short is smaller than int
  • �        // assigning int to long
  •         long l = i; // Valid, int is smaller than long
  • �        // assigning int to float
  •         float f = i; // Valid, int is smaller than float
  • �        // assigning float to double
  •         double d = f; // Valid, float is smaller than double

​

4 of 15

  • Refer sizeOf.java
  • Refer AutomaticConversions.java

​

5 of 15

Storing Literals

  • Java also performs an automatic type conversion when storing a literal integer constant into variables of type byte, short, long, or char.

6 of 15

Example

  • byte b = 10;
  • // Valid, 10 is within the range of byte (-128 to 127)
  • short s = 20000;
  • // Valid, 20000 is within the range of short (-32768 to 32767)
  • long l = 9223372036854775807L;
  • // Valid, 9223372036854775807 is within the range of long
  • char c = 'A’;
  • // Valid, 'A' is a character with a corresponding Unicode value�

​

7 of 15

Narrowing Conversions

  • Although the automatic type conversions are helpful, they will not fulfill all needs.
  • For example, what if you want to assign an int value to a byte variable?
  • This conversion will not be performed automatically, because a byte is smaller than an int.
  • This kind of conversion is sometimes called a narrowing conversion, since you are explicitly making the value narrower so that it will fit into the target type.

8 of 15

Casting

  • To create a conversion between two incompatible types, you must use a cast.
  • A cast is simply an explicit type conversion.
  • It has this general form: (target-type) value
  • Here, target-type specifies the desired type to convert the specified value to.
  • For example, the code fragment in the following slide casts an int to a byte.
  • If the integer’s value is larger than the range of a byte, it will be reduced modulo (the remainder of an integer division by the) byte’s range.

9 of 15

  • int a;
  • byte b;
  • // …
  • b = (byte) a;

10 of 15

Byte Overflow illustration

11 of 15

Byte overflow and underflow

0

1

2

3

126

127

-1

-2

-3

-127

-128

Overflow

Underflow

12 of 15

Truncation

  • A different type of conversion will occur when a floating-point value is assigned to an integer type: truncation.
  • As you know, integers do not have fractional components.
  • Thus, when a floating-point value is assigned to an integer type, the fractional component is lost.
  • If the size of the whole number component is too large to fit into the target integer type, then that value will be reduced modulo the target type’s range.

13 of 15

Automatic Type Promotion in Expressions

  • byte a = 40;
  • byte b = 50;
  • byte c = 100;
  • int d = a * b / c;

14 of 15

  • byte b = 50;
  • b = b * 2; // Error! Cannot assign an int to a byte!

15 of 15

  • byte b = 50;
  • b = (byte)(b * 2);