Incremental Java
Mixed Type Operations

Mixed Type Operations

We've assumed that the operands of a binary operator have the same type, and the result of evaluation an expression is the type of the operands. Thus, 3 + 4 evaluates to an int, while 3.0 + 4.0 evaluates to a double.

What happens when you have one operand that's an int and the other is a double? In other words, what does 3 + 4.3 evaluate to?

In Java, integer addition and double addition use different hardware. Even though they may seem the same to you, they are actually quite different to the computer.

The computer wants to either add two int values or two double values.

Java's solution (as in many other languages) is type promotion.

Type Promotion

When one operand is an int and the other is a double, Java creates a new temporary value that is the double version of the int operand. For example, suppose that we are adding dd + ii where ii is an int variable.

Suppose the value of ii is 3. Java creates a temporary value 3.0 that is the corresponding double value. It then adds this temporary value to the other double operand.

Notice that ii still remains an int after this addition. The temporary double is used to perform the addition, and then it is thrown away.

Java has rules for doing type promotion that are somewhat more involved. Here's the basic rules:

  1. double
  2. float
  3. long
  4. int
  5. char or short
  6. byte
double is considered the "widest" value. byte is the narrowest. When you have two operands with types from this chart, pick the one with the lowest number (i.e., the widest). Thus, if one operand is short and another is long, then a temporary long value is made from short value and added to the long.

You might wonder, what are all these types? float is like double, but it has a much smaller range of values. However, it also uses less memory. In particular, float uses 4 bytes while double uses 8 bytes. Java seems to have a strong preference for double.

short is a 2 byte int. int is 4 bytes. long is 8 bytes. However, we mostly use int in Java. byte is a 1 byte int. The more bytes a type has, the larger the range of valid values.

char to int

You can convert a char to int.

For example,

  int val = 'c' ;
What does this do? What does it mean to convert a character to an int?

All types in Java are eventually stored as binary in computer memory. double, int, and even char are stored as 0's and 1's.

How does one store 'c' as a number? Java uses a code called Unicode to store characters as numbers. Unicode assigns each character to a number between 0 and 65535. This uses two bytes of memory.

Unicode was developed to replace ASCII, which is the most commonly used character set used. ASCII assigned each character to a number between 0 and 127. This uses 1 byte (actually only 7 bits).

The problem with ASCII is that it was developed for the English language. As computers are used with speakers of many different languages, the need for a character set that could handle non-English alphabets (and even Chinese and Japanese) was so great that Unicode was invented. Unicode is an extension of ASCII. All the ASCII values are also Unicode values.

For example, 'c' has ASCII value 99. It also has Unicode value 99 as well.

In C and C++, char isn't just a character: it is also seen as a one byte int. Because characters in Java are in Unicode, it uses two bytes. Thus, it can't serve as a one byte int. That's why Java has a byte type. It also uses short for a two byte int so a char can be used primarily to store characters.

Even so, the influence of C is profound on Java. In C, it's easy to convert from char to int. Just use a char where you would use an int. Thus, it makes sense in C to write 'A' + 32. Java permits this, only because it assumes many programmers come from a C background and expect to be able to perform this kind of addition.

Are you curious what happens to 'A' + 32? 'A' is converted to an int using the Unicode value since int is wider than char. Then, the addition is performed, and the result is an int.

Summary

Programmers tend to forget which types they're working with. They want to be able to mix double and int and not run into any problems. They want to treat char as an int so they can see the Unicode values.

Java adds support for mixing operands of different type, and performing type promotion. Type promotion allows you to look at certain types, and determine which type is wider.

It then creates a temporary value of the wider type to the operand with the narrower type. Thus, if you add a short to an int, short is the narrower type, so a temporary int with the closest value to the short is created. This temporary int value is added to the other int operand, and results is an int.

The types where type promotion can be done are: byte, short, char, int, long, float, and double.