© Doug Winnie 2021
D. WinnieEssential Java for AP CompScihttps://doi.org/10.1007/978-1-4842-6183-5_20

20. Managing Type

Doug Winnie1  
(1)
Mission Hills, KS, USA
 

When you work with variables and methods, you need to juggle different value types. Sometimes, you need to work with an integer, but you have a float. Or you have a double that needs to be an integer. How you manage types and force them to be other things is a common part of working with programming languages.

Mixing Types in Evaluations

When you mix types, all values in a mixed evaluation are converted to double before any operations are performed.

Take this, for example:
int a = 11;
int b = 4;
double x = 11;
double y = 4;
System.out.print(a / b);
System.out.print(", ");
System.out.print(x / y);

Get Essential Java for AP CompSci: From Programming to Computer Science now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.