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.
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:
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.
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.
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.