# Mixed-Type Arithmetic and Type Casting

So far, we’ve used a single data type in the expressions we’ve evaluated. But life isn’t always like that. Calculations often involve data of different primitive types.

When calculations of mixed types are performed, lower-precision operands are converted, or promoted, to the type of the operand that has the higher precision.

This also occurs with human minds broadly. We can notice that there is a tendency towards survival/ greater alignment with Born Rule’s probability density, but we cannot use simple rules that chunk minds into the big 5 psychometric traits for instance, and deduce which should be converted to which on the ascent.

In Java, however, the promotions of data types is quite easy to understand:

The promotions are performed using the first of these rules that fits the situation:

1.   If either operand is a double, the other operand is converted to a double.

2.   If either operand is a float, the other operand is converted to a float.

3.   If either operand is a long, the other operand is converted to a long.

4.   If either operand is an int, the other operand is promoted to an int.

5.   If neither operand is a double, float, long, or an int, both operands are promoted to int.

The table below summarizes these rules of promotion.

This arithmetic promotion of operands is called implicit type casting because the compiler performs the promotions automatically, without our specifying that the conversions should be made. Note that the data type of any promoted variable is not permanently changed; its type remains the same after the calculation has been performed.

The table above shows many rules, but essentially, any arithmetic expression involving integers and floating-point numbers will evaluate to a floating-point number. Lines 9 to 12 of the code below illustrates the rules of promotion. At line 11, the expression PI * radius * radius is a mixed-type expression. This expression will be evaluated left to right, evaluating the mixed-type expression PI * radius first. PI is a double and radius is an int. Therefore, radius is promoted to a double (4.0) and the result of PI * radius is a double (12.56636). Then, the next calculation (12.56636 * radius) also involves a mixed-type expression, so radius is again promoted to a double (4.0). The final result, 50.26544, is a double and is assigned to area. The figure below shows the output of the complete program.

Sometimes, it’s useful to instruct the compiler specifically to convert the type of a variable. In this case, you use explicit type casting, which uses this syntax:

(dataType) (expression)

The expression will be converted, or type cast, to the data type specified. The parentheses around expression are needed only when the expression consists of a calculation that you want to be performed before the type casting.

Type casting is useful in calculating an average. The example shows how to calculate your average test grade. Your test scores are 94, 86, 88, and 97, making the combined total score 365. We expect the average to be 91.25.

Line 16 first attempts to calculate the average but results in a wrong answer because both total and count are integers. So integer division is performed, which truncates any remainder. Thus, the result of total / count is 91. Then 91 is assigned to average, which is a double, so 91 becomes 91.0. Line 22 is a second attempt to calculate the average; again, this code does not work correctly because the parentheses force the division to be performed before the type casting. Thus, because total and count are both integers, integer division is performed again. The quotient, 91, is then cast to a double, 91.0, and that double value is assigned to average.

At line 28, we correct this problem by casting only one of the operands to a double. This forces the other operand to be promoted to a double. Then floating-point division is performed, which retains the remainder. It doesn’t matter whether we cast total or count to a double. Casting either to a double forces the division to be a floating-point division.

This is the output:

# Data Type: Boolean

The boolean data type can store only two values, which are expressed using the Java reserved words true and false, as shown in the table below. Booleans are typically used for decision making and for controlling the order of execution of a program. Here are examples of declarations of boolean variables:

So behind the declaration of the boolean variables you see above which contains the identifiers: isEmpty, mindful, compassionate, and the type: boolean, you should realize that there is only the storing of 1 and 0: true and false.

# Integer Data Types

An integer data type is one that evaluates to a positive or negative whole number. Java provides four integer data types, int, short, long, and byte. The int, short, long, and byte types differ in the number of bytes of memory allocated to store each type and, therefore, the maximum and minimum values that can be stored in a variable of that type.

