Values of one type often need to be used as another type. Java converts some values automatically and requires the programmer to request others.

Implicit Conversion (Widening)

When a value of a smaller type is assigned to a variable of a larger compatible type, Java converts it automatically. This is widening conversion, and it is safe, since no information is lost. The order is: byte → short → int → long → float → double, and char → int. For example, int i = 100; double d = i; gives d = 100.0.

Definition: Type casting. The explicit conversion of a value from one data type to another, written by placing the target type in brackets before the value, as in (int) 3.7.

Explicit Conversion (Narrowing or Casting)

When a value of a larger type is assigned to a smaller type, the programmer must use a cast: (type) value. This is narrowing conversion, and information may be lost. (int) 3.99 gives 3, since the fraction is truncated, not rounded. (byte) 300 gives 44, since only the low 8 bits are kept (300 − 256 = 44). Casting a char to int gives its Unicode code: (int) 'A' is 65, and (char) 66 is 'B'.

Type Conversion in Expressions

In an expression with mixed types, Java applies numeric promotion before the operation. byte, short and char values are first promoted to int. Then, if either operand is double, the other becomes double; else if either is float, the other becomes float; else if either is long, the other becomes long; otherwise both are int. So the result type is that of the "larger" operand. A common surprise is that byte b = a + 1; does not compile when a is a byte, since a + 1 is an int and needs a cast. Another is integer division: 7 / 2 is 3, while 7 / 2.0 is 3.5.

Conversion between Strings and Numbers

Strings are not primitive, so they are converted with methods. Integer.parseInt("25") gives 25, Double.parseDouble("2.5") gives 2.5, and String.valueOf(25) or "" + 25 gives "25".

Table 2.2: Widening and narrowing

Conversion Example Result
Widening (automatic) double d = 100; 100.0
Narrowing (cast) (int) 3.99 3
Overflow on cast (byte) 300 44
char to int (int) 'A' 65
Mixed expression 7 / 2.0 3.5

Example 2.2: Effects of casting

Problem: Find the values of (a) (int) 9.81, (b) (byte) 130, (c) 5 / 2 and 5 / 2.0, (d) (char) ('a' + 2).

Solution:

  1. (int) 9.81 truncates to 9.
  2. 130 does not fit in a byte (max 127): 130 − 256 = −126.
  3. 5 / 2 is integer division, 2; 5 / 2.0 is 2.5.
  4. 'a' is 97, plus 2 is 99, which is 'c'.

Result: 9, −126, 2 and 2.5, and 'c'.

public class Casting {
    public static void main(String[] args) {
        int i = 100;
        double d = i;                         // widening
        System.out.println("widening: " + d);
        System.out.println("(int) 9.81 = " + (int) 9.81);
        System.out.println("(byte) 130 = " + (byte) 130);
        System.out.println("5 / 2 = " + 5 / 2 + ", 5 / 2.0 = " +
                5 / 2.0);
        System.out.println("(char) ('a' + 2) = " + (char) ('a' + 2));
        System.out.println("parseInt(\"25\") + 5 = " +
                (Integer.parseInt("25") + 5));
    }
}

Output

widening: 100.0
(int) 9.81 = 9
(byte) 130 = -126
5 / 2 = 2, 5 / 2.0 = 2.5
(char) ('a' + 2) = c
parseInt("25") + 5 = 30

Common mistakes

  • Expecting a cast to round. (int) 4.9 is 4.
  • Forgetting that byte + byte is an int.
  • Dividing two ints and expecting a decimal result.

Key takeaways

  • Widening (small to large) is automatic and safe.
  • Narrowing (large to small) needs a cast and may lose data.
  • In expressions, byte, short and char become int, then the larger type wins.
  • Use parseInt and parseDouble to convert strings.

Practice questions

  1. What is widening conversion?
  2. What is the value of (int) 7.8?
  3. Why does 7 / 2 give 3 in Java?

Answers to the practice questions

  1. Automatic conversion from a smaller type to a larger compatible type, without loss of data.
  2. 7, since the fraction is truncated.
  3. Both operands are int, so integer division is performed.

Sources

  • Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit II