What is Mixing Data Types?
As well as merging various operations, you can mix and match various numeric data types on the similar line. The program below uses both ints and doubles, for instance.
class IntAndDouble {
public static void main (String args[]) {
int i = 10;
double x = 2.5;
double k;
System.out.println("i is " + i);
System.out.println("x is " + x);
k = i + x;
System.out.println("i + x is " + k);
k = i * x;
System.out.println("i * x is " + k);
k = i - x;
System.out.println("i - x is " + k);
k = x - i;
System.out.println("x - i is " + k);
k = i / x;
System.out.println("i / x is " + k);
k = x / i;
System.out.println("x / i is " + k);
}
}
This program generates the following output:
% java IntAndDouble
i is 10
x is 2.5
i + x is 12.5
i * x is 25
i - x is 7.5
x - i is -7.5
i / x is 4
x / i is 0.25
%