All of Java’s integer types are signed, meaning that they can be positive or negative; the high-order, or leftmost bit is reserved for the sign. The table below summarizes the integer data types, their sizes in memory, and their maximum and minimum values.

In most applications, the int type will be sufficient for your needs, since it can store positive and negative numbers up into the 2 billion range – that’s 2/3rds of the base pairs in a human genome – in other words .666, in other words the mark of the beast, which roughly means “everything goes smoothly” in China.

The short and byte data types typically are used only when memory space is critical, and the long data type is needed only for data values larger than 2 billion. Let’s look at some examples of integer variable declarations. Note that the variable names clearly indicate the data that the variables will hold.

Floating-point data types store numbers with fractional parts. Java supports two floating-point data types: the single-precision float and the double-precision double.

The two types differ in the amount of memory allocated and the size of the number that can be represented. The single-precision type (float) is stored in 32 bits, while the double-precision type (double) is stored in 64 bits. Floats and doubles can be positive or negative. The table above summarizes Java’s floating-point data types, their sizes in memory, and their maximum and minimum positive nonzero values.

There are two things you must learn to balance: precision and use of memory. There is a kind of law of equivalent exchange here that must be mastered. These two are conjugated variables. Like the uncertainty principle that trades certainty about one discrete observable for another (the more you know about momentum the less you know about position), you must skillfully trade precision for use of memory and vice versa.

Because of its greater precision, the double data type is usually preferred over the float data type. However, for calculations not requiring such precision, floats are often used because they require less memory. Although integers can be stored as doubles or floats, it isn’t advisable to do so because floating-point numbers require more processing time for calculations.

Witness here a few examples of floating-point variable declarations:

# Data Types, Variables, and Constants

In the previous blog post where we calculated the area of the last of the nine circles of hell, we used as data the value of PI and the radius, and found how large was Treachery. For each of these values, we assigned a name.

We also used the Java keyword double, which defines the data type of the data. The keyword double means that the value will be a floating-point number.

Java allows you to refer to the data in a program by defining variables, which are named locations in memory where you can store values. A variable can store one data value at a time, but that value might change as the program executes, and it might change from one execution of the program to the next. The real advantage of using variables is that you can name a variable, assign it a value, and subsequently refer to the name of the variable in an expression rather than hard-coding the specific value.

When we use a named variable, we need to tell the compiler which kind of data we will store in the variable. We do this by giving a data type for each variable. Java supports eight primitive data types: byte, short, int, long, float, double, char, and boolean. They are called primitive data types because they are part of the core Java language. The data type you specify for a variable tells the compiler how much memory to allocate and the format in which to store the data.

For example, if you specify that a data item is an int, then the compiler will allocate four bytes of memory for it and store its value as a 32-bit signed binary number. If, however, you specify that a data item is a double (a double-precision floating-point number), then the compiler will allocate 8 bytes of memory and store its value as an IEEE 754 floating-point number. Once you declare a data type for a data item, the compiler will monitor your use of that data item. If you attempt to perform operations that are not allowed for that type or are not compatible with that type, the compiler will generate an error.

Because the Java compiler monitors the operations on each data item, Java is called a strongly typed language. Take care in selecting identifiers for your programs. The identifiers should be meaningful and should reflect the data that will be stored in a variable, the concept encapsulated by a class, or the function of a method.

For example, the identifier age clearly indicates that the variable will hold an age. When you select meaningful variable names, the logic of your program is more easily understood, and you are less likely to introduce errors. Sometimes, it may be necessary to create a long identifier in order to clearly indicate its use, for example, numberOfPeopleSignedUpForCryonics. Although the length of identifiers is essentially unlimited, avoid creating extremely long identifiers because the longer the identifier, the more likely you are to make typos when entering the identifier into your program and the more it takes to type it, swallowing precious time. Finally, although it is legal to use identifiers, such as TRUE, which differ from Java keywords only in case, it isn’t a good idea because they easily can be confused with Java keywords, making the program logic less clear.